1、二级 C 语言-304 及答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.程序通过定义学生结构体变量,存储了学生的学号、姓名和三门课的成绩。函数 fun 的功能是将形参 a所指结构体变量中的数据赋给函数中的结构体变量 b,并修改 b 中的学号和姓名,最后输出修改后的数据。例如:a 所指变量中的学号、姓名和三门课的成绩依次是:10001、“ZhangSan“、95、80、88,则修改后输出 b 中的数据应为:10002、“LiSi“、95、80、88。 请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。 不得增行或删行,也
2、不得更改程序的结构! 给定源程序: #includestdio.h #includestring.h struct student long sno; char name10; float score3; ; void fun(struct student a) struct student b;int i; /*found*/ b= 1; b.sno=10002; /*found*/ strcpy( 2,“LiSi“); printf(“/nThe data after modified:/n“); printf(“/nNo:%ld Name:%s/nScores:“,b.sno,b.nam
3、e); /*found*/ for(i=0;i3;i+)printf(“%6.2f“,b. 3); printf(“/n“); main() struct student s=10001,“ZhangSan“,95,80,88; int i; printf(“/n/nThe original data:/n“); printf(“/nNo:%ld Name:%s/nScores:“,s.sno,s.name); for(i=0;i3;i+)printf(“%6.2f“,s.scorei); printf(“/n“); fun(s); (分数:30.00)二、程序改错题(总题数:1,分数:30.
4、00)2.给定程序中函数 fun 的功能是:从 s 所指字符串中删除所有小写字母 c。 请改正程序中的错误,使它能计算出正确的结果。 注意:不要改动 main 函数,不得增行或删行,也不得更改程序的结构! 给定源程序: #includestdio.h void fun(char*s) int i,j; for(i=j=0;si!=“/0“:i+) if(si!=“c“) /*found*/ sj=si; /*found*/ si=/0“; main() char s80; printf(“Enter a string: “);gets(s); printf(“The original stri
5、ng: “);puts(s); fun(s); printf(“The string after deleted:“); puts(s);printf(“/n/n“); (分数:30.00)三、程序设计题(总题数:1,分数:40.00)3.假定输入的字符串中只包含字母和*号。请编写函数 fun,它的功能是:将字符串中的前导*号全部移到字符串的尾部。函数 fun 中给出的语句仅供参考。 例如,字符串中的内容为:*A*BC*DEF*G*,移动后,字符串中的内容应当是:A*BC*DEF*G*。在编写函数时,不得使用 C 语言提供的字符串函数。 请勿改动主函数 main 和其它函数中的任何内容,仅在函
6、数 fun 的花括号中填入你编写的若干语句。 给定源程序: #includestdio.h void funl(char*a) /*以下代码仅供参考*/ char*p,*q; iut n=0; p=a; while(*p=“*“)/*统计串头“*“个数 n*/ n+;p+;) q=a; /*向前复制字符串,请填写相应的语句完成其功能*/ for(;n0;n-)/*在串尾补 n 个“*“*/ *q+=“*“; *q=“/0“; main() char s81;int n=0; printf(“Enter a string:/n“);gets(s); fun(s); printf(“The str
7、ing after moveing:/n“);puts(s); (分数:40.00)_二级 C 语言-304 答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.程序通过定义学生结构体变量,存储了学生的学号、姓名和三门课的成绩。函数 fun 的功能是将形参 a所指结构体变量中的数据赋给函数中的结构体变量 b,并修改 b 中的学号和姓名,最后输出修改后的数据。例如:a 所指变量中的学号、姓名和三门课的成绩依次是:10001、“ZhangSan“、95、80、88,则修改后输出 b 中的数据应为:10002、“LiSi“、95、80、88。 请在程
8、序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。 不得增行或删行,也不得更改程序的结构! 给定源程序: #includestdio.h #includestring.h struct student long sno; char name10; float score3; ; void fun(struct student a) struct student b;int i; /*found*/ b= 1; b.sno=10002; /*found*/ strcpy( 2,“LiSi“); printf(“/nThe data after modified:/n“); prin
9、tf(“/nNo:%ld Name:%s/nScores:“,b.sno,b.name); /*found*/ for(i=0;i3;i+)printf(“%6.2f“,b. 3); printf(“/n“); main() struct student s=10001,“ZhangSan“,95,80,88; int i; printf(“/n/nThe original data:/n“); printf(“/nNo:%ld Name:%s/nScores:“,s.sno,s.name); for(i=0;i3;i+)printf(“%6.2f“,s.scorei); printf(“/n
10、“); fun(s); (分数:30.00)解析:(1)a (2)b.name (3)scorei 解析 填空 1:题目要求先将形参 a 所指结构体变量中的数据赋给函数中的结构体变量 b,所以此处 b-a。 填空 2:通过 strcpy 函数修改 b 中的学生姓名,注意结构变量成员的表示方法。 填空 3:printf 函数输出结果。二、程序改错题(总题数:1,分数:30.00)2.给定程序中函数 fun 的功能是:从 s 所指字符串中删除所有小写字母 c。 请改正程序中的错误,使它能计算出正确的结果。 注意:不要改动 main 函数,不得增行或删行,也不得更改程序的结构! 给定源程序: #in
11、cludestdio.h void fun(char*s) int i,j; for(i=j=0;si!=“/0“:i+) if(si!=“c“) /*found*/ sj=si; /*found*/ si=/0“; main() char s80; printf(“Enter a string: “);gets(s); printf(“The original string: “);puts(s); fun(s); printf(“The string after deleted:“); puts(s);printf(“/n/n“); (分数:30.00)解析:(1)sj+=si;或sj=s
12、i;j+; (2)sj=0; 解析 由循环条件 if(si!=“c“)可知,如果字符串中的字符不是“c“,则将保留该字符到sj位置,然后下标 j 进行加 1 操作。循环结束后要在新串的结尾加上“/0“。三、程序设计题(总题数:1,分数:40.00)3.假定输入的字符串中只包含字母和*号。请编写函数 fun,它的功能是:将字符串中的前导*号全部移到字符串的尾部。函数 fun 中给出的语句仅供参考。 例如,字符串中的内容为:*A*BC*DEF*G*,移动后,字符串中的内容应当是:A*BC*DEF*G*。在编写函数时,不得使用 C 语言提供的字符串函数。 请勿改动主函数 main 和其它函数中的任何
13、内容,仅在函数 fun 的花括号中填入你编写的若干语句。 给定源程序: #includestdio.h void funl(char*a) /*以下代码仅供参考*/ char*p,*q; iut n=0; p=a; while(*p=“*“)/*统计串头“*“个数 n*/ n+;p+;) q=a; /*向前复制字符串,请填写相应的语句完成其功能*/ for(;n0;n-)/*在串尾补 n 个“*“*/ *q+=“*“; *q=“/0“; main() char s81;int n=0; printf(“Enter a string:/n“);gets(s); fun(s); printf(“The string after moveing:/n“);puts(s); (分数:40.00)_正确答案:()解析:while(*p!=“/0“) *(q+)=*(p+); 解析 函数 fun 的功能是将字符串中的前导*号全部移到字符串的尾部。本题解题思路: (1)定义一个指针并指向字符串的首地址; (2)利用循环语句找出字符串的前导*号的个数 n; (3)利用循环语句把剩余的字符拷贝到另一个字符串中; (4)在字符串的末尾接上 n 个*号。