1、全国计算机二级 C 语言上机试题 36+2015 年及答案解析(总分:30.00,做题时间:90 分钟)1.给定程序中,函数 fun 的功能是:统计出带有头结点的单向链表中结点的个数,存放在形参 n 所指的存储单元中。 请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。 注意:源程序存放在考生文件夹下的 BLANK1.C 中。不得增行或删行,也不得更改程序的结构! 给定源程序: #include #include #define N 8 typedef struct list int data; struct list *next; SLIST; SLIST *creatl
2、ist(int *a); void outlist(SLIST *); void fun( SLIST *h, int *n) SLIST *p; /*found*/ _1_=0; p=h-next; while(p) (*n)+; /*found*/ p=p-_2_; main() SLIST *head; int aN=12,87,45,32,91,16,20,48, num; head=creatlist(a); outlist(head); /*found*/ fun(_3_, printf(“/nnumber=%d/n“,num); SLIST *creatlist(int a) S
3、LIST *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(“The list is NULL!/n“); else printf(“/nHead “); do printf(“-%d“,p-data); p=p-next; while(p!
4、=NULL); printf(“-End/n“); (分数:10.00)_2.给定程序 MODI1.C 中函数 fun 的功能是:求出 s 所指字符串中最后一次出现的 t 所指子字符串的地址,通过函数值返回,在主函数中输出从此地址开始的字符串;若未找到,则函数值为 NULL。 例如,当字符串中的内容为:“abcdabfabcdx“,t 中的内容为:“ab“时, 输出结果应是:abcdx。 当字符串中的内容为:“abcdabfabcdx“,t 中的内容为:“abd“时,则程序输出未找到信息:not be found!。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动 main 函数,
5、不得增行或删行,也不得更改程序的结构! 给定源程序: #include #include char * fun (char *s, char *t ) char *p , *r, *a; /*found*/ a = Null; while ( *s ) p = s; r = t; while ( *r ) /*found*/ if ( r = p ) r+; p+; else break; if ( *r = /0 ) a = s; s+; return a ; main() char s100, t100, *p; printf(“/nPlease enter string S :“); s
6、canf(“%s“, s ); printf(“/nPlease enter substring t :“); scanf(“%s“, t ); p = fun( s, t ); if ( p ) printf(“/nThe result is : %s/n“, p); else printf(“/nNot found !/n“ ); (分数:10.00)_3.函数 fun 的功能是: 将 s 所指字符串中除了下标为偶数、同时 ASCII 值也为偶数的字符外,其余的全都删除;串中剩余字符所形成的一个新串放在 t 所指的数组中。 例如,若 s 所指字符串中的内容为:“ABCDEFG123456“
7、,其中字符 A 的 ASCII 码值为奇数,因此应当删除;其中字符 B 的 ASCII 码值为偶数,但在数组中的下标为奇数,因此也应当删除;而字符 2 的 ASCII 码值为偶数,所在数组中的下标也为偶数,因此不应当删除,其它依此类推。最后 t 所指的数组中的内容应是:“246“。 注意: 部分源程序存在文件PROG1.C 中。请勿改动主函数 main 和其它函数中的任何内容,仅在函数 fun 的花括号中填入你编写的若干语句。 给定源程序: #include #include void fun(char *s, char t) main() char s100, t100; printf(“/
8、nPlease enter string S:“); scanf(“%s“, s); fun(s, t); printf(“/nThe result is: %s/n“, t); NONO(); (分数:10.00)_全国计算机二级 C 语言上机试题 36+2015 年答案解析(总分:30.00,做题时间:90 分钟)1.给定程序中,函数 fun 的功能是:统计出带有头结点的单向链表中结点的个数,存放在形参 n 所指的存储单元中。 请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。 注意:源程序存放在考生文件夹下的 BLANK1.C 中。不得增行或删行,也不得更改程序的结构
9、! 给定源程序: #include #include #define N 8 typedef struct list int data; struct list *next; SLIST; SLIST *creatlist(int *a); void outlist(SLIST *); void fun( SLIST *h, int *n) SLIST *p; /*found*/ _1_=0; p=h-next; while(p) (*n)+; /*found*/ p=p-_2_; main() SLIST *head; int aN=12,87,45,32,91,16,20,48, num;
10、 head=creatlist(a); outlist(head); /*found*/ fun(_3_, printf(“/nnumber=%d/n“,num); 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
11、 (p=NULL) printf(“The list is NULL!/n“); else printf(“/nHead “); do printf(“-%d“,p-data); p=p-next; while(p!=NULL); printf(“-End/n“); (分数:10.00)_正确答案:(第一处:对 n 所指的存储单元进行初始化,所以应填:*n。 第二处:指向 p 的下一个结点,所以应填:next。 第三处:函数调用,在主函数中已经给出了 head,所以应填:head。)解析:2.给定程序 MODI1.C 中函数 fun 的功能是:求出 s 所指字符串中最后一次出现的 t 所指子字
12、符串的地址,通过函数值返回,在主函数中输出从此地址开始的字符串;若未找到,则函数值为 NULL。 例如,当字符串中的内容为:“abcdabfabcdx“,t 中的内容为:“ab“时, 输出结果应是:abcdx。 当字符串中的内容为:“abcdabfabcdx“,t 中的内容为:“abd“时,则程序输出未找到信息:not be found!。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动 main 函数,不得增行或删行,也不得更改程序的结构! 给定源程序: #include #include char * fun (char *s, char *t ) char *p , *r,
13、*a; /*found*/ a = Null; while ( *s ) p = s; r = t; while ( *r ) /*found*/ if ( r = p ) r+; p+; else break; if ( *r = /0 ) a = s; s+; return a ; main() char s100, t100, *p; printf(“/nPlease enter string S :“); scanf(“%s“, s ); printf(“/nPlease enter substring t :“); scanf(“%s“, t ); p = fun( s, t );
14、if ( p ) printf(“/nThe result is : %s/n“, p); else printf(“/nNot found !/n“ ); (分数:10.00)_正确答案:(第一处:指向空指针错误,Null 应 NULL。 第二处:比较指针位置的值是否相等,所以应改为:if(*r=*p)。)解析:3.函数 fun 的功能是: 将 s 所指字符串中除了下标为偶数、同时 ASCII 值也为偶数的字符外,其余的全都删除;串中剩余字符所形成的一个新串放在 t 所指的数组中。 例如,若 s 所指字符串中的内容为:“ABCDEFG123456“,其中字符 A 的 ASCII 码值为奇数,
15、因此应当删除;其中字符 B 的 ASCII 码值为偶数,但在数组中的下标为奇数,因此也应当删除;而字符 2 的 ASCII 码值为偶数,所在数组中的下标也为偶数,因此不应当删除,其它依此类推。最后 t 所指的数组中的内容应是:“246“。 注意: 部分源程序存在文件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 = 0 ; i strlen(s) ; i += 2) if(si % 2 = 0) tj+ = si ; tj = 0 ; )解析: