《全国计算机二级C语言上机100题.docx》由会员分享,可在线阅读,更多相关《全国计算机二级C语言上机100题.docx(199页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、上机题库第一部分填空题*勘勘*注意:源程序存放在考生文件夹下的BLANK1. C中。1 .不得增行或删行,也不得更改程序的结构!2 .请在程序下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。Il蠹蠹,康维的功能是:计算出带有头接点的单向链表中各结点数据域之和作为 函数值返回。#include#include Sdefine typedefN struct8list int data;struct list SLIST;*next;SLIST *creatlist(int *); void outlist(SLIST *); int fun( SLIST *h) SLIST *p;
2、p=h-next; while (p)int s=0;s+= p-1dataP=p-一2nextreturn s;main() SLIST *head;int aN = 12, 87, 45, 32, 91, 16, 20, 48;head=creatlist(a); outlist(head);printf(nsum=%drT, fun(3);)headSLIST *creatlist(int 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;
3、return h;void outlist(SLIST *h) SLIST *p;p=h-next;if (p=NULL) printfCThe list is NULL!n); else printf(XnHead );do printf (一%d, p-data); p=p-next; while(p!=NULL);printf(一Endn);2 .给定程序中,函数FUN的功能是:求出形参SS所指字符串数组中最长字符串的长度,其余 字符串左边用字符*补齐,使其与最长的字符串等长。字符串数组中工有M个字符串,且串 长此 #include #include#defineM5#defineN20
4、void fun(char (*ss)N) int i,j,k=0,n, m, len;for(i=0; in) n=len; k=1;ifor(i=0; i=0; j)lenssi m-=ssi j;for(j=0; jn-len; j+)ssi j=3; *)main()char sMN=shanghai, guangzhou, beijing, tianjing, cchongqing;int i;printf(nThe original strings are :n);for (i=0; iM; i+) printf(%sn”, ssi);printf(n);fun(ss);print
5、f(nThe result:n);for (i=0; iM; i+) printf (%sn, ssi);3 .人员记录由编号和出生年,月,日组成,N名人员的数据已在主函数中存入结构体数组std 且编号唯一。函数fun的功能是;找出指定编号人员的数据,作为函数值返回,有主函数输 出,若指定编号不存在,返回数据中的编号为空串。#include#includedefine N 8 typedef struct char num10;int year, month, day ;)STU;1fun (STU *std, char *num)STU int i;STU a=“,9999, 99, 99;
6、for (i=0; iN; i+)if ( strcmp (2, num) =0 ) stdi. numreturn (3);stdireturn a; main()STU stdN = rilllir, 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
7、 (p. num0=0)printf(*nNot found !n);elseprintf(nSucceed !n );printf (%s%d-%d-%dnz,, p. num, p. year, p. month, p. day);) 4 .给定程序中已建立一个带有头结点的单向链表,链表中的各结点按数据域递增有序链接。 函数fun的功能:删除链表中数据域值相同的结点,使之保留一个。#include#includedefine N 8 typedef struct list int data;struct list *next; SLIST;void fun( SLIST *h) SLIST
8、 *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;nextSLIST *creatlist(int *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 *h) SLIST *p;p=h-
9、next;if (p=NULL) printf(*nThe list is NULL!n); else printf(nHead);do printfp-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); outlis
10、t(head);5 .函数fun的功能是进行数字字符转换。若形参ch中是数字字符F9,贝0转换成9,T转换成8, 2转换成79转换成O;若是其他字符则保持不变;并将转换后 的结果作为函数值返回.#include 1fun (char ch)charif (ch= Q9 &-2)ch=9return 9 - (ch-3);Oreturn 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=%cc
11、2=%cn”, cl, c2);cl=,a ; c2 = fun(cl);printf(cl=%cc2=%cn”, cl, c2);6 .给定程序中,函数fun的功能是:对形参ss所指字符串数组中的M字符串按长度由短到长 进行排序。SS所指字符串数组中共有M个字符串,且串长3#includeinclude#defineM5ttdefineN20void fun(char (*ss)N) int i, j, k, nM; char t N;for(i=0; iM; i+) ni=strlen(ssi);for(i=0; iM-l; i+) k=i;for(j=1; jnj) k=2;jif (k
12、!=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; iM; i+) printf(%sn”, ssi);printf(n);fun(ss);printf(nThe result :n);for(i=0; iM; i+) printf(%sn”, ssi);7 .函数FUN的功能是:从三个
13、形参a, b,c中找出中间的哪个数,作为函数值返回。例如,当 a=3, b=5, c=4 时,中数为 4。ttinclude int fun (int a, int b, int c) int t;t = (ab) ? (bc? b : (ac?c:1) : (ac)?2 : (bc)?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的功能是:统计出带有头结点的单向链表中接点的
14、个数,存放在形参 N所指的存储单元中。#include#include#define 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=creatlis
15、t(a); outlist(head);fun (head,3);&numprintf (/znnumber=%dn/z, num);SLIST *creatlist(int 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 *h) SLIST *p;p=h-next;if (p=NULL) printf(The list is NULL!n);else printf(*XnHead
16、);do printf(一%d”, p-data); p=p-next; while(p!=NULL);printf(-Endn); 9 .给定程序的功能是:调用函数FUN将指定源文件中的内容复制到指定的目标文件中,复制 成功时函数返回值为1,失败时返回值为0。在复制的过程中,把复制的内容输出到终端屏 幕。主函数中源文件名房子变量sfname中,目标文件名放在变量tfname中。#includeincludeint fun(char *source, char *target) FILE *fs, *ft; char ch;if(fs=fopen(source,1)=NULL)rreturn
17、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=niyfile2;FILE *myf; int i; char c;myf=fopen(sfname, w);printf(nThe origin
18、al data :n);for(i=l; i30; i+) c=A+rand( )%25;fprintf (myf, %c”, c); printf C%c*, 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 void fun(char *s) int i
19、, j, n;for(i=0; si!=0; i+)if(si= 0,1 sii; j)sj+l= 3;sji=i+l;main() char s80=ba3a54cd23a”;printf(nThe original string is : %sn”,s);fun(s);printf(nThe result is : %sn”, s);11 .给定程序中,函数FUN的功能是:将形参STD所指结构体数组中年龄最大者的数据作为函 数值返回,并将main函数输出。#include typedef struct char name10;int age;STD;STD fun(STD std, int
20、 n) STD max;int i;max=1;stdOfor(i=l; in; i+)if(max. age2) max=stdi;stdi. agereturn max;main() STD std5 = faaa, 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. name)12 .给定程序中,函数fun功能是:在带有头结点的单向链表中,查找数据域中为CH的
21、结点, 找到后通过函数值返回该结点在链表中所处的顺序号;若不存在值为CH的结点,函数返 回。值。include#include#define N 8typedef 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;)
22、main() SLIST *head;int k; char ch;char aN = ,m,,,p,g,,,a,,,w,,,x,,,r,,,d,;head=creatlist (a);outlist(head);printf(Enter a letter:);scanf (%c”,&ch);k=fun (3);head, chif (k=0)printf(nNot found!n);elseprintf(The sequence number is : %dn”, k);SLIST *creatlist(char *a) SLIST *h,*p,*q;int i;h=p=(SLIST *)m
23、alloc(sizeof(SLIST); for(i=0; idata=ai; p-next=q; p=q;p-next=O; return h;)void outlist(SLIST *h) SLIST *p; p=h-next; if (p=NULL) printfCnThe 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作
24、为字符串输入结素的标志。然后将文件的内容读出显示在屏幕 上。文件的读写分别由自定义函数read Text和write Text实现.#include #include Sinclude void WriteText(FILE *);void ReadText(FILE *);main() FILE *fp;if(fp=fopen(myfile4. txt, w)=NULL) printf (,z open fail!n”; exit (0); WriteText(fp);fclose(fp);if (fp=fopen (*myfile4. txt, r)=NULL) printf ( open
25、 fail!n); exit(0); ReadText(fp);fclose(fp);)void WriteText (FILE 1 )*fw char str81;printf(XnEnter string with -1 to end :n);gets(str);while(strcmp(str, T) !=0) fputs(2, fw); fputs(n, fw);strgets (str);)void ReadText(FILE *fr) char str81;printf(nRead file and output to screen :n);fgets(str, 81, fr);w
26、hile( !feof(fr) ) printf 3);strfgets(str, 81, fr);)14 .给定程序中,函数FUN的功能是:有N X N矩阵,根据给定的m(m=N)值,将每个元素中的 值均右移m个位置,左边置为Oo例如,N=3, m=2,有下列矩阵123001456程序执行结果为004789007#include #define N 4void fun(int (*t) N, int m) int i, j;for(i=0; i=0; j-)ti j+2 =ti j;mfor(j=0; j3; j+)mmain() int t N = 21,12, 13, 24, 25,16
27、, 47, 38, 29,11, 32, 54, 42, 21, 33, 10, i, j, m;printf(nThe original array:n);for (i=0; iN; i+) for(j=0; jN; j+)printf C%2d ”, ti j);printf Cn);) printf(Input m (m=%d): , N);scanf&m);fun(t, m);printf(*nThe result is:n*);for(i=0; iN; i+) for(j=0; jN: j+)printf(*%2d *,tij);printf(n);) )15 .函数FUN的功能是;
28、把形参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 #define N9void fun(int 1 , int n)*a int i, j, max, min, px, pn, t;for (i=0; in-l; i+=2)2 max = min = ai;px = pn = i;for (j=_3_; jn; j+)i i
29、f (max 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; iN; i+) printf(*%4d ”, bi);printf(n);fun(b, N);printf(*nThe data after moving :n);fo
30、r (i=0; iN; i+) printf(*%4d ”, bi);printf(n);16 .给定程序中,函数FUN的功能是;求SS所指字符串数组中长度最厂的字符串所在的行下 标,作为函数值返回,并把其串长防在形参n所指变量中。SS所指字符串数组中共有M个 字符串,且串长 N.#include #define M 5#define N 20int fun(char (*ss) 1, int *n)N int i, k=0, len=0;for(i=0; i*n) *n=3;1 enk=i;return(k);)main() char ssMN = shanghai, guangzhou,
31、beijing, tianjing, cchongqing; int n, k, i;printf(nThe original strings are :n);for(i=0;iM;i+)puts(ssi);k=fun(ss, &n);printf(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
32、,3,2, K形参n给出数组中数据 的个数。 #include void fun(int a, int n) int i, t;for (i=0; i1; i+)n/2t=ai;ai = 2 an-l-i;an-l-i= 3;t) main() int b9 = l,2, 3, 4, 5, 6, 7, 8, 9, i;printf(nThe original data :n);for (i=0; i9; i+)printf (%4d , bi);printf(n);fun(b, 9);printf(nThe data after invert :n);for (i=0; i9; i+)prin
33、tf (%4d , bi);printf(n);18 .给定程序中,函数FUN的功能是:在形参S所指字符串中寻找与参数C相同的字符,并在 其后插入一个与之相同的字符,若找不到相同的字符则函数不做任何处理。例如,S所指字符串:baacda,c中的字符为:a,执行后S所指字符串为:baaaacdaa include void fun(char *s, char c) int i, j, n;/*found*/for(i=0; si!=1; i+) 0if (si=c) /*found*/n=2;0while(si+l+n!= 0) n+;for(j=i+n+l; ji; j) sj+l=sj;/*
34、found*/sj+l=s3 ;ii=i+l;main() char s80=baacda”, c;printf(*nThe string: %sn*,s);printf (/znlnput 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,返回值
35、为5o#include #define N 9int fun(int a, int n) int i, j;j = 0;for (i=0; in; i+)if (ai%2=1)1aj = ai : 2;j+)return 3;jmain() int bN = 9,1,4, 2, 3, 6, 5, 8, 7, i, n;printf(nThe original data :n);for (i=0; iN; i+) printf(%4d , bi);printf(n);n = fun (b, N);printf(nThe number of odd : %d n”, n);printf(nThe
36、odd number :rT);for (i=0; in; i+) printf(*%4d ”, bi);printf(n);20 .给定程序的功能是:调用函数FUN将指定源文件中的内容复制到指定的目标文件中,复制 成功时函数返回值为0。在复制的过程中,把复制的内容输出到终端屏幕。主函数中源文件 名防在变量sfname中,目标文件名放在变量tfname中。#include#includeint fun(char *source, char *target) FILE *fs, *ft; char ch;if(fs=fopen(source, 1)=NULL)rreturn 0;if (ft=f
37、open (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, w);printf(*nThe original data
38、 :rT);for (i=l; i30; i+) c=A+rand( )%25;fprintf (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. n#include#includedouble fun(double x) double f, t; int n;f = 1. 0+1;xt = X; n = 1;do n+;t *= x
39、/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 void fun(char *s, int *t) int i, n;n=0;for(i=0; _1_ !=NULL; i+)si