1、二级 C 语言-346 (1)及答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.请补充 main()函数,该函数的功能是:从键盘输入两个字符串并分别保存在字符数组 str1 和 str2 中,用字符串 str2 替换字符串 str1 前面的所有字符,且 str2 的长度不大于 str1,否则需要重新输入。 例如,如果输入 str1=“abcdefg“,str2=“hij“,则输出“hijdefg“。 注意:部分源程序给出如下。 请勿改动 main()函数和其他函数中的任何内容,仅在 main()函数的横线上填入所编写的若干表达式或语句。 试
2、题程序: #includestdlib.h #includestdio.h #includestring.h void main() char str181,str281; char*p1=str1,*p2=str2; system(“CLS“); do printf(“Input str1/n“); gets(str1); printf(“Input str2/n“); gets(str2); while( 1); while( 2) *p1+=*p2+; printf(“Display str1/n“); puts( 3); (分数:30.00)二、程序改错题(总题数:1,分数:30.00
3、)2.下列给定程序中,函数 proc()的功能是:用下面的公式求 的近似值,直到最后一项的绝对值小于指定的数(参数 num)为止。 /41-1/3+1/5-1/7+ 例如,程序运行后,输入 0.0001,则程序输出 3.1414。 请修改程序中的错误,使它能得出正确的结果。 注意:不要改动 main()函数,不得增行或删行,也不得更改程序的结构。 试题程序: #includestdlib.h #includemath.h #includestdio.h float proc(float num) int s; float n,t,pi; t=1;pi=0;n=1;s=1; /*found* w
4、hile(t=num) pi=pi+t; n=n+2; s=-s; /*found* t=s%n; pi=pi*4; return pi; void main() float n1,n2; system(“CLS“); printf(“Enter a float number:“); scanf(“%f“, n2=proc(n1); printf(“%6.4f/n“,n2); (分数:30.00)三、程序设计题(总题数:1,分数:40.00)3.编写函数 void proc(int x,int pp,int*n),它的功能是求出能整除 x 且不是奇数的各整数,并按从小到大的顺序放在 pp 所指
5、的数组中,这些除数的个数通过形参 n 返回。 例如,若 x 中的值为 30,则有 4 个数符合要求,它们是 26 10 30。 注意:部分源程序给出如下。 请勿改动 main()函数和其他函数中的任何内容,仅在函数 proc()的花括号中填入所编写的若干语句。 试题程序: #includeconio.h #includestdio.h #includestdlib.h void proc(int x,int pp,int*n) void main() int x,arr1000,n,i; system(“CLS“); printf(“/nPlease enter an integer numb
6、er:/n“); scanf(“%d“, proc(x,arr. for(i=0;in;i+) printf(“%d“,arri); printf(“/n“); (分数:40.00)_二级 C 语言-346 (1)答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.请补充 main()函数,该函数的功能是:从键盘输入两个字符串并分别保存在字符数组 str1 和 str2 中,用字符串 str2 替换字符串 str1 前面的所有字符,且 str2 的长度不大于 str1,否则需要重新输入。 例如,如果输入 str1=“abcdefg“,str2=
7、“hij“,则输出“hijdefg“。 注意:部分源程序给出如下。 请勿改动 main()函数和其他函数中的任何内容,仅在 main()函数的横线上填入所编写的若干表达式或语句。 试题程序: #includestdlib.h #includestdio.h #includestring.h void main() char str181,str281; char*p1=str1,*p2=str2; system(“CLS“); do printf(“Input str1/n“); gets(str1); printf(“Input str2/n“); gets(str2); while( 1)
8、; while( 2) *p1+=*p2+; printf(“Display str1/n“); puts( 3); (分数:30.00)解析:strlen(str1)strlen(str2) *p2 str1解析 按照题目中要求,用字符串 str2 替换字符串 str1前面的所有字符的条件为:str2 的长度不大于 str1 的长度。因此,第一处填“strlen(str1)strlen(str2)”。当 str2 不结束时,将 str2 中的每一个字符替换 str1 前面的所有字符。因此,第二处填“*p2”。由程序可知,结果字符串存放在变量 str1 中。因此,第三处填“str1”。二、程序
9、改错题(总题数:1,分数:30.00)2.下列给定程序中,函数 proc()的功能是:用下面的公式求 的近似值,直到最后一项的绝对值小于指定的数(参数 num)为止。 /41-1/3+1/5-1/7+ 例如,程序运行后,输入 0.0001,则程序输出 3.1414。 请修改程序中的错误,使它能得出正确的结果。 注意:不要改动 main()函数,不得增行或删行,也不得更改程序的结构。 试题程序: #includestdlib.h #includemath.h #includestdio.h float proc(float num) int s; float n,t,pi; t=1;pi=0;n
10、=1;s=1; /*found* while(t=num) pi=pi+t; n=n+2; s=-s; /*found* t=s%n; pi=pi*4; return pi; void main() float n1,n2; system(“CLS“); printf(“Enter a float number:“); scanf(“%f“, n2=proc(n1); printf(“%6.4f/n“,n2); (分数:30.00)解析:错误:while(t=num) 正确:while(fabs(t)=num) 错误:t=s%n; 正确:t=s/n; 解析 按照题目中要求直到最后一项的绝对值小
11、于指定的数(参数 num)为止,取绝对值函数为 fabs。因此,“while(t=num)”应改为“while(fabs(t)=num)”;变量 t 表示的是变量 s 与变量 n 相除的结果,而不是取模。因此,“t=s%n;”应改为“t=s/n;”。三、程序设计题(总题数:1,分数:40.00)3.编写函数 void proc(int x,int pp,int*n),它的功能是求出能整除 x 且不是奇数的各整数,并按从小到大的顺序放在 pp 所指的数组中,这些除数的个数通过形参 n 返回。 例如,若 x 中的值为 30,则有 4 个数符合要求,它们是 26 10 30。 注意:部分源程序给出如
12、下。 请勿改动 main()函数和其他函数中的任何内容,仅在函数 proc()的花括号中填入所编写的若干语句。 试题程序: #includeconio.h #includestdio.h #includestdlib.h void proc(int x,int pp,int*n) void main() int x,arr1000,n,i; system(“CLS“); printf(“/nPlease enter an integer number:/n“); scanf(“%d“, proc(x,arr. for(i=0;in;i+) printf(“%d“,arri); printf(“/n“); (分数:40.00)_正确答案:()解析:void proc(int x,int pp,int*n) int i,j=0; for(i=2;i=x;i=i+2) /i 被整除的偶数 if(x%i=0) ppj+=i; /如果成立,则把其放到 pp 所指的数组中 *n=j; /把下标放到 n 中,通过指针返回到主函数中 解析 按照题目中要求,求出能整除 x 且不是奇数的各整数。首先判断小于等于整数 x 的所有偶数是否能被 x 整除,将能被 x 整除的奇数放入数组 pp 中。最后将数组 pp 中元素的个数返回到主函数当中。