1、二级 C 语言-134 及答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.请补充函数 proc(),该函数的功能是:计算下面公式的值(k50): (分数:30.00)二、程序改错题(总题数:1,分数:40.00)2.下列给定程序中,函数 proc()的功能是:用冒泡法对 6 个字符串按由小到大的顺序进行排序。 请修改程序中的错误,使它能得到正确结果。 注意:不要改动 main()函数,不得增行或删行,也不得更改程序的结构。 试题程序: #includestring.h #includestdlib.h #includeconio.h #in
2、cludestdio.h #define MAX 20 void proc(char *pstr6) int i, j; char *p; for(i=0; i5; i+) for(j=i+1; j6; j+) /*found* if(strcmp(pstr+i), (pstr+j)0) p=*(pstr+i); *(pstr+i)=*(pstr+j); /*found* *(pstr+j)=*p; void main() int i; char *p6, str6MAX; system(“CLS“); for(i=0; i6; i+) pi=stri; printf(“/nEnter 6 s
3、tring(1 string at each line):/n“); for(i=0; i6; i+) scanf(“%s“, pi); proc(p); printf(“The strings after sorting:/n“); for(i=0; i6; i+) printf(“%s/n“, pi); (分数:40.00)_三、程序设计题(总题数:1,分数:30.00)3.请编写函数 proc(),它的功能是:求出 str 所指字符串中指定字符的个数,并返回此值。 例如,若输入字符串 12341234123,输入字符 4,则输出 2。 注意:部分源程序给出如下。 请勿改动 main()函
4、数和其他函数中的任何内容,仅在函数 proc()的花括号中填入所编写的若干语句。 试题程序: #includestdlib.h #includeconio.h #includestdio.h #define N 81 int proc(char*str, char c) void main() char aN, ch; system(“CLS“); printf(“/nPlease enter a string:“); gets(a); printf(“/nPlease enter a char:“); ch=getchar(); printf(“/nThe number of the cha
5、r is: %d/n“, proc(a, ch); (分数:30.00)_二级 C 语言-134 答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.请补充函数 proc(),该函数的功能是:计算下面公式的值(k50): (分数:30.00)解析:1 k=n m*=f/p解析 题目中要求的是各项元素的乘积,因此其初始值为 1,第一处填“1”;变量 k 表示的是乘积的项数,故其一定要小于等于 n,因此第二处填“k=n”;由函数 proc()可知,变量f 表示各项的分子,变量 p 表示各项的分母,因此第三处填“m*=f/p”。二、程序改错题(总题数
6、:1,分数:40.00)2.下列给定程序中,函数 proc()的功能是:用冒泡法对 6 个字符串按由小到大的顺序进行排序。 请修改程序中的错误,使它能得到正确结果。 注意:不要改动 main()函数,不得增行或删行,也不得更改程序的结构。 试题程序: #includestring.h #includestdlib.h #includeconio.h #includestdio.h #define MAX 20 void proc(char *pstr6) int i, j; char *p; for(i=0; i5; i+) for(j=i+1; j6; j+) /*found* if(str
7、cmp(pstr+i), (pstr+j)0) p=*(pstr+i); *(pstr+i)=*(pstr+j); /*found* *(pstr+j)=*p; void main() int i; char *p6, str6MAX; system(“CLS“); for(i=0; i6; i+) pi=stri; printf(“/nEnter 6 string(1 string at each line):/n“); for(i=0; i6; i+) scanf(“%s“, pi); proc(p); printf(“The strings after sorting:/n“); for
8、(i=0; i6; i+) printf(“%s/n“, pi); (分数:40.00)_正确答案:()解析:(1)错误:if(strcmp(pstr+i), (pstr+j)0) 正确:if(strcmp(*(pstr+i), *(pstr+j)0) (2)错误:*(pstr+j)=*p 正确:*(pstr+j)=p 解析 变量 pstr 表示的是字符串数组的首地址,pstr+i 表示的是字符串首地址偏移量为 i 处的地址。程序中要比较的是字符,因此,“if(strcmp(pstr+i), (pstr+j)0)”应改为“if(strcmp(*(pstr+i), *(pstr+j)0)”;根据
9、程序可知,要交换的是字符串的首地址而不是字符串的内容,因此,“*(pstr+j)=*p”应改为“*(pstr+j)=p”。三、程序设计题(总题数:1,分数:30.00)3.请编写函数 proc(),它的功能是:求出 str 所指字符串中指定字符的个数,并返回此值。 例如,若输入字符串 12341234123,输入字符 4,则输出 2。 注意:部分源程序给出如下。 请勿改动 main()函数和其他函数中的任何内容,仅在函数 proc()的花括号中填入所编写的若干语句。 试题程序: #includestdlib.h #includeconio.h #includestdio.h #define N
10、 81 int proc(char*str, char c) void main() char aN, ch; system(“CLS“); printf(“/nPlease enter a string:“); gets(a); printf(“/nPlease enter a char:“); ch=getchar(); printf(“/nThe number of the char is: %d/n“, proc(a, ch); (分数:30.00)_正确答案:()解析:int proc(char *str, char c) int i=0; /i 是放与 c 相同的字符的个数的变量 for(; *str!=“/0“; str+) if(*str=c) /str 所指字符串中指定字符的个数 i+; return i; 解析 求出 str 所指字符串中指定字符的个数,可以通过将 str 所指字符串中每一个字符与指定字符相比较,变量 i 中存放字符串中指定字符的个数,最后返回给主函数。