计算机二级C语言上机试题100套及答案.doc

上传人:西安人 文档编号:3291979 上传时间:2019-08-08 格式:DOC 页数:328 大小:1.52MB
返回 下载 相关 举报
计算机二级C语言上机试题100套及答案.doc_第1页
第1页 / 共328页
计算机二级C语言上机试题100套及答案.doc_第2页
第2页 / 共328页
计算机二级C语言上机试题100套及答案.doc_第3页
第3页 / 共328页
计算机二级C语言上机试题100套及答案.doc_第4页
第4页 / 共328页
计算机二级C语言上机试题100套及答案.doc_第5页
第5页 / 共328页
点击查看更多>>
资源描述

《计算机二级C语言上机试题100套及答案.doc》由会员分享,可在线阅读,更多相关《计算机二级C语言上机试题100套及答案.doc(328页珍藏版)》请在三一文库上搜索。

1、上 机 题 库第一部分 填空题注意:源程序存放在考生文件夹下的BLANK1.C中。1. 不得增行或删行,也不得更改程序的结构!2. 请在程序下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。1. 给定程序中,函数FUN的功能是:计算出带有头接点的单向链表中各结点数据域之和作为函数值返回。#include #include #define N 8typedef struct list int data;struct list *next; SLIST;SLIST *creatlist(int *);void outlist(SLIST *);int fun( SLIST *h) SLI

