1、二级 C 语言-213 及答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.下列给定程序中,函数 fun 的功能是:统计带头结点的单向链表中结点的个数,并存放在形参 n 所指的存储单元中。 请在下划线处填入正确的内容并将下划线删除,使程序得出正确的结果。 注意 :部分源程序给出如下。 不得增行或删行,也不得更改程序的结构! 试题程序: #include stdio.h #include stdlib.h #define N 8 typedef struct list int data; struct list *next; SLIST; SL
2、IST*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; head=creatlist(a); outlist(head); /*found*/ fun( 3, printf(“/nnumber=%d/n“,num); SLIST *creatlist(int
3、 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(“The list is NULL!/n“); else printf(“/nHead“); do printf(“-%d“,p-data); p=p-next; whi
4、le(p!=NULL); prinft(“-End/n“); (分数:30.00)二、程序改错题(总题数:1,分数:30.00)2.下列给定程序中,函数 fun 的功能是:求出 s 所指字符串中最后一次出现的 t 所指字符串的地址,并通过函数值返回,在主函数中输出从此地址开始的字符串;若未找到,则函数值为 NULL。 例如,当字符串中的内容为“abcdabfabcdx”,t 中内容为“ab”时,输出结果应是“abcdx”。 当字符串中的内容为“abcdabfabcdx”,t 中内容为“abd”时,则程序输出未找到信息“Notfound!”。 请改正程序中的错误,使它能得出正确的结果。 注意 :
5、不要改动 main 函数,不得增行或删行,也不得更改程序的结构! 试题程序: #include stdlib.h #include stdio.h #include conio.h #include string.h 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; void main() char s100,t100,*p; syst
6、em(“CLS“); printf(“/nPlease enter string S:“); scanf(“%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“); (分数:30.00)三、程序设计题(总题数:1,分数:40.00)3.函数 fun 的功能是:将 s 所指字符串中除下标为偶数同时 ASCII 码值也为偶数的字符外,其余的全部删除;字符串中剩余字符所形
7、成的新串放在 t 所指的数组中。 例如,若 s 所指字符串中的内容为 “ABCDEFGl23456”,其中字符 A 的 ASCII 码值为奇数,因此应当删除;字符 B 的 ASCII 码值为偶数,但在数组中的下标为奇数,因此也应当删除;字符 2 的 ASCII 码值为偶数,在数组中的下标也为偶数,因此不应当删除,其他依次类推。最后 t 所指的数组中的内容应是“246”。 注意 :部分源程序给出如下。 请勿改动主函数 main 和其他函数中的任何内容,仅在函数 fun 的花括号中填入你编写的若干语句。 试题程序: #include stdlib.h #include conio.h #inclu
8、de stdio.h #include string.h void fun(char*s, char t) void main() char s100,t100; system(“CLS“); printf(“/nPlease enter string S:“); scanf(“%s“,s); fun(s,t); printf(“/nThe result is:%s/n“,t); (分数:40.00)_二级 C 语言-213 答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.下列给定程序中,函数 fun 的功能是:统计带头结点的单向链表中结点
9、的个数,并存放在形参 n 所指的存储单元中。 请在下划线处填入正确的内容并将下划线删除,使程序得出正确的结果。 注意 :部分源程序给出如下。 不得增行或删行,也不得更改程序的结构! 试题程序: #include stdio.h #include stdlib.h #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;
10、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) SLIST *h,*p,*q;int i; h=p=(SLIST *)malloc(sizeof(SLIST); for(i=0;iN;i+) q=(SLIST *)malloc(size
11、of(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!=NULL); prinft(“-End/n“); (分数:30.00)解析:*n next head 解析 填空 1:*n 用来存放结点的个数,对其赋初值为 0。 填空 2:while
12、 循环用于遍历链表,循环一次,指针指向链表的下一个结点。 填空 3:根据函数定义语句确定调用函数的实际参数,即 fun(head, /*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; void main() char s100,t100,*p; system(“CLS“); printf(“/nPlease enter string S:“); scanf(“%s“,s); printf(“/nPlease enter
13、substring t:“); scanf(“%s“,t); p=fun(s,t); if(p) printf(“/nThe result is:%s/n“,p); else printf(“/nNot found!/n“); (分数:30.00)解析:a=NULL; if(*r=*p) 解析 (1)aNull;是个明显的语法错误,指针指向空值的关键字应为NULL。 (2)r 和 p 均为指针变量,分别指向两个字符串中的字符变量,循环条件是当 r 和 p 所指向的字符相同时,进行指针后移操作,故此处应为 if(*r=*p)。三、程序设计题(总题数:1,分数:40.00)3.函数 fun 的功能
14、是:将 s 所指字符串中除下标为偶数同时 ASCII 码值也为偶数的字符外,其余的全部删除;字符串中剩余字符所形成的新串放在 t 所指的数组中。 例如,若 s 所指字符串中的内容为 “ABCDEFGl23456”,其中字符 A 的 ASCII 码值为奇数,因此应当删除;字符 B 的 ASCII 码值为偶数,但在数组中的下标为奇数,因此也应当删除;字符 2 的 ASCII 码值为偶数,在数组中的下标也为偶数,因此不应当删除,其他依次类推。最后 t 所指的数组中的内容应是“246”。 注意 :部分源程序给出如下。 请勿改动主函数 main 和其他函数中的任何内容,仅在函数 fun 的花括号中填入你
15、编写的若干语句。 试题程序: #include stdlib.h #include conio.h #include stdio.h #include string.h void fun(char*s, char t) void main() char s100,t100; system(“CLS“); printf(“/nPlease enter string S:“); scanf(“%s“,s); fun(s,t); printf(“/nThe result is:%s/n“,t); (分数:40.00)_正确答案:()解析:void fun(char *s, char t) int i,j=0; for(i=0;si!=“/0“;i+) if(i%2=0 tj=“/0“; /*在字符串最后加上结束标识*/ 解析 本题要求删除除了下标为偶数同时 ASCII 码值也为偶数的字符,即保留下标为偶数同时 ASCII码值也为偶数的字符。循环语句用于遍历字符串,条件语句用于判断当前字符是否符合要求。注意判断条件是下标为偶数,同时 ASCII 码值也为偶数,所以应使用逻辑与“&“运算符。