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