1、全国计算机等级考试二级 C 语言机试真题 2010 年 3 月及答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.给定程序中已建立一个带有头结点的单向链表,在 main 函数中将多次调用 fun 函数,每调用一次 fun函数,输出链表尾部结点中的数据,并释放该结点,使链表缩短。注意 部分源程序给出如下。请勿改动主函数 main 和其他函数中的任何内容,仅在函数 fun 的横线上填入所编写的若干表达式或语句。试题源程序#includestdio.h#includestdlib.h#define N 8typedef struct listint
2、 data;struct list *next;SLIST;void fun(SLIST *p)SLIST *t, *s;t=P-next;s=p;while(t-next!=NULL)s=t;/*found*/t=t- (1) ;/*found*/printf(”%d”, (2) );s-next=NULL:/*found*/free( (3) );SLIST *creatlist(int *a)SLIST *h, *p, *q;int i;h=p=(SLIST *)malloc(sizeof(SLIST);for(i=0; iN; i+)q=(SLIST *)malloc(sizeof(S
3、LIST);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(“/nThe list is NULL!/n“);elseprintf(“/nHead“);doprintf(“-%d“, P-data);p=p-next;while(P!=NULL);printf(“-End/n“);main()SLIST *head;int aN=(11, 12, 15, 18, 19, 22, 25, 29);head=creatlist(a);printf(“/
4、nOutput from head:/n“);outlist(head);printf(“/nOutput from tail:/n“);while(head-next!=NULL)fun(head);printf(“/n/n“);printf (“/nOutput from head again:/n“);outlist(head);(分数:30.00)填空项 1:_二、程序修改(总题数:1,分数:30.00)2.给定程序中函数 fun 的功能是:将一个由八进制数字字符组成的字符串转换为与其值相等的十进制整数。规定输入的字符串最多只能包含 5 位八进制数字字符。例如,若输入:77777,则输
5、出将是:32767。请改正程序中的错误,使它能得到正确结果。注意 不要改动 main 函数,不得增行或删行,也不得更改程序的结构。试题源程序#includestdio.h#includestring.h#includestdlib.hint fun(char *p)int n;/*found*/n=*p-o;p+;while(*p!=0)/*found*/n=n*8+*p-o;p+;return n;main()char s6; int i; int n;printf(“Enter a string(Ocatal digits):“);gets(s);if(strlen(s)5)printf(
6、“Error: String too longer!/n/n“);exit(0);for(i=0; si; i+)if(si0|si7)printf(“Error: %c not is ocatal digits!/n/n“, si);exit(0);printf(“The original string:“);puts(s);n=fun(s);printf(“/n%s iS convered to integer number: %d/n/n“, s, n);(分数:30.00)_三、程序设计(总题数:1,分数:40.00)3.请编写函数 fun(),它的功能是:实现两个字符串的连接(不使用
7、库函数 strcat(),即把 p2 所指的字符串连接到 p1 所指的字符串后。例如,分别输入下面两个字符串:FirstString-SecondString则程序输出:FirstString-SecondString注意 部分源程序给出如下。请勿改动主函数 main 和其他函数中的任何内容,仅在函数 fun 的花括号中填入所编写的若干语句。试题源程序#includestdio.h#includeconio.hvoid fun(char p1, char p2)main()char s180, s240;clrscr();printf(“Enter s1 and s2:/n“);scanf(“
8、%s%s“, s1, s2);printf(“s1=%s/n“, s1);printf(“s2=%s/n“, s2);printf(“Invoke fun(s1, s2):/n“);fun(s1, s2);printf(“After invoking:/n“);printf(“%s/n“, s1);(分数:40.00)_全国计算机等级考试二级 C 语言机试真题 2010 年 3 月答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.给定程序中已建立一个带有头结点的单向链表,在 main 函数中将多次调用 fun 函数,每调用一次 fun函数,输
9、出链表尾部结点中的数据,并释放该结点,使链表缩短。注意 部分源程序给出如下。请勿改动主函数 main 和其他函数中的任何内容,仅在函数 fun 的横线上填入所编写的若干表达式或语句。试题源程序#includestdio.h#includestdlib.h#define N 8typedef struct listint data;struct list *next;SLIST;void fun(SLIST *p)SLIST *t, *s;t=P-next;s=p;while(t-next!=NULL)s=t;/*found*/t=t- (1) ;/*found*/printf(”%d”, (2
10、) );s-next=NULL:/*found*/free( (3) );SLIST *creatlist(int *a)SLIST *h, *p, *q;int i;h=p=(SLIST *)malloc(sizeof(SLIST);for(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(“/nThe list is NULL!/n“);elseprintf(
11、“/nHead“);doprintf(“-%d“, P-data);p=p-next;while(P!=NULL);printf(“-End/n“);main()SLIST *head;int aN=(11, 12, 15, 18, 19, 22, 25, 29);head=creatlist(a);printf(“/nOutput from head:/n“);outlist(head);printf(“/nOutput from tail:/n“);while(head-next!=NULL)fun(head);printf(“/n/n“);printf (“/nOutput from h
12、ead again:/n“);outlist(head);(分数:30.00)填空项 1:_ (正确答案:1 next2 t-data3 t)解析:解析 填空 1:要求输出链表尾部的数据,函数利用 while 循环语句找出链表尾部的指针并存入临时变量 s 中,即每循环一次就要判断链表是否已结束位置,如果是,则退出循环,进行输出,由于是通过 t 指针变量进行操作的,因此,都要取 t 的 next 指针重新赋给 t 来实现,所以本处应填 next。填空 2:输出最后一个结点的数据,所以应填 t-data 或(*t).data。填空 3:输出最后一个结点的数据后,并把此结点删除,程序要求释放内存,故
13、应填 t。二、程序修改(总题数:1,分数:30.00)2.给定程序中函数 fun 的功能是:将一个由八进制数字字符组成的字符串转换为与其值相等的十进制整数。规定输入的字符串最多只能包含 5 位八进制数字字符。例如,若输入:77777,则输出将是:32767。请改正程序中的错误,使它能得到正确结果。注意 不要改动 main 函数,不得增行或删行,也不得更改程序的结构。试题源程序#includestdio.h#includestring.h#includestdlib.hint fun(char *p)int n;/*found*/n=*p-o;p+;while(*p!=0)/*found*/n=
14、n*8+*p-o;p+;return n;main()char s6; int i; int n;printf(“Enter a string(Ocatal digits):“);gets(s);if(strlen(s)5)printf(“Error: String too longer!/n/n“);exit(0);for(i=0; si; i+)if(si0|si7)printf(“Error: %c not is ocatal digits!/n/n“, si);exit(0);printf(“The original string:“);puts(s);n=fun(s);printf(
15、“/n%s iS convered to integer number: %d/n/n“, s, n);(分数:30.00)_正确答案:(1)错误:*p正确:*p(2)错误:o;正确:o;)解析:解析 错误 1:函数的形参用的是小写 p,而函数中调用参数时用了大写 p, *p 错写成了*p。错误 2:编译后可知,o错写成了o。三、程序设计(总题数:1,分数:40.00)3.请编写函数 fun(),它的功能是:实现两个字符串的连接(不使用库函数 strcat(),即把 p2 所指的字符串连接到 p1 所指的字符串后。例如,分别输入下面两个字符串:FirstString-SecondString则
16、程序输出:FirstString-SecondString注意 部分源程序给出如下。请勿改动主函数 main 和其他函数中的任何内容,仅在函数 fun 的花括号中填入所编写的若干语句。试题源程序#includestdio.h#includeconio.hvoid fun(char p1, char p2)main()char s180, s240;clrscr();printf(“Enter s1 and s2:/n“);scanf(“%s%s“, s1, s2);printf(“s1=%s/n“, s1);printf(“s2=%s/n“, s2);printf(“Invoke fun(s1, s2):/n“);fun(s1, s2);printf(“After invoking:/n“);printf(“%s/n“, s1);(分数:40.00)_正确答案:(void fun(char p1, char p2)int i=0, n=0;char *p=p1, *q=p2;while(*p)p+;n+;i=n;while(*q)p1i=*q;q+;i+;p1i=/0;)解析:解析 用指针遍历第一个字符串,把指针定位到串尾标志符处;遍历第二个字符串,依次把字符复制到第一个字符串的末尾;最后赋结尾标志符。