1、二级 C 语言-286 (1)及答案解析(总分:64.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.请补充 fun()函数,该函数的功能是:把一个整数转换成字符串,并倒序保存在字符数组 S 中。 例如,当 n=123 时,s=“321“。 注意:部分源程序给出如下。 请勿改动 main()函数和其他函数中的任何内容,仅在 main()函数的横线上填入所编写的若干表达式或语句。 试题程序: #includestdio.h #includeconio.h #define M 80 char sM; void fun(long int n) int j=0; whil
2、e( 1) sj= 2; n/=10; j+; 3; void main() long int n=1234567; printf(“The origial data/n“); printf(“n=%1d“, n); fun(n); printf(“/n%s“, s); (分数:30.00)二、程序改错题(总题数:1,分数:4.00)2.下列给定程序中,函数 fun()的功能是:将 str 所指字符串中的字母转换为按字母序列的后续字母(Z 转换 A,z 转换 a),其他字符不变。 请修改函数 fun()中的错误,得出正确的结果。 注意:不要改动 main()函数,不得增行或删行,也不得更改程序
3、的结构。 试题程序: #includestdio.h #includectype.h #includeconio.h void fun(char *str) /*found* while(*str1=“) if(*str=“A“ else if(*str=“z“) *str=“a“; else *str+=1; /*found* (*str)+; void main() char str80; printf(“/n Enter a string with length80. :/n/n“); gets(str); printf(“/n The string:/n/n“); puts(str);
4、 fun(str); printf(“/n/n The Cords: /n/n“); puts(str); (分数:4.00)_三、程序设计题(总题数:1,分数:30.00)3.请编一个函数 fun(char *str),该函数的功能是把字符串中的内容逆置。 例如,字符串中原有的字符串为 asdfg,则调用该函数后,串中的内容为 gfdsa。 请勿改动 main()函数和其他函数中的任何内容,仅在函数 proc()的花括号中填入所编写的若干语句。 试题程序: #includestring.h #includeconio.h #includestdio.h #define N 100 void
5、fun(char *str) void main() char aN; FILE *out; printf(“Enter a string:“); gets(a); printf(“The original string is:“); puts(a); fun(a); printf(“The string after modified:“); puts(a); strcpy(a, “Welcome!“); fun(a); out=fopen(“outfile.dat“, “w“); fprintf(out, “%s“, a); fclose(out); (分数:30.00)_二级 C 语言-2
6、86 (1)答案解析(总分:64.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.请补充 fun()函数,该函数的功能是:把一个整数转换成字符串,并倒序保存在字符数组 S 中。 例如,当 n=123 时,s=“321“。 注意:部分源程序给出如下。 请勿改动 main()函数和其他函数中的任何内容,仅在 main()函数的横线上填入所编写的若干表达式或语句。 试题程序: #includestdio.h #includeconio.h #define M 80 char sM; void fun(long int n) int j=0; while( 1) sj=
7、2; n/=10; j+; 3; void main() long int n=1234567; printf(“The origial data/n“); printf(“n=%1d“, n); fun(n); printf(“/n%s“, s); (分数:30.00)解析:n0 n%10+“0“ sj=“/0“ 解析 该题中函数功能是把一个整数转换成字符串,并倒序保存在字符数组 s 中。本题解题思路是在循环中对字符串进行截取,并把截取后的字符串进行转换。 第一空:循环截取 n 的每一位,所以循环条件是 n 大于 0,即第一空填“n0”。 第二空:循环中截取当前数字的最低一位并进行数字到字符
8、的转换,数字字符与对应的整数 ASCII 码相差是 48,等于字符“0“的 ASCII 码,所以第二空填“n%10+“0“”。 第三空:将截取后的字符放入新串,在新串尾部加结束标志,所以第三空填“sj=“/0“”。二、程序改错题(总题数:1,分数:4.00)2.下列给定程序中,函数 fun()的功能是:将 str 所指字符串中的字母转换为按字母序列的后续字母(Z 转换 A,z 转换 a),其他字符不变。 请修改函数 fun()中的错误,得出正确的结果。 注意:不要改动 main()函数,不得增行或删行,也不得更改程序的结构。 试题程序: #includestdio.h #includectyp
9、e.h #includeconio.h void fun(char *str) /*found* while(*str1=“) if(*str=“A“ else if(*str=“z“) *str=“a“; else *str+=1; /*found* (*str)+; void main() char str80; printf(“/n Enter a string with length80. :/n/n“); gets(str); printf(“/n The string:/n/n“); puts(str); fun(str); printf(“/n/n The Cords: /n/n
10、“); puts(str); (分数:4.00)_正确答案:()解析:(1)错误:while(*str!=“) 正确:while(*str)或 while(*str!=“/0“)或 while(*str!=0) (2)错误:(*str+); 正确:str+ 解析 首先判断字母是否为小写字母,如果是小写字母则进行转换。 第一个标识下“while(*str!=“)”语句原意是 str 不指向字符串尾就进入下面的循环,所以对于指针判断不为结束符的语句应该是循环为真,所以应为“while(*str)”或“while(*str!=“/0“)”或“while(*str!=0)”。 第二个标识下“(*str
11、)+;”是对字符内容加 1,而原题是对字符位置加 1,所以将其改为“str+;”。三、程序设计题(总题数:1,分数:30.00)3.请编一个函数 fun(char *str),该函数的功能是把字符串中的内容逆置。 例如,字符串中原有的字符串为 asdfg,则调用该函数后,串中的内容为 gfdsa。 请勿改动 main()函数和其他函数中的任何内容,仅在函数 proc()的花括号中填入所编写的若干语句。 试题程序: #includestring.h #includeconio.h #includestdio.h #define N 100 void fun(char *str) void mai
12、n() char aN; FILE *out; printf(“Enter a string:“); gets(a); printf(“The original string is:“); puts(a); fun(a); printf(“The string after modified:“); puts(a); strcpy(a, “Welcome!“); fun(a); out=fopen(“outfile.dat“, “w“); fprintf(out, “%s“, a); fclose(out); (分数:30.00)_正确答案:()解析:char ch; int i, m, n; i=0; m=n=strlen(str)-1; /求字符串 str 长度 while(i(n+1)/2) /循环逆置交换 ch=stri; stri=strm; strm=ch; i+; m-; 解析 在 fun()函数中,首先求行字符串的长度,然后通过循环进行字符交换。要注意的是,如果字符串长度是奇数,则最中间的元素在逆置前后的位置是没有改变的。