1、二级 C 语言-210 及答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.下列给定程序的功能是:调用函数 fun 将指定源文件中的内容复制到指定的目标文件中,复制成功时函数返回 1,失败时返回 0。在复制的过程中,把复制的内容输出到屏幕。主函数中源文件名放在变量sfname 中,目标文件名放在变量 tfname 中。 请在下划线处填入正确的内容并将下划线删除,使程序得出正确的结果。 注意 :部分源程序给出如下。 不得增行或删行,也不得更改程序的结构! 试题程序: #include stdio.h #include stdlib.h int
2、fun(char *source, char ,target) FILE *fs,*ft;char ch; /*found*/ if(fs=fopen(source, 1)=NULL) return0; if(ft=fopen(target, “w“)=NULL) return0; printf(“/nThe data in file :/n“); ch=fgetc(fs); /*found*/ while(!feof( 2) putchar(ch); /*found*/ fputc(ch, 3); ch=fgetc(fs); fclose(fs); fclose(ft); printf(“/
3、n/n“); return 1; main() char sfname20=“myfile1“,tfname20=“myfile2“; FILE *myf;int i;char c; myf=fopen(sfname,“w“); printf(“/nThe original data :/n“); for(i=1;i30; i+) c=“A“+rand()%25; fprintf(myf,“%c“);printf(“%c“,c); fclose(myf);printf(“/n/n“); if (fun(sfname, tfname) printf(“Succeed!“); else print
4、f(“Fail!“); (分数:30.00)二、程序改错题(总题数:1,分数:30.00)2.下列给定程序中函数 fun 的功能是:将长整型数中各位上为偶数的数依次取出,构成一个新数放在 t 中。高位仍在高位,低位仍在低位。 例如,当 s 中的数为 87653142 时,t 中的数为 8642。 请改正程序中的错误,使它能得出正确的结果。 注意 :不要改动 main 函数,不得增行或删行,也不得更改程序的结构! 试题程序: #include conio.h #include stdio.h void fun(long s,long *t) int d; long s1=1; *t=0; whi
5、le(s0) d=s%10; /*found*/ if(d%2=0) *t=d,s1+*t; s1*=10; /*found*/ s/=10; main() long s,t; printf(“/nPlease enter s:“); scanf(“%ld“, fun(s, printf(“The result is:%ld/n“,t); (分数:30.00)三、程序设计题(总题数:1,分数:40.00)3.编写函数 fun,其功能是:将两个两位数的正整数 a、b 合并成一个整数放在 c 中。合并的方式是:将a 数的十位和个位数依次放在 c 数的十位和干位上,b 数的十位和个位数依次放在 c
6、数的百位和个位上。 例如,当 a=45,b=12 时,调用该函数后,c=5142。 注意 :部分源程序给出如下。数据文件 INDAT 中的数据不得修改。 请勿改动主函数 main 和其他函数中的任何内容,仅在函数 fun 的花括号中填入编写的若干语句。 试题程序: #include stdlib.h #include stdio.h #include conio.h void fun(int a ,int b,long *c) void main() int a,b; long c; system(“CLS“); printf(“Input a,b:“); scanf(“%d%d“, fun(
7、a,b, printf(“The result is:%ld/n“,c); (分数:40.00)_二级 C 语言-210 答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.下列给定程序的功能是:调用函数 fun 将指定源文件中的内容复制到指定的目标文件中,复制成功时函数返回 1,失败时返回 0。在复制的过程中,把复制的内容输出到屏幕。主函数中源文件名放在变量sfname 中,目标文件名放在变量 tfname 中。 请在下划线处填入正确的内容并将下划线删除,使程序得出正确的结果。 注意 :部分源程序给出如下。 不得增行或删行,也不得更改程序的结
8、构! 试题程序: #include stdio.h #include stdlib.h int fun(char *source, char ,target) FILE *fs,*ft;char ch; /*found*/ if(fs=fopen(source, 1)=NULL) return0; if(ft=fopen(target, “w“)=NULL) return0; printf(“/nThe data in file :/n“); ch=fgetc(fs); /*found*/ while(!feof( 2) putchar(ch); /*found*/ fputc(ch, 3);
9、 ch=fgetc(fs); fclose(fs); fclose(ft); printf(“/n/n“); return 1; main() char sfname20=“myfile1“,tfname20=“myfile2“; FILE *myf;int i;char c; myf=fopen(sfname,“w“); printf(“/nThe original data :/n“); for(i=1;i30; i+) c=“A“+rand()%25; fprintf(myf,“%c“);printf(“%c“,c); fclose(myf);printf(“/n/n“); if (fu
10、n(sfname, tfname) printf(“Succeed!“); else printf(“Fail!“); (分数:30.00)解析:“r“ fs ft 解析 填空 1:本题考查对文件操作的掌握。打开一个文件的调用方式是,fsfopen(文件名,使用文件方式),以只读的方式打开文件,所以文件打开方式为“r“。 填空 2:while 循环语句中,循环条件通过 feof()函数来检测是否到文件结尾。 填空 3:fputc()函数用于将一个字符写到磁盘文件上去,调用形式为:fputc(要输出的字符,文件指针)。二、程序改错题(总题数:1,分数:30.00)2.下列给定程序中函数 fun
11、的功能是:将长整型数中各位上为偶数的数依次取出,构成一个新数放在 t 中。高位仍在高位,低位仍在低位。 例如,当 s 中的数为 87653142 时,t 中的数为 8642。 请改正程序中的错误,使它能得出正确的结果。 注意 :不要改动 main 函数,不得增行或删行,也不得更改程序的结构! 试题程序: #include conio.h #include stdio.h void fun(long s,long *t) int d; long s1=1; *t=0; while(s0) d=s%10; /*found*/ if(d%2=0) *t=d,s1+*t; s1*=10; /*foun
12、d*/ s/=10; main() long s,t; printf(“/nPlease enter s:“); scanf(“%ld“, fun(s, printf(“The result is:%ld/n“,t); (分数:30.00)解析:if(d%2=0) s/=10; 解析 (1)分析程序,在 if 的条件表达式中,不应该出现赋值运算符“,应使用“运算符。 (2)这里是一个运算符的错误,表示除法的运算符是“/“。三、程序设计题(总题数:1,分数:40.00)3.编写函数 fun,其功能是:将两个两位数的正整数 a、b 合并成一个整数放在 c 中。合并的方式是:将a 数的十位和个位数依
13、次放在 c 数的十位和干位上,b 数的十位和个位数依次放在 c 数的百位和个位上。 例如,当 a=45,b=12 时,调用该函数后,c=5142。 注意 :部分源程序给出如下。数据文件 INDAT 中的数据不得修改。 请勿改动主函数 main 和其他函数中的任何内容,仅在函数 fun 的花括号中填入编写的若干语句。 试题程序: #include stdlib.h #include stdio.h #include conio.h void fun(int a ,int b,long *c) void main() int a,b; long c; system(“CLS“); printf(“
14、Input a,b:“); scanf(“%d%d“, fun(a,b, printf(“The result is:%ld/n“,c); (分数:40.00)_正确答案:()解析:void fun(int a, int b, long *c) *c=b%10+(a/10)*10+(b/10)*100+(a%10)*1000; 解析 本题中主要的问题是如何取出 a 和 b 的个位数和十位数,取出后如何表示成 c 中相应的位数。由于 a 和 b 都是只有两位的整数,所以分别对它们除 10 可得到它们的十位数,分别用 10 对它们求余可得到它们的个位数。得到后对应乘以 1000、100、10、1 就可得到 c 的千位数、百位数、十位数和个位数。注意:使用 c 时要进行指针运算。