1、二级 C 语言-609 及答案解析(总分:60.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:20.00)1.给定程序中,函数 fun 的功能是:计算出带有头结点的单向链表中各结点数据域中值之和作为函数值返回。 请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。 #includestdio.h #includestdlib.h #define N 8 typedef struct list int data; struct list *next; SLIST; SLIST*creatlist(int*); void outlist(SLIST*); int
2、fun(SLIST*h) SLIST *p;int s=0; p=h-next; while(p) /*found*/ s+=P- 1; /*found*/ p=p- 2; return s; main() SLIST*head; int aN=12,87,45,32,91,16,20,48; head=creatlist(a);outlist(head); /*found*/ printf(“/nsum=%d/n“,fun( 3); SLIST*creatlist(int a) SLIST*h,*p,*q;int i; h=p=(SLIST*)malloc(sizeof(SLIST); fo
3、r(i=0;iN;i+) q=(SLIST*)malloc(sizeof(SLIST); q-data=ai;p-next=q;p=q; P-next=0: return h; void outlist(SLIST*h) SLIST*p; p=h-next; if(p=NULL)printf(“The list is NULL!/n“); else printf(“/nHead“); do printf(“-%dtt,p-data);p=p-next; while(p!=NULL); printf(“-End/n“); (分数:20.00)二、程序修改题(总题数:1,分数:20.00)2.给定
4、程序 MODI1.C 中函数 fun 的功能是:将 s 所指字符串中出现的与 t1 所指字符串相同的子串全部替换成 t2 所指字符串,所形成的新串放在 w 所指的数组中。在此处,要求 t1 和 t2 所指字符串的长度相同。例如,当 s 所指字符串中的内容为:“abcdabfab“,t1 所指子串中的内容为:“ab“,t2 所指子串中的内容为:“99“时,结果在 w 所指的数组中的内容应为:“99cd99f99“。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动 main 函数,不得增行或删行,也不得更改程序的结构。 #includestdio.h #includestring.h
5、void fun(char*s,char*t1,char*t2,char*w) char*p,*r,*a; strcpy(w,s); while(*w) p=w;r=t1; /*found*/ while(r) if(*r=*p)r+;p+; else break; if(*r=“/0“) a=w;r=t2; while(*r) /*found*/ *a=*r;a+;r+ w+=strlen(t2); else w+: main() chars100,t1100,t21003,w100; printf(“/nPlease enter string S:“);scanf(“%s“,s); pri
6、ntf(“/nPlease enter substring t1:“);scanf(“%s“,t1); printf(“/nPlease enter substring t2:“);scanf(“%s“,t2); if(strlen(t1)=strlen(t2) fun(s,t1,t2,w); printf(“/nThe result is:%s/n“,w); else printf(“Error:strlen(t1)!=strlen(t2)/n“); (分数:20.00)_三、程序设计题(总题数:1,分数:20.00)3.函数 fun 的功能是:将 s 所指字符串中下标为偶数的字符删除,串中
7、剩余字符形成的新串放在 t 所指数组中。 例如,当 s 所指字符串中的内容为:“ABCDEFGHIJK”,在 t 所指数组中的内容应是:“BDFHJ”。 注意:部分源程序存在文件 FROG1.C 中。 请勿改动主函数 main 和其他函数中的任何内容,仅在函数 fun 的花括号中填入要编写的若干语句。 #includestdio.h #includestring.h void fun(char*s,char t) main() char s100,t100; void NONO(); printf(“/nPlease enter string S:“);scanf(“%s“,s); fun(s
8、,t); printf(“/nThe result is:%s/n“,t); NONO(); void NONO() /*本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。*/ char s100,t100; FILE*rf,*wf; int i; rf=fopen(“in.dat“,“r“); wf=fopen(“out.dat“,“w“); for(i=0;i10;i+) fscanf(rf“%s“,s); fun(s,t); fprintf(wf,“%s/n“,t); fclose(rf); fclose(wf); (分数:20.00)_二级 C 语言-609 答案解析(总分:
9、60.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:20.00)1.给定程序中,函数 fun 的功能是:计算出带有头结点的单向链表中各结点数据域中值之和作为函数值返回。 请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。 #includestdio.h #includestdlib.h #define N 8 typedef struct list int data; struct list *next; SLIST; SLIST*creatlist(int*); void outlist(SLIST*); int fun(SLIST*h) SLIST *p
10、;int s=0; p=h-next; while(p) /*found*/ s+=P- 1; /*found*/ p=p- 2; return s; main() SLIST*head; int aN=12,87,45,32,91,16,20,48; head=creatlist(a);outlist(head); /*found*/ printf(“/nsum=%d/n“,fun( 3); SLIST*creatlist(int a) SLIST*h,*p,*q;int i; h=p=(SLIST*)malloc(sizeof(SLIST); for(i=0;iN;i+) q=(SLIST
11、*)malloc(sizeof(SLIST); q-data=ai;p-next=q;p=q; P-next=0: return h; void outlist(SLIST*h) SLIST*p; p=h-next; if(p=NULL)printf(“The list is NULL!/n“); else printf(“/nHead“); do printf(“-%dtt,p-data);p=p-next; while(p!=NULL); printf(“-End/n“); (分数:20.00)解析:data next head 解析 函数 fun 的功能是计算出链表中各结点数据域中值之和
12、,由于链表单向,利用循环从头结点开始搜索直到最后一个结点,每找到一个结点就把结点的数据域上的数据累加起来。 第一空:循环“while(p)”遍历整个链表,在循环体中,s 是累加变量,每个结点的数据域上的数据都累加在 s 上。因此第一空处应为“s+=p-data;”。 第二空:p 是循环变量,p 不断指向下一个结点,故第二空处应为“p=p-next;”。 第三空:fun 函数的声明为:int fun(SLIST*h),参数是 SLIST 结构体指针,表示头结点地址。因此第三空处的函数调用应为“printf(“/nsum=%d/n“,fun(head);”。 考点 链表;指针。二、程序修改题(总题
13、数:1,分数:20.00)2.给定程序 MODI1.C 中函数 fun 的功能是:将 s 所指字符串中出现的与 t1 所指字符串相同的子串全部替换成 t2 所指字符串,所形成的新串放在 w 所指的数组中。在此处,要求 t1 和 t2 所指字符串的长度相同。例如,当 s 所指字符串中的内容为:“abcdabfab“,t1 所指子串中的内容为:“ab“,t2 所指子串中的内容为:“99“时,结果在 w 所指的数组中的内容应为:“99cd99f99“。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动 main 函数,不得增行或删行,也不得更改程序的结构。 #includestdio.h
14、#includestring.h void fun(char*s,char*t1,char*t2,char*w) char*p,*r,*a; strcpy(w,s); while(*w) p=w;r=t1; /*found*/ while(r) if(*r=*p)r+;p+; else break; if(*r=“/0“) a=w;r=t2; while(*r) /*found*/ *a=*r;a+;r+ w+=strlen(t2); else w+: main() chars100,t1100,t21003,w100; printf(“/nPlease enter string S:“);s
15、canf(“%s“,s); printf(“/nPlease enter substring t1:“);scanf(“%s“,t1); printf(“/nPlease enter substring t2:“);scanf(“%s“,t2); if(strlen(t1)=strlen(t2) fun(s,t1,t2,w); printf(“/nThe result is:%s/n“,w); else printf(“Error:strlen(t1)!=strlen(t2)/n“); (分数:20.00)_正确答案:()解析:while(*r) *a=*r;a+;r+; 解析 该程序功能是将
16、字符串中的某个子串全部替换成某个子串。从已给定源程序的 main 主函数开始入手,语句“fun(s,t1,t2,w);”调用 fun 函数,通过 while 循环首先比较两个子串是否相同,然后进行替换,实现题目要求。 (1)函数通过 while 语句比较 r 所指字符串是否和 p 所指的子串相同,若有相同,会一直循环到*r 是“/0“才退出循环。所以第二个标识下的“while(r)”改为“while(*r)”。 (2)C 语言中,语句应该以“;”结束,在第二个标识处,“r+”语句没有以“;”结束,因此第二个标识处应该是“*a=*r;a+;r+;”。 考点 函数逻辑;指针操作。三、程序设计题(总
17、题数:1,分数:20.00)3.函数 fun 的功能是:将 s 所指字符串中下标为偶数的字符删除,串中剩余字符形成的新串放在 t 所指数组中。 例如,当 s 所指字符串中的内容为:“ABCDEFGHIJK”,在 t 所指数组中的内容应是:“BDFHJ”。 注意:部分源程序存在文件 FROG1.C 中。 请勿改动主函数 main 和其他函数中的任何内容,仅在函数 fun 的花括号中填入要编写的若干语句。 #includestdio.h #includestring.h void fun(char*s,char t) main() char s100,t100; void NONO(); prin
18、tf(“/nPlease enter string S:“);scanf(“%s“,s); fun(s,t); printf(“/nThe result is:%s/n“,t); NONO(); void NONO() /*本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。*/ char s100,t100; FILE*rf,*wf; int i; rf=fopen(“in.dat“,“r“); wf=fopen(“out.dat“,“w“); for(i=0;i10;i+) fscanf(rf“%s“,s); fun(s,t); fprintf(wf,“%s/n“,t); fclo
19、se(rf); fclose(wf); (分数:20.00)_正确答案:()解析:int i,j=0; for(i=1;istrlen(s);i+=2)if(i%2=0)tj+=si; tj=“/0“; 解析 该程序功能是删除字符串中下标为偶数的字符。解题过程利用 if 判断表达式选择下标为奇数的字符,然后将符合条件的字符放入指定的字符串,这样就实现了函数功能。 (1)判断一个字符串中的某一字符下标 i 是否为偶数,可用“i%2=0”来判断;也可以从下标 0 开始,每次增 2。 (2)对串 s 中各个字符逐个进行考查,下标为奇数的字符就顺序放在新串 t 中,否则就不存放。 (3)所有字符处理完后,记得在新串 t 的末尾加上结束符“/0“”。 考点 数组操作。