C语言考试可能会涉及到的内容

以下是汽院下属科院C(B)考试范围,C(A)的话,不考的内容应该都会涉及到,重点内容会延伸至三、四章。

一、教材上的两套自测题

二、C语言实验与实训教程 第一章、第二章、第三章、第四章的典型练习及解释以及练习题中的选择题(凡二维数组,二重循环不考,递归不考,第一章、第二章是重点)

三、程序设计题

1、会编写函数计算所给参数因子之和

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<math.h>
 4 int main()
 5 {    
 6     /*
 7      * 改进版:能够求任意实数范围内的素数,并且将其逐个显示出来
 8      * 能够根据用户需要 自定义输出任意个数最大的素数
 9      */
10     int j,k,n,m=0,i,temp=0,input,input1;
11     int a[150];
12     printf("请问您想输入多少范围内的素数?比如500以内,输入500,回车键结束
");
13     scanf("%d",&input);
14     printf("1-%d内的所有素数如下表所示:
",input);
15     for(n=2;n<=input;n++)
16     {
17         k=0;
18         for(j=2;j<=sqrt(n);j++)
19         {
20             if(n%j==0)
21             {
22                 k++;
23                 break;//如果N能被j整除,则k+1
24             }
25         }
26         if(k==0)
27         {
28             m++;
29             printf("%5d",n);//如果N是素数输出n
30             a[m]=n;
31             if((m+1)%5==0)
32             {
33                 printf("
");//每行输入5个素数
34                 
35             }
36         }
37     }
38     printf("
合计有%d个素数
",m);
39     printf("你想输出多少个最大的素数?(倒序排列)
比如 输出三个素数就填3
");
40     scanf("%d",&input1);
41     printf("
最后%d个素数为:

",input1);
42     for(i=m;i>(m-input1);i--)
43     {
44         printf("a[%d]=%d
",i,a[i]);
45         temp=temp + a[i];
46         
47     }
48     printf("最后%d个元素之和为:%d
",input1,temp);
49 
50 }

2、编写函数float cal(float d1,char op,float d2),根据OP计算d1和d2的运算结果。

 1 #include<stdio.h>
 2 float cal(float d1,char op,float d2)
 3 {
 4     if(op=='+')
 5         return (d1+d2);
 6     else if(op=='-')
 7         return (d1-d2);
 8     else if(op=='*')
 9         return (d1*d2);
10     else if(op=='/')
11         return (d1/d2);
12     else 
13     {
14         printf("输入非法关联符!
");
15         return 0;
16     }
17 }
18 int main()
19 {
20     float d1,d2,result;
21     char op;
22     printf("请输入两个数:");
23     scanf("%f %f",&d1,&d2);
24     getchar();    //用来接收上句的回车符,避免对后续输入造成影响
25     printf("
请输入您想对两个数进行的操作符(+ - * /):");
26     scanf("%c",&op);
27     result=cal(d1,op,d2);
28     printf("结果为:%.2f
",result);
29 }

3、编写函数会求四个参数的最大值和最小值。

 1 #include<stdio.h>
 2 float array[4];
 3 void compare(float a,float b,float c,float d)
 4 {
 5     float temp=0;
 6     int i,j;
 7     array[0]=a;array[1]=b;array[2]=c;array[3]=d;
 8     /* 由于有四个参数,写一堆if else很蠢,这里选择冒泡排序 */
 9     for(i=0;i<3;i++)
10     {
11         for(j=0;j<3-i;j++)
12         {
13             if(array[j]>array[j+1])
14             {
15                 temp=array[j];
16                 array[j]=array[j+1];
17                 array[j+1]=temp;
18             }
19         }
20     }
21     printf("最大值为:%.2f,最小值为:%.2f",array[3],array[0]);
22 }
23 
24 int main()
25 {
26     float x1,x2,x3,x4;
27     printf("请输入四个数:");
28     scanf("%f %f %f %f",&x1,&x2,&x3,&x4);
29     compare(x1,x2,x3,x4);
30     return 0;
31 }

4、会编写函数计算所给参数组的元素之和或平均值。

 1 #include<stdio.h>
 2 float sum(float array[],int n)
 3 {
 4     float sum=0;
 5     for(int i=0;i<n;i++)
 6     {
 7         sum+=array[i];
 8     }
 9     return sum;
10 }
11 float average(float array[],int n)
12 {
13     float sum=0,average=0;
14     for(int i=0;i<n;i++)
15     {
16         sum+=array[i];
17     }
18     average=sum/n;
19     return average;
20 }
21 int main()
22 {
23     float array[]={1,2,3,4,5,6,7,8,9};
24     printf("%.2f
",sum(array,9));
25     average(array,9);
26     printf("%.2f
",average(array,9));
27 }

5、会编写函数计算参数所表示的重量的托运费。

例题3-5 编写程序计算行李的托运费。行李托运费计算方法:行李重量不超过50公斤时,每公斤0.5元;超过50公斤不超过100公斤时,其超过部分每公斤1.5元;超过100公斤时,其超过部分每公斤2元;

 1 #include<stdio.h>
 2 int main()
 3 {
 4     float weight,money;
 5     printf("请输入重量:");
 6     scanf("%f",&weight);
 7     if(weight<=50)
 8         money=0.5*weight;
 9     if(weight>50&&weight<=100)
10         money=50*0.5+1.5*(weight-50);
11     if(weight>100)
12         money=50*0.5+1.5*50+2*(weight-100);
13     printf("托运费为%.2f",money);
14     return 0;
15 }

6、会编写函数,求出形参数组所指字符串中指定字符的个数。

 1 #include<stdio.h>
 2 #include<string.h>
 3 int f(char a[],char x)
 4 {
 5     int len;
 6     int count = 0;
 7     len=strlen(a);
 8     for(int i=0;i<len;i++)
 9     {
10         if(a[i]==x)
11             count++;
12     }
13 
14     return count;
15 }
16 int main()
17 {
18     char input;
19     char a[50];
20     printf("请输入一串字符串:");
21     gets(a);
22     printf("请输入您想查询的字符:");
23     scanf("%c",&input);
24     printf("包含%c的字符个数有:%d个
",input,f(a,input));;
25     return 0;
26 }
原文地址:https://www.cnblogs.com/battlecry/p/6171876.html