1、全国计算机等级考试二级 C 语言机试真题 2006 年 9 月及答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.给定程序中,函数 fun 的功能是:将形参 std 所指结构体数组中年龄最大者的数据作为函数值返回,并在 main 函数中输出。请在程序的下画线处填入正确的内容,并把下画线删除,使程序得出正确的结果。注意 部分源程序给出如下。请勿改动主函数 main 和其他函数中的任何内容,仅在 fun 函数的横线上填入所编写的若干表达式或语句。试题源程序#includestdio.htypedef structchar name10;int a
2、ge;)STD;STD fun(STD std, int n)STD max; int i;/*found*/max= (1) ;for(i=1; in; i+)/*found*/if(max.age (2) )max=stdi;return max;main()STD std5=“aaa“, 17, “bbb“, 16, “ccc“, 18, “ddd“, 17, “eee“, 15;STD max;max=fun(std, 5);printf(“/nThe result: /n“);/*found*/printf(“/nName: %s, Age: %d/n“, (3) , max.age
3、);(分数:30.00)填空项 1:_二、程序修改(总题数:1,分数:30.00)2.下列给定程序是建立一个带头结点的单向链表,并用随机函数为各结点赋值。函数 fun()的功能是:将单向链表结点(不包括头结点)数据域为偶数的值累加起来,并作为函数值返回。其累加和通过函数值返回 main()函数。例如,若 n=5,则应输出 8.391667。请改正程序中的错误,使它能得到正确结果。注意 不要改动 main 函数,不得增行或删行,也不得更改程序的结构。试题源程序#includestdio.h#includestdiib.htypedef struct aaint data;struct aa *n
4、ext;NODE;int fun(NODE *h)int sum=0;NODE *P;/*found*/p=h;while(P-next)if(p-data%2=0)sum+=p-data;/*found*/p=h-next;return sum;NODE *creatlink(int n)NODE *h, *p, *s, *q;int i, x;h=p=(NODE *)malloc(si zeof(NODE);for(i=1; i=n; i+)s=(NODE *)malloc(sizeof(NODE);s-data=rand()%16;s-next=p-next;p-next=s;p=p-n
5、ext;p-next=NULL;return h;outlink(NODE *h, FILE *Pf)NODE *p;p=h-next;fprintf(Pf, “/n/nTHE LIST:/n/n HEAD“);while(P)fprintf(Pf, “-%d“, p-data); p=p-next;fprintf(pf, “/n“);outresult(int s, FILE *pf)fprintf(Pf, “/nThe sum of even numbers : %d/n“, s);main()NODE *head; int even;head=creatlink(12);head-dat
6、a=9000;outlink(head, stdout);even=fun(head);printf(“/nThe result :/n“); outresult(even, stdout);(分数:30.00)_三、程序设计(总题数:1,分数:40.00)3.请编写函数 fun(),它的功能是:将带头结点单向链表按 data 域由大到小排序(排序时不考虑头结点),主函数用随机函数为各节点 data 域赋值,头结点 data 域赋值为 0。注意 部分源程序给出如下。请勿改动主函数 main 和其他函数中的任何内容,仅在函数 fun 的花括号中填入所编写的若干语句。试题源程序#include s
7、tdio.h#includeconi0.hstruct asint data;struct aa *next;void fun(struct aa *p)main()int i, n, m=100;struct aa *h=NULL, *s=NULL, *p=NULL;clrscr()s=(struct aa*) malloc(siZeof(struct aa);h=s; h-data=0; h-next=NULL;printf(“Please input n:“);scanf(“%d“, n);for(i=1; i=n; i+)p=(struct aa*) malloc(sizeof(str
8、uct aa);p-data=rand()%m; P-next=NULL;printf(“%d“, P-data);s-next=p; s=s-next;fun(h);printf(“/n“);for(h=h-next; h!=NULL; h=h-nnext)printf(“%d“, h-data);(分数:40.00)_全国计算机等级考试二级 C 语言机试真题 2006 年 9 月答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.给定程序中,函数 fun 的功能是:将形参 std 所指结构体数组中年龄最大者的数据作为函数值返回,并在 mai
9、n 函数中输出。请在程序的下画线处填入正确的内容,并把下画线删除,使程序得出正确的结果。注意 部分源程序给出如下。请勿改动主函数 main 和其他函数中的任何内容,仅在 fun 函数的横线上填入所编写的若干表达式或语句。试题源程序#includestdio.htypedef structchar name10;int age;)STD;STD fun(STD std, int n)STD max; int i;/*found*/max= (1) ;for(i=1; in; i+)/*found*/if(max.age (2) )max=stdi;return max;main()STD std
10、5=“aaa“, 17, “bbb“, 16, “ccc“, 18, “ddd“, 17, “eee“, 15;STD max;max=fun(std, 5);printf(“/nThe result: /n“);/*found*/printf(“/nName: %s, Age: %d/n“, (3) , max.age);(分数:30.00)填空项 1:_ (正确答案:1 *std2 stdi.age3 max.name)解析:解析 本题是从结构体中找出年龄最大的记录。填空 1:给存放最大者 max 赋初值,所以应填:*std。填空 2:当前最大者的年龄和结构中所有的年龄进行比较,所以应填:
11、stdi.age。填空 3:输出最大者的姓名和年龄,所以应填:max.name。二、程序修改(总题数:1,分数:30.00)2.下列给定程序是建立一个带头结点的单向链表,并用随机函数为各结点赋值。函数 fun()的功能是:将单向链表结点(不包括头结点)数据域为偶数的值累加起来,并作为函数值返回。其累加和通过函数值返回 main()函数。例如,若 n=5,则应输出 8.391667。请改正程序中的错误,使它能得到正确结果。注意 不要改动 main 函数,不得增行或删行,也不得更改程序的结构。试题源程序#includestdio.h#includestdiib.htypedef struct aa
12、int data;struct aa *next;NODE;int fun(NODE *h)int sum=0;NODE *P;/*found*/p=h;while(P-next)if(p-data%2=0)sum+=p-data;/*found*/p=h-next;return sum;NODE *creatlink(int n)NODE *h, *p, *s, *q;int i, x;h=p=(NODE *)malloc(si zeof(NODE);for(i=1; i=n; i+)s=(NODE *)malloc(sizeof(NODE);s-data=rand()%16;s-next=
13、p-next;p-next=s;p=p-next;p-next=NULL;return h;outlink(NODE *h, FILE *Pf)NODE *p;p=h-next;fprintf(Pf, “/n/nTHE LIST:/n/n HEAD“);while(P)fprintf(Pf, “-%d“, p-data); p=p-next;fprintf(pf, “/n“);outresult(int s, FILE *pf)fprintf(Pf, “/nThe sum of even numbers : %d/n“, s);main()NODE *head; int even;head=c
14、reatlink(12);head-data=9000;outlink(head, stdout);even=fun(head);printf(“/nThe result :/n“); outresult(even, stdout);(分数:30.00)_正确答案:(1)错误:while(p-next)正确:while(p!=NULL)或 while(p)(2)错误:p=h-next;正确:p=p-next;)解析:解析 错误 1:循环时要注意循环中止的条件是 p!=NULL(最后一个结点为空,就像字符串的最后一个字符为结尾标志符/0一样),while(p-next)使循环提前结束,所以应改为
15、 while(p!=NULL)或 while(p)。错误 2:当累加完后,应该把指针指向下一个结点的指针赋给 p,而不是把头指针指向的下一个结点赋给p,否则还会造成死循环,所以 p=h-next;应该改为 p=p-next;。三、程序设计(总题数:1,分数:40.00)3.请编写函数 fun(),它的功能是:将带头结点单向链表按 data 域由大到小排序(排序时不考虑头结点),主函数用随机函数为各节点 data 域赋值,头结点 data 域赋值为 0。注意 部分源程序给出如下。请勿改动主函数 main 和其他函数中的任何内容,仅在函数 fun 的花括号中填入所编写的若干语句。试题源程序#inc
16、lude stdio.h#includeconi0.hstruct asint data;struct aa *next;void fun(struct aa *p)main()int i, n, m=100;struct aa *h=NULL, *s=NULL, *p=NULL;clrscr()s=(struct aa*) malloc(siZeof(struct aa);h=s; h-data=0; h-next=NULL;printf(“Please input n:“);scanf(“%d“, n);for(i=1; i=n; i+)p=(struct aa*) malloc(size
17、of(struct aa);p-data=rand()%m; P-next=NULL;printf(“%d“, P-data);s-next=p; s=s-next;fun(h);printf(“/n“);for(h=h-next; h!=NULL; h=h-nnext)printf(“%d“, h-data);(分数:40.00)_正确答案:(void fun(struct as *p)int temp;struct as *1st;for(p=p-next; p-next!=NULL; p=p-next)for(1st=p-next; 1st!=NULL; 1st=1st-next)if(1st-datap-data)temp=1st-data;1st-data=p-data;p-data=temp;)解析:解析 本题考查单链表的操作。为了交换,需定义中间变量,本题可以采用选择排序法进行排序。基本方法和对数组进行排序相似,只不过是通过指针的移动实现对比较次数的控制和每个结点的访问,注意循环结束的控制条件。