二级c语言上机考试一百题及答案.docx
全国计算机考试二级C语言上机考试一百题及答案第一部分填空题注意:源程序存放在考生文件夹下的BLANKLC中。1 .不得增行或删行,也不得更改程序的结构!2 .请在程序下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。1 .给定程序中,函数FUN的功能是:计算出带有头接点的单向链表中各结点数据域之和作为 函数值返回。#include <stdio. h>include <stdlib. h>#define N 8 typedef struct list int data;struct list *next; SLIST;SLIST *creatlist(int *);void outlist(SLIST *); int fun( SLIST *h) SLIST *p; int s=0;p=h->next; while(p) (s+= p->1;datap=p->2;next) 一 return s;main() SLIST *head;int aN = 12,87, 45, 32, 91,16, 20, 48);head二creatlist(a);outlist(head);printf(*nsum=%dn*, fun(3);headSLIST *creatlist(int a) SLIST *h,*p,*q;int i;h=p=(SLIST *)malloc(sizeof(SLIST);for(i=0; i<N; 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(XnHead ");do printf ("一>%d”, p->data); p=p->next; while(p!=NULL);printf("->Endn");2 .给定程序中,函数FUN的功能是:求出形参SS所指字符串数组中最长字符串的长度,其余 字符串左边用字符*补齐,使其与最长的字符串等长。字符串数组中工有M个字符串,且串 长<N。include <stdio.h>include <string. h>#defineM5ttdefineN20void fun(char (*ss)N) int i, j, k=0, n, m, len;for(i=0; i<M; i+) len=strlen(ssi);if(i=0) n=len;if(len>n) n=len; k=1;ifor(i=0; i<M; i+) if (i!=k) m=n;len=strlen(ssi);for(j=2; j>=0; j)lenssi m-=ssi j;for(j=0; j<n-len; j+)ssij=3;'*'main()char sMN = "shanghai", "guangzhou", "beijing", "tianjing", "cchongqing" int i;printf("nThe original strings are :n");for (i=0; i<M; i+) printfssi);printf('n");fun(ss);printf("nThe result:n");for (i=0; i<M; i+) printf("%sn", ssi);3 .人员记录由编号和出生年,月,日组成,N名人员的数据已在主函数中存入结构体数组std 且编号唯一。函数fun的功能是;找出指定编号人员的数据,作为函数值返回,有主函数输 出,若指定编号不存在,返回数据中的编号为空串。#include<stdio.h>itinclude<string. h>ttdefine N 8 typedef struct char num10;int year, month, day ;STU;1fun (STU *std, char *num)STU int i;STU a=",9999,99,99;for (i=0; i<N; i+)if ( strcmp (2, num) =0 ) stdi. numreturn (3);stdireturn a; main()STU stdN = "111111”, 1984,2,15, “222222”, 1983,9,21, “333333,1984,9,1,"444444”, 1983, 7,15, “555555,1984, 9, 28, “666666”, 1983,11,15,"777777,1983, 6, 22, “888888”, 1984, 8,19;STU p;char n10="666666”;p=fun(std, n);if (p. num0=0)printf(*nNot found !n");elseprintf(XnSucceed !n ");printf ("%s%d-%d-%dn/z, p. num, p. year, p. month, p. day);) 4 .给定程序中已建立一个带有头结点的单向链表,链表中的各结点按数据域递增有序链接。 函数fun的功能:删除链表中数据域值相同的结点,使之保留一个。include<stdio. h>4include<stdlib. h>define N 8typedef struct list int data;struct list *next; SLIST;void fun( SLIST *h) SLIST *p, *q;p=h->next;if (p!=NULL) q=p->next;while(q!=NULL) if (p->data=q->data) p->next=q->next;free (1);qq=p->2;next else p=q;q=q->3;next)SLIST *creatlist(int *a) SLIST *h, *p, *q; int i;h=p= (SLIST *)malloc(sizeof(SLIST);for(i=0; i<N; i+) q=(SLIST *)malloc(sizeof(SLIST); q->data=ai; p->next=q; p=q;)p->next=O;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 ('一(!”, p->data); p=p->next; while (p!=NULL);printf('->Endn");main() SLIST *head; int aN = l, 2, 2, 3, 4, 4, 4, 5; head=creatlist(a);printf("nThe list before deleting :n"); outlist(head);fun(head);printf("nThe list after deleting :n"); outlist(head);5 .函数fun的功能是进行数字字符转换。若形参ch中是数字字符F9',贝0'转换成'9', T'转换成'8', '2'转换成'7''9'转换成'0'若是其他字符则保持不变;并将转换后的结果作为函数值返回. ttinclude <stdio. h>1fun(char ch)char (if (ch>='0' &&-2)ch<='9'return ' 9' - (ch-3);'O'return ch ; main() char cl, c2;printf('nThe result :n");cl=,2' ; c2 = fun (cl);printf("cl=%cc2=%cn”, cl, c2);cl=,8' ; c2 = fun (cl);printf("cl=%cc2=%cn”, cl, c2);cl二'a ; c2 = fun (cl);printf("cl=%cc2=%cn”, cl, c2);6 .给定程序中,函数fun的功能是:对形参ss所指字符串数组中的M字符串按长度由短到长 进行排序。SS所指字符串数组中共有M个字符串,且串长3 tfinclude<stdio. h>include<string. h>#defineM5ttdefineN20void fun(char (*ss)N) int i, j, k, nM; char t N;for(i=0; i<M; i+) ni=strlen(ssi);for(i=0; i<M-l; i+) k=i;for(j=1; j<M; j+)i+1if (nk >nj) k=_2_;jif (k!=i) strcpy(t, ssi); strcpy(ssi, ssk);strcpy(ssk,3);tnk=ni; ) main() char sMN="shanghai", "guangzhou", "beijing", "tianjing", "cchongqing"int i;printf(*nThe original strings are :n"); for(i=0; i<M; i+) printf("%sn”, ssi); printf('n");fun(ss);printf(XnThe result :n");for(i=0; i<M; i+) printf(sn”, ssi);7 .函数FUN的功能是:从三个形参a, b,c中找出中间的哪个数,作为函数值返回。例如,当 a=3, b=5, c=4 时,中数为 4。#include <stdio. h>int fun(int a, int b, int c) int t;t = (a>b) ? (b>c? b : (a>c?c:1) : (a>c)?2 : (b>c)?c:3);return t;1. a 2. a 3. bmain() int al=3, a2=5, a3=4, r;r = fun (al, a2, a3);printf(nThe middle number is : %dn”, r);8 .给定程序中,函数FUN的功能是:统计出带有头结点的单向链表中接点的个数,存放在形参 N所指的存储单元中。ttinclude<stdio. h>4include<stdlib. h>tfdefine N 8typedef struct list int data;struct list *next; SLIST;SLIST *creatlist(int *a);void outlist(SLIST *);void fun( SLIST *h, int *n) SLIST *p;*n=1;0p=h->next;while(p) (*n)+;p=2;p->nextmain() SLIST *head;int aN = 12, 87, 45, 32, 91,16, 20, 48, num;head=creatlist(a); outlist(head);fun (head,3);&numprintf ("rmumber=%dn”, num);SLIST *creatlist(int a) SLIST *h, *p, *q;int i;h=p=(SLIST *)malloc(sizeof(SLIST);for(i=0; i<N; 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(XnHead ");do printf(*->%d, p->data); p=p->next; while(p!=NULL);printf("->Endn");9 .给定程序的功能是:调用函数FUN将指定源文件中的内容复制到指定的目标文件中,复制 成功时函数返回值为1,失败时返回值为0o在复制的过程中,把复制的内容输出到终端屏 幕。主函数中源文件名房子变量sfname中,目标文件名放在变量tfname中。ttinclude<stdio. h>include<stdlib. h>int fun(char *source, char *target) FILE *fs, *ft; char ch;if(fs=fopen(source,1)=NULL)"r"return 0;if (ft=fopen (target, "w") =NULL)return 0;printf(*nThe data in file :n");ch=fgetc(fs);while(!feof (2)fs putchar( ch );fputc (ch,3);ftch=fgetc(fs);)fclose(fs); fclose(ft);printf('nn");return 1;)main() char sfname20="myfilel”, tfname20="myfile2”;FILE *myf; int i; char c;myf=fopen(sfname,,zw/z);printf(*nThe original data :n");for (i=l; i<30; i+) c='A'+rand( )%25;fprintf (myf, "%c”, c); printf c); fclose(myf);printf('nn");if (fun(sfname, tfname) printf("Succeed!");else printf("Fail!");10 .给定程序中,函数FUN的功能是:在形参S所指字符串中的每个数字字符之后插入一个 *o例如,形参S所指字符串为:def35adh3kjsdf7.执行结果为:def3*5*adh3*kjsdf7* include <stdio. h> void fun(char *s) int i, j, n;for(i=0; si!='0' i+)if(si>='0' _1 si<='9')&& n=0;while(si+l+n!=_2_) n+;0for(j=i+n+l; j>i; j-)sj+l= 3;sji=i+l;)main() char s 80="ba3a54cd23a”;printf(nThe original string is : %sn',s);fun(s);printf("nThe result is : %sn”, s);11 .给定程序中,函数FUN的功能是:将形参STD所指结构体数组中年龄最大者的数据作为函 数值返回,并将main函数输出。#include <stdio. h> typedef struct char name10;int age;STD;STD fun(STD std, int n) STD max;int i;max二 1;stdOfor(i=l; i<n; i+)if (max. age<2) max=stdi;stdi. agereturn max;)main() STD std5 = "aaa", 17, "bbb”, 16, "ccc", 18, "ddd”, 17, "eee”, 15 ;STD max;max=fun(std, 5);printf("nThe result: n");printf ("nName : %s, Age : %dn”,3 , max. age);max. name12 .给定程序中,函数fun功能是:在带有头结点的单向链表中,查找数据域中为CH的结点, 找到后通过函数值返回该结点在链表中所处的顺序号;若不存在值为CH的结点,函数返 回。值。 include<stdio.h>#include<stdlib. h>ttdefine N 8 typedef struct 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;while(p!=_1_)NULLn+;if (p->data=ch) return 2;nelse p=p->next;return 0;)main() SLIST *head; int k; char ch;char aLNJ = ( m , p , g , a , w , x , r , d );head=creatlist(a);outlist(head);printf("Enter a letter:*); scanf&ch);k=fun (3);head, chif (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<N; 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*);else printf("nHead"); do printf ('一>%c”, p->data); p=p->next; while(p!=NULL);printf("->Endn");)13 .给定程序的功能是:从键盘输入若干行文本(每行不超过80个字符),写到文件 myfile4. txt中,用-1作为字符串输入结素的标志。然后将文件的内容读出显示在屏幕 上。文件的读写分别由自定义函数read Text和write Text实现.ttinclude <stdio. h>itinclude <string. h>#include <stdlib. h>void WriteText(FILE *);void ReadText(FILE *);main() FILE *fp;if (fp=fopen(myfile4. txt", "w")=NULL) printf(" open fail!n”); exit(0); WriteText(fp);fclose(fp);if(fp=fopen(*myfile4. txt", "r")=NULL) printf(" open fail!n*); exit (0); ReadText(fp);fclose(fp);void WriteText(FILE 1 )*fw char str81;printf("nEnter string with -1 to end :n*);gets (str);while(strcmp(str, *-l*)!=0) fputs(2, fw); fputsfw);strgets(str);)void ReadText(FILE *fr) char str81;printf("nRead file and output to screen :n");fgets(str, 81, fr);while( !feof(fr) ) printf 3);strfgets(str, 81, fr);)14 .给定程序中,函数FUN的功能是:有N X N矩阵,根据给定的m(m=N)值,将每个元素中的 值均右移m个位置,左边置为Oo例如,N=3, m=2,有下列矩阵123001456程序执行结果为004789007ttinclude <stdio. h>ttdefine N 4void fun(int (*t)N, int m) int i, j;for(i=0; i<N; _1_ )i+ for(j=N-l-m; j>=0: j)ti j+2 =ti j;mfor(j=0; j<3; j+)mti j=0;)main() int t N=21,12,13,24,25,16,47, 38,29,11, 32, 54,42,21,33,10, i, j, m;printf("nThe original array:n*);for(i=0; i<N; i+) for(j=0; j<N; j+)printf("%2d ",tij);printf("n");)printf(*Input m (m<=%d): ", N);scanf&m);fun(t, m):printf(*nThe result is:n");for(i=0; i<N; i+) for(j=0; j<N; j+)printf(*%2d *,tij);printf(*n*);15 .函数FUN的功能是;把形参a所指数组中的最大值放杂a0中,接着求出a所指数组中 的最小值防在al中;再把a所指数组元素中的次大值a2中,把a所指数组的数据最初 排列为:1,4, 2, 3,9, 6, 5, 8, 7,则按规则移动后,数据排列为:9,1,8, 2, 7, 3, 6, 4, 5.形参n 中存放a所指数组中数据的个数。 include <stdio. h> define N9void fun(int 1 , int n)*a int i, j, max, min, px, pn, t;for (i=0; i<n-l; i+=2)2 max = min = ai;px = pn = i;for (j=_3; j<n; j+)i if (max < aj) max = aj; px = j; if (min > aj) min = aj; pn = j; if (px != i) t = ai; ai = max; apx = t;if (pn = i) pn= px;if (pn != i+1) t = ai+l; ai+l = min; apn = t; )main() int bN = l,4, 2, 3,9,6, 5, 8, 7), i:printf(*nThe original data :n*);for (i=0; i<N; i+) printf("%4d ", bi);printf('n");fun(b, N);printf("nThe data after moving :n");for (i=0; i<N; i+) printf C%4d ”, bi);printf('n");16 .给定程序中,函数FUN的功能是;求SS所指字符串数组中长度最厂的字符串所在的行下 标,作为函数值返回,并把其串长防在形参n所指变量中。SS所指字符串数组中共有M个 字符串,且串长< N.ttinclude <stdio. h>define M 5define N 20int fun(char (*ss) 1, int *n)N int i, k=0, len=0;for(i=0; i<M; i+) len=strlen(ssi);if (i=0) *n=2;lenif(len>*n) *n=3;1 enk=i;)return(k);.main() char ssMN="shanghai", "guangzhou", "beijing", "tianjing", "cchongqing"int n, k, i;printf("nThe original strings are :n");for(i=0;i<M;i+)puts(ssi);k=fun(ss, &n);printf (z,nThe length of longest string is : %dn”, n);printf("nThe longest string is : %sn”,ssk);17 .函数FUN的功能是:逆置数组元素中的值。例如:若a所指数组中的数据依次 为:1,2, 3, 4,5, 6, 7,8, 9,则逆置后依次为:9,8, 7。6, 5, 4, 3,2,1。形参n给出数组中数据 的个数。 #include <stdio. h> void fun(int a, int n) int i, t;for (i=0; i<1; i+)n/2t=ai;ai = 2 an-l-i;an-l-i= 3;t)main() int b9 = 1,2, 3, 4, 5,6, 7, 8, 9, i; printf(*nThe original data :n*); for (i=0; i<9; i+)printf(*%4d ”, bi);printf("n");fun(b, 9);printf(*nThe data after invert :n");for (i=0; i<9; i+)printf(*%4d ”, bi);printf(*n*);18 .给定程序中,函数FUN的功能是:在形参S所指字符串中寻找与参数C相同的字符,并在 其后插入一个与之相同的字符,若找不到相同的字符则函数不做任何处理。例如,S所指字符串:baacda,c中的字符为:a,执行后S所指字符串为:baaaacdaa include <stdio. h> void fun(char *s, char c) int i, j, n;/*found*/for(i=0; si !=1; i+)' 0'if(si=c) (/*found*/n=2;0while(si+l+n!=' 0') n+; for(j=i+n+l: j>i; j) sj+l=sj;/*found*/sj+l=s3 ;ii=i+l;)main() char s80=*baacda*, c;printf(*nThe string: %sn*, s);printf("nlnput a character: "); scanf&c);fun(s, c);printf(*nThe result is: %sn”, s);19 .函数FUN的功能是:把形参a所指数组总的奇数原顺序依次存放到a0,al,a2. 中,把偶数从数组中删除,奇数个数通过函数值返回。例如:若a所指数组中的数据最初 排列为:9、1、4、2、3、6、5、8、7,删除偶数后,a所指数组中数据为:9、1、3、5、 7,返回值为5»include <stdio. h>ttdefine N 9int fun(int a, int n) int i, j;j = 0;for (i=0; i<n; i+)if (ai%2=1)1(aj = ai; 2;j+return 3;j)main() int bN = 9,1,4, 2, 3, 6, 5, 8, 7), i, n;printf("nThe original data :n");for (i=0; i<N; i+) printf("%4d ", bi);printf("n");n = fun (b, N);printf("nThe number of odd : %d n', n);printf('nThe odd number :n");for (i=0; i<n; i+) printf("%4d ", bi);printf ('n");20 .给定程序的功能是:调用函数FUN将指定源文件中的内容复制到指定的目标文件中,复制 成功时函数返回值为0。在复制的过程中,把复制的内容输出到终端屏幕。主函数中源文件 名防在变量sfname中,目标文件名放在变量tfname中。#include<stdio. h>#include<stdlib. h>int fun(char *source, char *target) FILE *fs, *ft;char ch;if(fs=fopen(source, 1)=NULL)"r"return 0;if (ft=fopen (target, "w") =NULL)return 0;printf("nThe data in file :n");ch=fgetc(fs);while(!feof (2)fs putchar( ch );fputc (ch,3);ftch=fgetc(fs);fclose(fs); fclose(ft);printf('nn");return 1;.main() char sfname 20tfname20="niyfile2”;FILE *myf; int i; char c;myf=fopen(sfname, "w");printf('nThe original data :n");for (i=l; i<30; i+) c='A'+rand()%25; f printf (myf, "%c”, c); printf c); fclose(myf);printf('nn");if (fun(sfname, tfname) printf("Succeed!");else printf("Fail!");21 .函数FUN的功能是:计算直到土 <10«.若x=2.5,函数值为:12.182494. ntlinclude<stdio. h>#include<math. h>double fun(double x) double f, t; int n;f = 1. 0+1;xt = X; n = 1;do n+;t *= x/2;nf += 1;t while (fabs(t) >= le-6); return f;)main() double x, y;x=2. 5;y = fun(x);printf("nThe result is :n");printf (x=%-12. 6f y=%-12. 6f n”, x, y);22 .给定程序中,函数FUN的功能是:统计形参S所指字符串中数字字符出现的次数,并存放 在形参t所指的变量中最后在函数中输出。例如,形参所指的字符串为:abcdef35adgh3kjsdf7.输出结果为:4#include <stdio. h>void fun(char *s, int *t) int i, n;n=0;for(i=0; 1 !=NULL; i+)siif (s i >=' 0, &&s i <=2) n