1、国家二级 C语言机试(操作题)模拟试卷 152及答案与解析 一、程序填空题 1 给定程序中,函数 fun的功能是:在带有头结点的单向链表中,查找数据域中值为 ch的结点。找到后通过函数值返回该结点在链表中所处的顺序号;若不存在值为 ch的结点,函数返回 0值。 请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。 注意:源程序存放在考生文件夹下的 BLANK1 C中。 不得增行或删行,也不得更改程序的结构 ! #include #include #define N 8 typedef struct list int data; struct list *next; SLIST
2、; SLIST *creatlist(char *); void outliSt(SLIST *); int fun(SLIST *h, char ch) SLIST *P; int n=0; p=h-next; *found* while(P!=_1_) n+; *found* i f(p-data=ch)return_2_; else p=p-next; return 0; main() SLIST *head; int k; char ch; char aN=m, P, g, a, w, x, r, d); head=creatlist(a); outliSt(head); printf
3、(”Enter a letter: “); scanf(“ c“, ch); *found* k=fun(_3_); 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; idata=ai; P-next=q; P=q; P-next=0; return h; void outliSt(SLIST *
4、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“); 二、程序修改题 2 给定程序 MODI1 C中函数 fun的功能是:删除 P所指字符串中的所有空白字符 (包括制表符、回车符及换行符 )。 输入字符串时用 #结束输入。 请改正程序中的错误,使它能输出正确的结果。 注意:不要改动 main函数,不得增行或删行,也不得更改程序的
5、结构 ! #include #include #include 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); main() char c, s8 0; int i=0; printf(“Input a string: “); c=getchar(); while(c!=#) si: c; i+; c=getchar(); si= 0; fun(s); puts(s); 三、程序设计题 3 请编写一个
6、函数 fun,它的功能是:将 ss所指字符串中所:有下标为奇数位置上的字母转换为大写 (若该位置上不是字母,则不转换 )。 例如,若输入 “abe4EFg“,则应输出 “aBc4EFg“。 注意:部分源程序存在文件 PROGl C中。 请勿改动主函数 main和其他函数中的任何内容,仅在函数 fun的花括号中填入你编写的若干语句。 #include #include void fun(char*ss) main() char tt81; void NONO(); printf(“inPlease enter an string within 80 characters: n“); gets(t
7、t); printf(“ininAfter changing, the string n “ s “, tt); fun(tt); printf(“inbecomes n “ si“in“, tt); NONO(); void NONO() *本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 * FILE *fp, *wf; char tt81; int i; fp=fopen(“in dat“, “r“); wf=fopen(“but dat“, “w“); for(i=0; idata=ch)”成立,则已经找到数据域中值为 ch的结点,返回顺序号,即 “return n: ”。
8、 第三空: fun函数的调用形式是: fun(SLIST *h, char ch),第一个参数是 头结点地址,第二个参数是需要寻找的 ch值,因此第三空处应该是 “k=fun(head,ch): ”。 二、程序修改题 2 【正确答案】 (1)for(i=0, t=0; pi; i+) (2)ct= 0; 【试题解析】 函数的功能是删除字符串 s中的所有空白字符。 (1)第一个标识下 “For(i=0, t=0; pi; i+)”语句是 for循环语句,而: For不是C语言的关键字,所示 “For(i=0, t=0; pi; i+)”应改为 “for(i=0, t=0; pi;i+)”。 (2)第二个标识下的 “ct=“ 0“; ”语句是在新生成的串后面加尾符标志,在 C语言中规定使用单引号把一个字符括起来作为字符常量,所以 “ct=“ 0“; ”应改为 “ct= 0; ”。 三、程序设计题 3 【正确答案】 int i; for(i=1; i=a& ssi=z)ssi-=32; 【试题解析】 (1)首先取得字符串中下标为奇数的字符。 (2)判断奇数位置的字符是 否为小写字母。 (3)若 (2)中的判断结果是小写字母,则对其进行转换。