1、二级 C 语言-403 及答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.给定程序中,函数 fun 的功能是将带头结点的单向链表逆置,即若原链表中从头至尾结点数据域依次为2、4、6、8、10,逆置后,从头至尾结点数据域依次为 10、8、6、4、2。 请在程序的下画线处填入正确的内容并把下画线删除,使程序得出正确的结果。 注意:部分源程序给出如下。 不得增行或删行,也不得更改程序的结构! 试题程序: #includestdio.h #includestdlib.h #define N 5 typedef struct node int dat
2、a; struct node*next; NODE; void fun(NODE*h) NODE*p, *q, *r; /*found*/ p=h- 1; /*found*/ if(p= 2)return; q=p-next; p-next=NULL; while(q) r=q-next;q-next=p; /*found*/ p=q;q= 3; h-next=p; NODE*creatlist(int a) NODE*h,*p,*q;int i; h=(NODE*)malloc(sizeof(NODE); h-next=NULL; for(i=0;iN;i+) q=(NODE*)malloc
3、(sizeof(NODE); q-data=ai; q-next=NULL; if(h-next=NULL) h-next=p=q; elsep-next=q;p=q; return h; void outlist(NODE*h) NODE *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(“-End/n“); main() NODE*head; int aN=2,4,6,
4、8,10; head=creatlist(a); printf(“/nThe original list:/n“); outlist(head); fun(head); printf(“/nThe list after invetting:/n“); outlist(head); (分数:30.00)二、程序修改题(总题数:1,分数:30.00)2.下列给定程序中,函数 fun 的功能是:计算 s 所指字符串中含有 t 所指字符串的数目,并作为函数值返回。 请改正程序中的错误或在下画线处填上正确的内容并把下画线删除,使它能得出正确的结果。 注意:不要改动 main 函数,不得增行或删行,也不得
5、更改程序的结构! 试题程序: #includestdlib.h #includeconio.h #includestring.h #includestdio.h #define N 80 int fun(char*s,char*t) int n; char *p,*r; n=0; /*found*/ *r=t; while(*s) p=s; while(*r) if(*r=*p) r+; p+; else break; if(*r=“/0“) n+; /*found*/ 1; s+; return n; void main() char aN,bN;int m; printf(“/nPleas
6、e enter string a:“); gets(a); printf(“/nPlease enter substring b:“); gets(b); m=fun(a,b); printf(“/nThe result is:m=%d/n“,m); (分数:30.00)三、程序设计题(总题数:1,分数:40.00)3.请编写函数 fun,其功能是:将放在字符串数组中的 M 个字符串(每串的长度不超过 N),按顺序合并组成一个新的字符串。 例如,若字符串数组中的 M 个字符串为“AAAA“,“BBBBBBB“,“CC“,则合并后的字符串内容应该是“AAAABBBBBBBCC”。 注意:部分源程
7、序给出如下。 请勿改动主函数 main 和其他函数中的任何内容,仅在函数 fun 的花括号中填入你编写的若干语句。 试题程序: #includestdio.h #includeconio.h #define M 3 #define N 20 void fun(char aMN,char *b) void main() char wMN=“AAAA“,“BBBBBBB“,“CC“; char a100=“#“; int i; printf(“The string:/n“); for(i=0;iM;i+) puts(wi); printf(“/n“); fun(w,a); printf(“The
8、A string:/n“); printf(“%s“,a); printf(“/n/n“); (分数:40.00)_二级 C 语言-403 答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.给定程序中,函数 fun 的功能是将带头结点的单向链表逆置,即若原链表中从头至尾结点数据域依次为2、4、6、8、10,逆置后,从头至尾结点数据域依次为 10、8、6、4、2。 请在程序的下画线处填入正确的内容并把下画线删除,使程序得出正确的结果。 注意:部分源程序给出如下。 不得增行或删行,也不得更改程序的结构! 试题程序: #includestdio.h
9、 #includestdlib.h #define N 5 typedef struct node int data; struct node*next; NODE; void fun(NODE*h) NODE*p, *q, *r; /*found*/ p=h- 1; /*found*/ if(p= 2)return; q=p-next; p-next=NULL; while(q) r=q-next;q-next=p; /*found*/ p=q;q= 3; h-next=p; NODE*creatlist(int a) NODE*h,*p,*q;int i; h=(NODE*)malloc(
10、sizeof(NODE); h-next=NULL; for(i=0;iN;i+) q=(NODE*)malloc(sizeof(NODE); q-data=ai; q-next=NULL; if(h-next=NULL) h-next=p=q; elsep-next=q;p=q; return h; void outlist(NODE*h) NODE *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!
11、=NULL); printf(“-End/n“); main() NODE*head; int aN=2,4,6,8,10; head=creatlist(a); printf(“/nThe original list:/n“); outlist(head); fun(head); printf(“/nThe list after invetting:/n“); outlist(head); (分数:30.00)解析:(1)next (2)NULL (3)r 解析 填空 1:本空考查了为 p 赋初值,根据题目的要求是将带头结点的单向链表逆置可知,p 的初值应该为 h-next。 填空 2:if
12、 判断语句表明当 p 等于什么时就要返回,因此只能当 p 等于 NULL 时返回,不用做后面的链表的逆置了。 填空 3:把 q 的指针向后移动,才能实现将带头结点的单向链表逆置。因此本空填写 r。二、程序修改题(总题数:1,分数:30.00)2.下列给定程序中,函数 fun 的功能是:计算 s 所指字符串中含有 t 所指字符串的数目,并作为函数值返回。 请改正程序中的错误或在下画线处填上正确的内容并把下画线删除,使它能得出正确的结果。 注意:不要改动 main 函数,不得增行或删行,也不得更改程序的结构! 试题程序: #includestdlib.h #includeconio.h #incl
13、udestring.h #includestdio.h #define N 80 int fun(char*s,char*t) int n; char *p,*r; n=0; /*found*/ *r=t; while(*s) p=s; while(*r) if(*r=*p) r+; p+; else break; if(*r=“/0“) n+; /*found*/ 1; s+; return n; void main() char aN,bN;int m; printf(“/nPlease enter string a:“); gets(a); printf(“/nPlease enter
14、substring b:“); gets(b); m=fun(a,b); printf(“/nThe result is:m=%d/n“,m); (分数:30.00)解析:(1)r=t; (2)r=t;或 r= 解析 从字符串 s 中找出子字符串的方法是:从第一个字符开始,对字符串进行遍历,若 s 串的当前字符等于 t串的第一个字符,两字符串的指针自动加 1,继续比较下一个字符;若比较至字符串 t 的末尾,则跳出循环;若 s 串的字符与 t 串的字符不对应相同,则继续对 s 串的下一个字符进行处理。三、程序设计题(总题数:1,分数:40.00)3.请编写函数 fun,其功能是:将放在字符串数组
15、中的 M 个字符串(每串的长度不超过 N),按顺序合并组成一个新的字符串。 例如,若字符串数组中的 M 个字符串为“AAAA“,“BBBBBBB“,“CC“,则合并后的字符串内容应该是“AAAABBBBBBBCC”。 注意:部分源程序给出如下。 请勿改动主函数 main 和其他函数中的任何内容,仅在函数 fun 的花括号中填入你编写的若干语句。 试题程序: #includestdio.h #includeconio.h #define M 3 #define N 20 void fun(char aMN,char *b) void main() char wMN=“AAAA“,“BBBBBBB
16、“,“CC“; char a100=“#“; int i; printf(“The string:/n“); for(i=0;iM;i+) puts(wi); printf(“/n“); fun(w,a); printf(“The A string:/n“); printf(“%s“,a); printf(“/n/n“); (分数:40.00)_正确答案:()解析:void fun(char aMN,char*b) int i,j,k=0; for(i=0;iM;i+) /*将字符串数组中的 M 个字符串,按顺序存入一个新的字符串*/ for(j=0;aij!=“/0“;j+) bk+=aij; bk=“/0“; /*在字符串最后加上字符串结束标记符*/ 解析 本题考查:字符串连接操作。本程序中第 1 个 for 循环的作用是对二维数组行的控制,第 2 个循环的作用是从同一行中取出字符并存放到一维数组 b 中,语句是 bk+=aij;。