2、ST *p; int s=0;p=h-next;while(p)/*found*/s+= p-_1_; data/*found*/p=p-_2_; nextreturn s;main( ) SLIST *head;int aN=12,87,45,32,91,16,20,48;head=creatlist(a); outlist(head);/*found*/printf(nsum=%dn, fun(_3_); headSLIST *creatlist(int a ) SLIST *h,*p,*q; int i;h=p=(SLIST *)malloc(sizeof(SLIST);for(i=0;

3、 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(nHead );do printf(-%d, p-data); p=p-next; while(p!=NULL);printf(-Endn);2. 给定程序中,函数FUN的功能是:求出形参SS所指字符串数组中最长字符串的长度,其余字符串左边用字符*补齐,使其与最长的字符串等长。字符串数组中工有M个字符串,且串长N。#includ

4、e #include #define M 5#define N 20void fun(char (*ss)N) int i, j, k=0, n, m, len;for(i=0; in) /*found*/n=len; k=_1_; i for(i=0; i=0; j-) lenssim-=ssij;for(j=0; jn-len; j+)/*found*/ssij=_3_; *main( )char sMN=shanghai,guangzhou,beijing,tianjing,cchongqing;int i;printf(nThe original strings are :n);for

5、(i=0; iM; i+) printf(%sn,ssi);printf(n);fun(ss);printf(nThe result:n);for(i=0; iM; i+) printf(%sn,ssi);3. 人员记录由编号和出生年,月,日组成,N名人员的数据已在主函数中存入结构体数组std且编号唯一。函数fun 的功能是;找出指定编号人员的数据,作为函数值返回,有主函数输出,若指定编号不存在,返回数据中的编号为空串。#include #include #define N 8typedef struct char num10;int year,month,day ;STU;/*found*/

6、_1_fun(STU *std, char *num) STU int i; STU a=,9999,99,99;for (i=0; iN; i+)/*found*/if( strcmp(_2_,num)=0 ) stdi.num/*found*/return (_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

7、,19;STU p; char n10=666666;p=fun(std,n);if(p.num0=0)printf(nNot found !n);else printf(nSucceed !n );printf(%s %d-%d-%dn,p.num,p.year,p.month,p.day);4. 给定程序中已建立一个带有头结点的单向链表,链表中的各结点按数据域递增有序链接。函数fun 的功能:删除链表中数据域值相同的结点,使之保留一个。#include #include #define N 8typedef struct list int data;struct list *next; S

8、LIST;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;/*found*/free(_1_); q/*found*/q=p-_2_; nextelse p=q;/*found*/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;

9、 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(-%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=creatlist(a);printf(nThe list before deleting :n); outlist(head);f

10、un(head);printf(nThe list after deleting :n); outlist(head);5. 函数fun 的功能是进行数字字符转换。若形参ch中是数字字符0 9,则0转换成9, 1转换成8, 2转换成7 9转换成0;若是其他字符则保持不变;并将转换后的结果作为函数值返回.#include /*found*/_1_fun(char ch) char/*found*/if (ch=0 &_2_) ch=9/*found*/return 9- (ch-_3_); 0return ch ;main( ) char c1, c2;printf(nThe result :n

11、);c1=2; c2 = fun(c1);printf(c1=%c c2=%cn, c1, c2);c1=8; c2 = fun(c1);printf(c1=%c c2=%cn, c1, c2);c1=a; c2 = fun(c1);printf(c1=%c c2=%cn, c1, c2);6. 给定程序中,函数fun的功能是:对形参ss所指字符串数组中的M字符串按长度由短到长进行排序。SS所指字符串数组中共有M个字符串,且串长N#include #include #define M 5#define N 20void fun(char (*ss)N) int i, j, k, nM; cha

12、r tN;for(i=0; iM; i+) ni=strlen(ssi);for(i=0; iM-1; i+) k=i;/*found*/for(j=_1_; jnj) k=_2_; jif(k!=i) strcpy(t,ssi);strcpy(ssi,ssk);/*found*/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,

13、ssi);printf(n);fun(ss);printf(nThe result :n);for(i=0; iM; i+) printf(%sn,ssi);7. 函数FUN的功能是:从三个形参a,b,c中找出中间的哪个数,作为函数值返回。例如,当a=3,b=5,c=4时,中数为4。#include int fun(int a, int b, int c) int t;/*found*/t = (ab) ? (bc? b :(ac?c:_1_) : (ac)?_2_ : (bc)?c:_3_);return t; 1. a 2. a 3.bmain( ) int a1=3, a2=5, a3=

14、4, r;r = fun(a1, a2, a3);printf(nThe middle number is : %dn, r);8. 给定程序中,函数FUN的功能是:统计出带有头结点的单向链表中接点的个数,存放在形参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;/*found*/*n=_1_

15、;0p=h-next;while(p) (*n)+;/*found*/p=_2_; p-nextmain( ) SLIST *head;int aN=12,87,45,32,91,16,20,48, num;head=creatlist(a); outlist(head);/*found*/fun(head,_3_); &numprintf(nnumber=%dn,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

16、=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(nHead );do printf(-%d,p-data); p=p-next; while(p!=NULL);printf(-Endn);9. 给定程序的功能是:调用函数FUN将指定源文件中的内容复制到指定的目标文件中,复制成功时函数返回值为1,失败时返回值为0。在复制的过程中,把复制的内容输出到终端屏幕。主函数中源文件名房子变量sfname中,目标文件名放在变量tfn

17、ame中。#include #include int fun(char *source, char *target) FILE *fs,*ft; char ch;/*found*/if(fs=fopen(source,_1_)=NULL) rreturn 0;if(ft=fopen(target, w)=NULL)return 0;printf(nThe data in file :n);ch=fgetc(fs);/*found*/while(!feof(_2_) fs putchar( ch );/*found*/fputc(ch,_3_); ftch=fgetc(fs);fclose(fs

18、); fclose(ft);printf(nn);return 1;main( ) char sfname20 =myfile1,tfname20=myfile2;FILE *myf; int i; char c;myf=fopen(sfname,w);printf(nThe original data :n);for(i=1; i30; i+) c=A+rand( )%25;fprintf(myf,%c,c); printf(%c,c); fclose(myf);printf(nn);if (fun(sfname, tfname) printf(Succeed!);else printf(F

19、ail!);10. 给定 程序中,函数FUN的功能是:在形参S所指字符串中的每个数字字符之后插入一个*。例如,形参S所指字符串为:def35adh3kjsdf7.执行结果为:def3*5*adh3*kjsdf7*#include void fun(char *s) int i, j, n;for(i=0; si!=0; i+)/*found*/if(si=0 _1_ sii; j-)/*found*/sj+1= _3_;sjsj+1=*;i=i+1;main( ) char s80=ba3a54cd23a;printf(nThe original string is : %sn,s);fun(

20、s);printf(nThe result is : %sn,s);11. 给定程序中,函数FUN的功能是:将形参STD所指结构体数组中年龄最大者的数据作为函数值返回,并将 main函数输出。#include typedef struct char name10;int age;STD;STD fun(STD std, int n) STD max; int i;/*found*/max=_1_; std0for(i=1; in; i+)/*found*/if(max.age_2_) max=stdi; stdi.agereturn max;main( ) STD std5=aaa,17,bb

21、b,16,ccc,18,ddd,17,eee,15 ;STD max;max=fun(std,5); printf(nThe result: n);/*found*/printf(nName : %s, Age : %dn,_3_ ,max.age); max.name12. 给定程序中,函数fun 功能是:在带有头结点的单向链表中,查找数据域中为CH的结点,找到后通过函数值返回该结点在链表中所处的顺序号;若不存在值为CH的结点,函数返回0值。#include #include #define N 8typedef struct list int data;struct list *next;

22、 SLIST;SLIST *creatlist(char *);void outlist(SLIST *);int fun( SLIST *h, char ch) SLIST *p; int n=0;p=h-next;/*found*/while(p!=_1_)NULL n+;/*found*/if (p-data=ch) return _2_; nelse 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(En

23、ter a letter:);scanf(%c,&ch);/*found*/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; idata=ai; p-next=q; p=q;p-next=0;return h;void outlist(SLIST *h) SLIST *p;p=h-n

24、ext;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实现.#include #include #include void WriteText(FILE *);

25、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);/*found*/void WriteText(FILE _1_ ) *fw char str81;printf(nEnter string with -1 to en

26、d :n);gets(str);while(strcmp(str,-1)!=0) /*found*/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);while( !feof(fr) ) /*found*/printf(%s,_3_); strfgets(str,81,fr);14. 给定程序中,函数FUN的功能是:有N X N矩阵,根据给定的m(m=N)值,将每个元素中的值均右移m个位置,左边置为0。例如,N=3,m=2,有下列矩阵123 001456 程序执行结果为004789007#include #define N 4void fun(int (*t)N, int m) int i, j;/*found*/for(i=0; iN; _1_ )

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 研究报告 > 信息产业


经营许可证编号:宁ICP备18001539号-1