1、二级 C 语言-391 及答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.给定程序中,函数 fun 的功能是:根据形参 i 的值返回某个函数的值。当调用正确时,程序输出: x1=5.000000,x2=3.000000,x1*x1+x1*x2=40.000000。 注意:不得增行或删行,也不得更改程序的结构! 试题程序: #includestdio.h double f1(double x) return x*x; double f2(double x,double y) return x*y; /*found*/ 1fun(int i,d
2、ouble x,double y) if(i=1) /*found*/ return 2(x); else /*found*/ return 3(x,y); main() double x1=5,x2=3,r; r=fun(1,x1,x2); r+=fun(2,x1,x2); printf(“/nx1=%f,x2=%f,x1*x1+x1*x2=%f/n/n“,x1,x2,r); (分数:30.00)二、程序修改题(总题数:1,分数:30.00)2.下列给定程序中,函数 fun 的功能是:比较两个字符串,将长的字符串的首地址作为函数值返回。 请改正程序中的错误,使它能得出正确的结果。 注意:不要
3、改动 main 函数,不得增行或删行,也不得更改程序的结构! 试题程序: #includeconio.h #includestdio.h /*found*/ double fun(char*s,char*t) int s1=0,t1=0; char*ss,*tt; ss=s; tt=t; while(*ss) s1+; /*found*/ (*ss)+; while(*tt) t1+; /*found*/ (*tt)+; if(t1s1) return t; else return s; void main() char a80,b80; printf(“/nEnter a string:“)
4、; gets(a); printf(“/nEnter a string again:“); gets(b); printf(“/nThe longer is:/n/n%s/n“,fun(a,b); (分数:30.00)三、程序设计题(总题数:1,分数:40.00)3.请编写函数 fun,其功能是:移动字符串中的内容,移动的规则是把第 1m 个字符,平移到字符串的最后,把第 m+1 到最后的字符移到字符串的前部。 例如,字符串中原有的内容为“ABCDEFGHIJK“,m 的值为 3,移动后,字符串中的内容应该是“DEFGHIJKABC“。 注意:请勿改动 main 函数和其他函数中的任何内容,仅
5、在函数 fun 的花括号中填入你编写的若干语句。 试题程序: #includestdio.h #includestring.h #define N 80 void fun(char*w,int m) void main() FILE*wf; char aN=“ABCDEFGHIJK“,bN=“ABCDEFGHIJK“; int m; printf(“The origina string:/n“); puts(a); printf(“/n/nEnter m:“); scanf(“%d“, fun(a,m); printf(“/nThe string after moving:/n“); puts
6、(a); printf(“/n/n“); /*found*/ wf=fopen(“out.dat“,“w“); fun(b,3); fprintf(wf,“%s“,b); fclose(wf); /*found*/ (分数:40.00)_二级 C 语言-391 答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.给定程序中,函数 fun 的功能是:根据形参 i 的值返回某个函数的值。当调用正确时,程序输出: x1=5.000000,x2=3.000000,x1*x1+x1*x2=40.000000。 注意:不得增行或删行,也不得更改程序的结构!
7、 试题程序: #includestdio.h double f1(double x) return x*x; double f2(double x,double y) return x*y; /*found*/ 1fun(int i,double x,double y) if(i=1) /*found*/ return 2(x); else /*found*/ return 3(x,y); main() double x1=5,x2=3,r; r=fun(1,x1,x2); r+=fun(2,x1,x2); printf(“/nx1=%f,x2=%f,x1*x1+x1*x2=%f/n/n“,x
8、1,x2,r); (分数:30.00)解析:(1)double (2)f1 (3)f2 解析 填空 1:本空函数 fun 的返回值,根据 return 可知,不论返回的是 f1 还是 f2,它们的返回值均为double 型的,故此空应该填写 double。 填空 2:因为函数 fun 的功能是根据形参 i 的值返回某个函数的值,当 i 等于 1 时,应该返回的是 f1。 填空 3:如果不满足 if 的话,应该返回的是 f2。二、程序修改题(总题数:1,分数:30.00)2.下列给定程序中,函数 fun 的功能是:比较两个字符串,将长的字符串的首地址作为函数值返回。 请改正程序中的错误,使它能得
9、出正确的结果。 注意:不要改动 main 函数,不得增行或删行,也不得更改程序的结构! 试题程序: #includeconio.h #includestdio.h /*found*/ double fun(char*s,char*t) int s1=0,t1=0; char*ss,*tt; ss=s; tt=t; while(*ss) s1+; /*found*/ (*ss)+; while(*tt) t1+; /*found*/ (*tt)+; if(t1s1) return t; else return s; void main() char a80,b80; printf(“/nEnte
10、r a string:“); gets(a); printf(“/nEnter a string again:“); gets(b); printf(“/nThe longer is:/n/n%s/n“,fun(a,b); (分数:30.00)解析:(1)char*fun(char*s,char*t) (2)ss+; (3)tt+; 解析 (1)在主函数的输出语句中,函数:fun 是以字符串格式输出的,所以定义函数时应为char*fun(char*s,char*t)。 (2)和(3)这里是地址加 1,而不是内容加 1,所以改为 ss+和 tt+。三、程序设计题(总题数:1,分数:40.00)3
11、.请编写函数 fun,其功能是:移动字符串中的内容,移动的规则是把第 1m 个字符,平移到字符串的最后,把第 m+1 到最后的字符移到字符串的前部。 例如,字符串中原有的内容为“ABCDEFGHIJK“,m 的值为 3,移动后,字符串中的内容应该是“DEFGHIJKABC“。 注意:请勿改动 main 函数和其他函数中的任何内容,仅在函数 fun 的花括号中填入你编写的若干语句。 试题程序: #includestdio.h #includestring.h #define N 80 void fun(char*w,int m) void main() FILE*wf; char aN=“ABC
12、DEFGHIJK“,bN=“ABCDEFGHIJK“; int m; printf(“The origina string:/n“); puts(a); printf(“/n/nEnter m:“); scanf(“%d“, fun(a,m); printf(“/nThe string after moving:/n“); puts(a); printf(“/n/n“); /*found*/ wf=fopen(“out.dat“,“w“); fun(b,3); fprintf(wf,“%s“,b); fclose(wf); /*found*/ (分数:40.00)_正确答案:()解析:void fun(char*w,int m) int i,j; char t; for(i=1;i=m;i+)/*进行 m 次的循环左移*/ t=w0; for(j=1;wj!=“/0“;j+)/*从第 2 个字符开始以后的每个字符都依次前移一个字符*/ wj-1=wj; wj-1=t;/*将第 1 个字符放到最后一个字符中*/ 解析 本题应采用“循环左移”的算法,多层循环用于控制移动的字符的个数,即需进行多少次循环,内嵌循环的作用是将从第 2 个字符以后的每个字符依次前移一个位置,最后将第 1 个字符放到最后一个字符中。