1、计算机二级考试C语言模拟题五套题库6一、填空题给定程序中已建立一个带有头结点的单向链表,在main函数中将多次调用fun 函数,每调用一次fun函数,输出链表尾部结点中的数据,并释放该结点,使链表缩短。请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。注意:源程序存放在考生文件夹下BLANK1.C中。不得增行或删行,也不得更改程序的结构!给定源程序:#include #include #define N 8typedef struct listint data;struct list *next; SLIST;void fun(SLIST *p)SLIST *t, *s;t
2、=p-next; s=p;while(t-next != NULL)s=t;/*found*/t=t-_1_;/*found*/printf( %d ,_2_);s-next=NULL;/*found*/free(_3_);SLIST *creatlist(int *a)SLIST *h,*p,*q; int i;h=p=(SLIST *)malloc(sizeof(SLIST);for(i=0; i q=(SLIST *)malloc(sizeof(SLIST);q-data=ai; p-next=q; p=q;p-next=0;return h;void outlist(SLIST *h)
3、SLIST *p;p=h-next;if (p=NULL) printf(nThe list is NULL!n);elseprintf(nHead);do printf(-%d,p-data); p=p-next; while(p!=NULL);printf(-Endn);main()SLIST *head;int aN=11,12,15,18,19,22,25,29;head=creatlist(a);printf(nOutput from head:n); outlist(head);printf(nOutput from tail: n);while (head-next != NUL
4、L)fun(head);printf(nn);printf(nOutput from head again :n); outlist(head);解题答案:/*第一空*/t=t-next;/*第二空*/printf( %d ,t-data);/*第三空*/free(t);*二、改错题:给定程序MODI1.C中函数fun的功能是:将字符串中的字符按逆序输出,但不改 变字符串中的内容。 例如,若字符串为abcd,则应输出:dcba。请改正程序中的错误,使它能计算出正确的结果。注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!给定源程序:#include /*found*/fun (
5、char a)if (*a)fun(a+1);/*found*/printf(%c *a);main()char s10=abcd;printf(处理前字符串=%sn处理后字符串=, s);fun(s); printf(n);解题答案:/*found*/void fun(char *a)/*found*/printf(%c, *a);*三、程序题:请编写一个函数fun,它的功能是:比较两个字符串的长度,(不得调用C语言 提供的求字符串长度的函数),函数返回较长的字符串。若两个字符串长度相同, 则返回第一个字符串。 例如,输入beijing shanghai (为回车键), 函数将返回shang
6、hai。注意: 部分源程序存在文件PROG1.C中。请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。给定源程序:#include char *fun (char *s, char *t)main()char a20,b20;void NONO ();printf(Input 1th string:);gets(a);printf(Input 2th string:);gets(b);printf(%sn,fun (a, b);NONO ();void NONO ()/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */FILE *f
7、p, *wf ;int i ;char a20, b20 ;fp = fopen(in.dat,r);wf = fopen(out.dat,w);for(i = 0 ; i next;if (p!=NULL)q=p-next;while(q!=NULL)if (p-data=q-data)p-next=q-next;/*found*/free(_1_);/*found*/q=p-_2_;elsep=q;/*found*/q=q-_3_;SLIST *creatlist(int *a)SLIST *h,*p,*q; int i;h=p=(SLIST *)malloc(sizeof(SLIST);
8、for(i=0; 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(nThe list is NULL!n);elseprintf(nHead);do printf(-%d,p-data); p=p-next; while(p!=NULL);printf(-Endn);main()SLIST *head; int aN=1,2,2,3,4,4,4,5;head=creatli
9、st(a);printf(nThe list before deleting :n); outlist(head);fun(head);printf(nThe list after deleting :n); outlist(head);解题答案:/*found*/free(q);/*found*/q=p-next;/*found*/q=q-next;*二、改错题:给定程序MODI1.C中函数fun的功能是:用选择法对数组中的n个元素按从小到大的顺序进行排序。 请修改程序中的错误,使它能得出正确的结果。注意:不要改动main函数,不得增行和删行,也不得更改程序的结构!给定源程序:#includ
10、e #define N 20void fun(int a, int n)int i, j, t, p;for (j = 0 ;j n-1 ;j+) /*found*/p = jfor (i = j;i n; i+)if(ai ap)/*found*/p = j;t = ap ; ap = aj ; aj = t;main()int aN=9,6,8,3,-1,i, m = 5;printf(排序前的数据:);for(i = 0;i m;i+) printf(%d ,ai); printf(n);fun(a,m);printf(排序后的数据:);for(i = 0;i m;i+) printf(
11、%d ,ai); printf(n);解题答案:/*found*/p=j;/*found*/p=i;*三、程序题:请编写一个函数fun,它的功能是:求出1到m之间(含m)能被7或11整除的所有整数放在数组a中,通过n返回这些数的个数。例如,若传送给m的值为50,则程序输出: 7 11 14 21 22 28 33 35 42 44 49注意: 部分源程序存在文件PROG1.C中。请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。给定源程序:#include #define M 100void fun (int m, int *a , int *n)ma
12、in()int aaM, n, k;void NONO ();fun (50, aa, &n);for (k = 0; k n; k+)if(k+1)%20=0) printf(n);else printf(%4d, aak);printf(n);NONO();void NONO ()/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */FILE *fp, *wf ;int i, n, j, k, aaM, sum ;fp = fopen(in.dat,r);wf = fopen(out.dat,w);for(i = 0 ; i 10 ; i+) fscanf(fp, %d,
13、 &j);fun(j, aa, &n);sum = 0 ;for(k = 0 ; k n ; k+) sum+=aak ;fprintf(wf, %dn, sum);fclose(fp);fclose(wf);参考答案:int i ;*n=0 ;for(i=7 ; inext;/*found*/while(p!=_1_)n+;/*found*/if (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);ou
14、tlist(head);printf(Enter a letter:);scanf(%c,&ch);/*found*/k=fun(_3_);if (k=0) printf(nNot found!n);else printf(The sequence number is : %dn,k);SLIST *creatlist(char *a)SLIST *h,*p,*q; int i;h=p=(SLIST *)malloc(sizeof(SLIST);for(i=0; i q=(SLIST *)malloc(sizeof(SLIST);q-data=ai; p-next=q; p=q;p-next=
15、0;return h;void outlist(SLIST *h)SLIST *p;p=h-next;if (p=NULL) printf(nThe list is NULL!n);elseprintf(nHead);doprintf(-%c,p-data); p=p-next;while(p!=NULL);printf(-Endn);解题答案:/*第一空*/while(p!=0)/*第二空*/if (p-data=ch) return n;/*第三空*/k=fun(head,ch);*二、改错题:给定程序MODI1.C中函数fun的功能是:删除p所指字符串中的所有空白字符(包括制表符、回车符
16、及换行符)。输入字符串时用#结束输入。 请改正程序中的错误,使它能输出正确的结果。注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!给定源程序:#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,s80;int i=0;printf(Input a string:);c=getchar();while(c!=#)si=
17、c;i+;c=getchar();si=0;fun(s);puts(s);解题答案:/*found*/for(i=0,t=0; pi; i+)/*found*/ct=0;*三、程序题:请编写一个函数fun,它的功能是:将ss所指字符串中所有下标为奇数位置上的字母转换为大写(若该位置上不是字母,则不转换)。 例如, 若输入abc4EFg,则应输出aBc4EFg。注意: 部分源程序存在文件PROG1.C中。请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。给定源程序:#include #include void fun (char *ss)main()ch
18、ar tt81 ;void NONO ();printf(nPlease enter an string within 80 characters:n); gets(tt);printf(nnAfter changing, the stringn %s, tt);fun(tt);printf(nbecomesn %sn, tt);NONO ();void NONO ()/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */FILE *fp, *wf ;char tt81 ;int i ;fp = fopen(in.dat,r);wf = fopen(out.dat,w);for(i = 0 ; i 10 ; i+) fscanf(fp, %s, tt);fun(tt);fprintf(wf, %sn, tt);fclose(fp);fclose(wf);参考答案:int i ;for(i = 1 ; i = a & ssi next;while(p
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1