1、二级 C 语言-210 (1)及答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.请补充函数 proc(),该函数可以统计一个长度为 n 的字符串在另一个字符串中出现的次数。例如,假定输入的字符串为:fjdkad*fdadf fdadjkfdad,子字符串为 ad,则应输出 4。 注意:部分源程序给出如下。 请勿改动 main()函数和其他函数中的任何内容,仅在函数 proc()的横线上填入所编写的若干表达式或语句。 试题程序: #includestdlib.h #includestdio.h #includestring.h #includ
2、econio.h int proc(char*str.char*sub) int n; char*p,*r; n=0; while(*str) p=str; r=sub; while( 1) if(*r=*p) r+; p+; else 2; if(*r=“/0“) n+; sir+; return 3; void main() char str81,suh3; int n; system(“CLS“); printf(“输入主字符串:“); gets(str); printf(“输入子字符串:“); gets(sub); puts(str); puts(sub); n=proc(str,su
3、b); printf(“n=%d/n“,n); (分数:30.00)二、程序改错题(总题数:1,分数:30.00)2.下列给定程序中,函数 proc()的功能是:先将字符串 s 中的字符按顺序存放到 t 中,然后把 s 中的字符按正序连接到 t 的后面。例如,当 s 中的字符串为 WXYZ 时,则 t 中的字符串应为 WXYZWXYZ。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动 main()函数,不得增行或删行,也不得更改程序的结构。 试题程序: #includestdlib.h #includeconio.h #includestdio.h #includestring.h
4、 void proc(char*s,char*t) int i,s1; s1=strlen(s); for(i=0;is1;i+) /*found* ti=ss1; for(i=0;is1;i+) ts1+i=si; t2*s1=“/0“; void main() char s100,t100; system(“CLS“); printf(“/nPlease enter string s:“); scanf(“%s“,s); proc(s,t); printf(“The result is:%s/n“,t); (分数:30.00)三、程序设计题(总题数:1,分数:40.00)3.请编写函数 p
5、roc(),其功能是:计算并输出下列多项式值。 S=(1+1/2)+(1/3+1/4)+(1/(2n-1)+1/2n) 例如,若主函数从键盘给 n 输入 100 后,则输出为 S=5.878031。 n 的值要求大于 1 但不大于 100。 注意:部分源程序给出如下。 请勿改动主函数 main()和其他函数中的任何内容,仅在函数 proc()的花括号中填入所编写的若干语句。 试题程序: #includestdio.h double proc(int n) void main() int n; double sum; printf(“/nInput n:“); scanf(“%d“, sum=p
6、roc(n); printf(“/ns=%f/n“,sum); (分数:40.00)_二级 C 语言-210 (1)答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.请补充函数 proc(),该函数可以统计一个长度为 n 的字符串在另一个字符串中出现的次数。例如,假定输入的字符串为:fjdkad*fdadf fdadjkfdad,子字符串为 ad,则应输出 4。 注意:部分源程序给出如下。 请勿改动 main()函数和其他函数中的任何内容,仅在函数 proc()的横线上填入所编写的若干表达式或语句。 试题程序: #includestdlib.
7、h #includestdio.h #includestring.h #includeconio.h int proc(char*str.char*sub) int n; char*p,*r; n=0; while(*str) p=str; r=sub; while( 1) if(*r=*p) r+; p+; else 2; if(*r=“/0“) n+; sir+; return 3; void main() char str81,suh3; int n; system(“CLS“); printf(“输入主字符串:“); gets(str); printf(“输入子字符串:“); gets
8、(sub); puts(str); puts(sub); n=proc(str,sub); printf(“n=%d/n“,n); (分数:30.00)解析:*r break n解析 由函数 proc()可知,字符指针变量 p 和 r 分别指向字符串和子字符串,要对其指向的每一个字符进行比较,除非子字符串结束,因此第一处填“*r”;如果当前比较的两个字符相等,则分别指向下一个字符,否则退出本次循环,进行下一次比较,因此第二处填“break”;最后要返回所含子字符串的个数,因此第三处填“n”。二、程序改错题(总题数:1,分数:30.00)2.下列给定程序中,函数 proc()的功能是:先将字符串
9、 s 中的字符按顺序存放到 t 中,然后把 s 中的字符按正序连接到 t 的后面。例如,当 s 中的字符串为 WXYZ 时,则 t 中的字符串应为 WXYZWXYZ。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动 main()函数,不得增行或删行,也不得更改程序的结构。 试题程序: #includestdlib.h #includeconio.h #includestdio.h #includestring.h void proc(char*s,char*t) int i,s1; s1=strlen(s); for(i=0;is1;i+) /*found* ti=ss1; for
10、(i=0;is1;i+) ts1+i=si; t2*s1=“/0“; void main() char s100,t100; system(“CLS“); printf(“/nPlease enter string s:“); scanf(“%s“,s); proc(s,t); printf(“The result is:%s/n“,t); (分数:30.00)解析:错误:ti=ss1; 正确:ti=si; 解析 函数 proc()首先要实现将数组 s 中的元素顺序放在数组 t 中,即数组 t 中下标为 i 的元素在数组 s 中下标也为 i,因此“ti=ss1;”应改为“ti=si;”。三、程
11、序设计题(总题数:1,分数:40.00)3.请编写函数 proc(),其功能是:计算并输出下列多项式值。 S=(1+1/2)+(1/3+1/4)+(1/(2n-1)+1/2n) 例如,若主函数从键盘给 n 输入 100 后,则输出为 S=5.878031。 n 的值要求大于 1 但不大于 100。 注意:部分源程序给出如下。 请勿改动主函数 main()和其他函数中的任何内容,仅在函数 proc()的花括号中填入所编写的若干语句。 试题程序: #includestdio.h double proc(int n) void main() int n; double sum; printf(“/n
12、Input n:“); scanf(“%d“, sum=proc(n); printf(“/ns=%f/n“,sum); (分数:40.00)_正确答案:()解析:double proc(int n) int i; double sum=0.0; /变量 s 存放和,因此其初始值为 0 for(i=1;i=n;i+)/从第 1 项到第 n 项 sum=sum+(1.0/(2*i-1)+1.0/(2*i); /把分子、分母转换为符合题意的类型 return sum; /最后把和值返回到主函数中 解析 首先定义一个变量来存放表达式的和,根据题目中提供的多项式,通过 n 次循环来求出 n 项的和。最后把所求得的和返回到主函数当中。