1、二级 C 语言分类模拟题 256 及答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.给定程序中,函数 fun 的功能是:在带有头结点的单向链表中,查找数据域中值为 ch 的结点。找到后通过函数值返回该结点在链表中所处的顺序号;若不存在值为 ch 的结点,函数返回 0 值。 请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。 注意:源程序存放在 下的 BLANK1.C 中。 不得增行或删行,也不得更改程序的结构! #includestdio.h #includestdlib.h #define N 8 typedef st
2、ruct list int data; struct list *next; SLIST; SLIST *creatlist(char *); void outlist(SLIST *); int fun(SLIST *h, char ch) SLIST *p; int n=0; p=h-next; /*found*/ while(p!=_) n+; /*found*/ if(p-data=ch)return_; else p=p-next; return 0; main() SLIST *head; int k; char ch; char a N=“m“, “p“, “g“, “a“, “
3、w“, “x“, “r“, “d“; head=creatlist(a); outlist(head); printf(“Enter a letter:“); scanf(“%c“, /*found*/ k=fun(_); if(k=0)printf(“/nNot found!/n“); else printf(“The sequence number is : %d/n“, k); SLIST *creatlist(char *a) SLIST *h, *p, *q; int i; h=p=(SLIST*)malloc(sizeof(SLIST); for(i=0; iN; i+) q=(S
4、LIST*)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“); else printf(“/nHead“); do printf(“-%c“, p-data); p=p-next; while(p!=NULL); printf(“-End/n“); (分数:30.00)_二、程序修改题(总题数:1,分数:30.00)2.给定程序 MODI1.
5、C 中函数 fun 的功能是:删除 p 所指字符串中的所有空白字符(包括制表符、回车符及换行符)。 输入字符串时用“#“结束输入。 请改正程序中的错误,使它能输出正确的结果。 注意:不要改动 main 函数,不得增行或删行,也不得更改程序的结构! #includestring.h #includestdio.h #includectype.h fun(char *p) int i, t; char c80; /*found*/ For(i=0, t=0; pi; i+) if(!isspace(*(p+i) ct+=pi; /*found*/ ct=“/0“; strcpy(p, c); ma
6、in() char c, s80; int i=0; printf(“Input a string:“); c=getchar(); while(c!=“#“) si=c; i+; c=getchar(); si=“/0“; fun(s); puts(s); (分数:30.00)_三、程序设计题(总题数:1,分数:40.00)3.请编写一个函数 fun,它的功能是:将 ss 所指字符串中所有下标为奇数位置上的字母转换为大写(若该位置上不是字母,则不转换)。 例如,若输入“abc4EFg“,则应输出“aBc4EFg“。 注意:部分源程序存在文件 PROG1.C 中。 请勿改动主函数 main 和
7、其他函数中的任何内容,仅在函数 fun 的花括号中填入你编写的若干语句。 #includestdio.h #includestring.h void fun(char *ss) main() char tt81; void NONO(); printf(“/nPlease enter an string within 80 characters:/n“); gets(tt); printf(“/n/nAfter changing, the string/n“%s“ “, tt); fun(tt); printf(“/nbecomes/n“%s“/n“, tt); NONO(); VOid NO
8、NO() /*本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */ FILE *fp, *wf; char tt81; int i; fp=fopen(“in.dat“, “r“); wf=fopen(“out.dat“, “w“); for(i=0; i10; i+) fscanf(fp, “%s“, tt); fun(tt); fprintf(wf, “%s/n“, tt); fclose(fp); fclose(wf); (分数:40.00)_二级 C 语言分类模拟题 256 答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)
9、1.给定程序中,函数 fun 的功能是:在带有头结点的单向链表中,查找数据域中值为 ch 的结点。找到后通过函数值返回该结点在链表中所处的顺序号;若不存在值为 ch 的结点,函数返回 0 值。 请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。 注意:源程序存放在 下的 BLANK1.C 中。 不得增行或删行,也不得更改程序的结构! #includestdio.h #includestdlib.h #define N 8 typedef struct list int data; struct list *next; SLIST; SLIST *creatlist(char
10、 *); void outlist(SLIST *); int fun(SLIST *h, char ch) SLIST *p; int n=0; p=h-next; /*found*/ while(p!=_) n+; /*found*/ if(p-data=ch)return_; else p=p-next; return 0; main() SLIST *head; int k; char ch; char a N=“m“, “p“, “g“, “a“, “w“, “x“, “r“, “d“; head=creatlist(a); outlist(head); printf(“Enter
11、a letter:“); scanf(“%c“, /*found*/ k=fun(_); if(k=0)printf(“/nNot found!/n“); else printf(“The sequence number is : %d/n“, k); SLIST *creatlist(char *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; ret
12、urn h; void outlist(SLIST *h) SLIST *p; p=h-next; if(p=NULL)printf(“/nThe list is NULL!/n“); else printf(“/nHead“); do printf(“-%c“, p-data); p=p-next; while(p!=NULL); printf(“-End/n“); (分数:30.00)_正确答案:()解析:(1)0 或 NULL (2)n (3)head, ch 答案考生文件夹 解析 函数 fun 的功能是查找数据域中值为 ch 的结点。 第一空:这里是补充循环结束条件,链表是单向有限的,
13、最后一个结点的指针域指向 NULL,因此可以用这个来作为循环结束条件,在 p 指向的结点的指针域不是 NULL 时表明还有下一个结点,因此第一空处应该是“while(p!=NULL)”。 第二空:在循环当中 n 用来记录顺序号,如果“if(p-data=ch)”成立,则已经找到数据域中值为 ch的结点,返回顺序号,即“return n; ”。 第三空:fun 函数的调用形式是:fun(SLIST *h, char ch),第一个参数是头结点地址,第二个参数是需要寻找的 ch 值,因此第三空处应该是“k=fun(head, ch); ”。 考点 单链表、函数的定义以及调用。二、程序修改题(总题数
14、:1,分数:30.00)2.给定程序 MODI1.C 中函数 fun 的功能是:删除 p 所指字符串中的所有空白字符(包括制表符、回车符及换行符)。 输入字符串时用“#“结束输入。 请改正程序中的错误,使它能输出正确的结果。 注意:不要改动 main 函数,不得增行或删行,也不得更改程序的结构! #includestring.h #includestdio.h #includectype.h fun(char *p) int i, t; char c80; /*found*/ For(i=0, t=0; pi; i+) if(!isspace(*(p+i) ct+=pi; /*found*/
15、ct=“/0“; strcpy(p, c); main() char c, s80; int i=0; printf(“Input a string:“); c=getchar(); while(c!=“#“) si=c; i+; c=getchar(); si=“/0“; fun(s); puts(s); (分数:30.00)_正确答案:()解析:(1)for(i=0, t=0; pi; i+) (2)ct=“/0“; 答案考生文件夹 解析 函数的功能是删除字符串 s 中的所有空白字符。 (1)第一个标识下“For(i=0, t=0; pi; i+)”语句是 for 循环语句,而 For 不
16、是 C 语言的关键字,所示“For(i=0, t=0; pi; i+)”应改为“for(i=0, t=0; pi; i+)”。 (2)第二个标识下的“ct=“/0“; ”语句是在新生成的串后面加尾符标志,在 C 语言中规定使用单引号把一个字符括起来作为字符常量,所以“ct=“/0“; ”应改为“ct=“/0“; ”。 考点 字符数组和字符串。三、程序设计题(总题数:1,分数:40.00)3.请编写一个函数 fun,它的功能是:将 ss 所指字符串中所有下标为奇数位置上的字母转换为大写(若该位置上不是字母,则不转换)。 例如,若输入“abc4EFg“,则应输出“aBc4EFg“。 注意:部分源程
17、序存在文件 PROG1.C 中。 请勿改动主函数 main 和其他函数中的任何内容,仅在函数 fun 的花括号中填入你编写的若干语句。 #includestdio.h #includestring.h void fun(char *ss) main() char tt81; void NONO(); printf(“/nPlease enter an string within 80 characters:/n“); gets(tt); printf(“/n/nAfter changing, the string/n“%s“ “, tt); fun(tt); printf(“/nbecomes
18、/n“%s“/n“, tt); NONO(); VOid NONO() /*本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */ FILE *fp, *wf; char tt81; int i; fp=fopen(“in.dat“, “r“); wf=fopen(“out.dat“, “w“); for(i=0; i10; i+) fscanf(fp, “%s“, tt); fun(tt); fprintf(wf, “%s/n“, tt); fclose(fp); fclose(wf); (分数:40.00)_正确答案:()解析:int i; for(i=1; istrlen(ss); i+=2) if(ssi=“a“ 答案考生文件夹 解析 (1)首先取得字符串中下标为奇数的字符。 (2)判断奇数位置的字符是否为小写字母。 (3)若(2)中的判断结果是小写字母,则对其进行转换。 考点 字符数组和字符串、数据类型和常量、变量。