1、全国计算机二级 C语言上机试题 54+2015年及答案解析(总分:30.00,做题时间:90 分钟)1.给定程序中,函数 fun的功能是:计算出带有头结点的单向链表中各结点数据域中值之和作为函数值返回。 请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。 注意:源程序存放在考生文件夹下的 BLANK1.C中。不得增行或删行,也不得更改程序的结构! 给定源程序: #include #include #define N 8 typedef struct list int data; struct list *next; SLIST; SLIST *creatlist(int *
2、); void outlist(SLIST *); int 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;
3、int i; h=p=(SLIST *)malloc(sizeof(SLIST); for(i=0; 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(“-%d“, p-data); p=p-next; while(p!=NULL); printf
4、(“-End/n“); (分数:10.00)_2.给定程序 MODI1.C中函数 fun的功能是:将 s所指字符串中出现的与 t1所指字符串相同的子串全部替换成 t2所指字符串,所形成的新串放在 w所指的数组中。在此处,要求 t1和 t2所指字符串的长度相同。 例如,当 s所指字符串中的内容为:“abcdabfab“,t1 所指子串中的内容为: “ab“,t2 所指子串中的内容为:“99“时, 结果在 w所指的数组中的内容应为: “99cd99f99“。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动 main函数,不得增行或删行,也不得更改程序的结构! 给定源程序: #inclu
5、de #include int fun (char *s, char *t1, char *t2 , char *w) int i; 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() char s100,
6、t1100, t2100, w100; printf(“/nPlease enter string S:“); scanf(“%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(
7、t1) != strlen(t2)/n“); (分数:10.00)_3.函数 fun的功能是:将 s所指字符串中下标为偶数的字符删除,串中剩余字符形成的新串放在 t所指数组中。 例如,当 s所指字符串中的内容为:“ABCDEFGHIJK“,在 t所指数组中的内容应是:“BDFHJ“。 注意: 部分源程序存在文件 PROG1.C中。请勿改动主函数 main和其它函数中的任何内容,仅在函数 fun的花括号中填入你编写的若干语句。 给定源程序: #include #include void fun(char *s, char t) main() char s100, t100; printf(“/n
8、Please enter string S:“); scanf(“%s“, s); fun(s, t); printf(“/nThe result is: %s/n“, t); NONO(); (分数:10.00)_全国计算机二级 C语言上机试题 54+2015年答案解析(总分:30.00,做题时间:90 分钟)1.给定程序中,函数 fun的功能是:计算出带有头结点的单向链表中各结点数据域中值之和作为函数值返回。 请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。 注意:源程序存放在考生文件夹下的 BLANK1.C中。不得增行或删行,也不得更改程序的结构! 给定源程序: #
9、include #include #define N 8 typedef struct list int data; struct list *next; SLIST; SLIST *creatlist(int *); void outlist(SLIST *); int 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=cre
10、atlist(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; 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
11、(“The list is NULL!/n“); else printf(“/nHead “); do printf(“-%d“, p-data); p=p-next; while(p!=NULL); printf(“-End/n“); (分数:10.00)_正确答案:(第一处:累加数据域中的值,所以应填:data。 第二处:指定 p的下一个指针,所以应填:next。 第三处:函数调用,在主函数中已经给出了 head,所以应填:head。)解析:2.给定程序 MODI1.C中函数 fun的功能是:将 s所指字符串中出现的与 t1所指字符串相同的子串全部替换成 t2所指字符串,所形成的新串放在
12、w所指的数组中。在此处,要求 t1和 t2所指字符串的长度相同。 例如,当 s所指字符串中的内容为:“abcdabfab“,t1 所指子串中的内容为: “ab“,t2 所指子串中的内容为:“99“时, 结果在 w所指的数组中的内容应为: “99cd99f99“。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动 main函数,不得增行或删行,也不得更改程序的结构! 给定源程序: #include #include int fun (char *s, char *t1, char *t2 , char *w) int i; char *p , *r, *a; strcpy( w, s
13、); 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() char s100, t1100, t2100, w100; printf(“/nPlease enter string S:“); scanf(“%s“, s); printf(“/nPlease enter sub
14、string 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“); (分数:10.00)_正确答案:(第一处:判断字符串当前字符是否是字符串结束符,所以应改为:while(*r)。 第二处:语句后缺少分号。)解析:3.
15、函数 fun的功能是:将 s所指字符串中下标为偶数的字符删除,串中剩余字符形成的新串放在 t所指数组中。 例如,当 s所指字符串中的内容为:“ABCDEFGHIJK“,在 t所指数组中的内容应是:“BDFHJ“。 注意: 部分源程序存在文件 PROG1.C中。请勿改动主函数 main和其它函数中的任何内容,仅在函数 fun的花括号中填入你编写的若干语句。 给定源程序: #include #include void fun(char *s, char t) main() char s100, t100; printf(“/nPlease enter string S:“); scanf(“%s“, s); fun(s, t); printf(“/nThe result is: %s/n“, t); NONO(); (分数:10.00)_正确答案:(void fun(char *s, char t) int i, j = 0 ; for(i = 1 ; i strlen(s); i+=2) tj+ = si ; tj = 0 ; )解析: