国家计算机二级C语言上机100套题及答案.docx

上传人:文*** 文档编号:68358293 上传时间:2022-12-27 格式:DOCX 页数:219 大小:289.37KB
返回 下载 相关 举报
国家计算机二级C语言上机100套题及答案.docx_第1页
第1页 / 共219页
国家计算机二级C语言上机100套题及答案.docx_第2页
第2页 / 共219页
点击查看更多>>
资源描述

《国家计算机二级C语言上机100套题及答案.docx》由会员分享,可在线阅读,更多相关《国家计算机二级C语言上机100套题及答案.docx(219页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、国家计算机二级c语言上机100套题答案一、填空题1.给定程序中,函数fun的功能是:将形参n所指变量中,各位上为偶数的去除,剩余的数 按原来从高到低的顺序组成一个新的数,并通过形参指针n传回所指变量。例如:输入一个数:27638496,新的数为:739。#include void fun(unsigned long *n) unsigned long x=0, i; int t;i=l;while(*n)/* found* */ t=*n % 10;/*d* */if(t%2!= 0) x=x+t*i; i=i*10; *n =*n /10;*n=x;main() unsigned long

2、n=-l;while(n99999999lln0) printf( Please input(0n 100000000): ); scanf(lcT,&n); fun(&n);printf(nnThe result is: %ldnM,n);2 .给定程序中涵数fun的功能是将形参给定的字符串、整数、浮点数写到文本文件中,再用 字符方式从此文本文件中逐个读入并显示在终端屏幕上。#include void fun(char *s, int a, double f) /* Qund* */FILE * fp;char ch;fp = fopen(file 1.txt, w1);fjprintf(f

3、p, M%s %d %fnH, s, a, f);fclose(fp);fp = fopen(Mfilel.txt, Hrn);printf(unThe result :nnH);ch = fgetc(fp);/* und* */while (!feof(fp) /* *lound* */putchar(ch); ch = fgetc(fp); putcharCn);fclose(fp);main() char a10=,Hello!H; int b= 12345;double c= 98.76;fun(a,b,c);3 .程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。所有

4、学生数 据均以二进制方式输出到文件中。函数fun的功能是重写形参filename所指文件中最后一个 学生的数据,即用新的学生数据覆盖该学生原来的数据,其它学生的数据不变。#include #define N 5 typedef struct student long char float STU;sno;name10;score3;void fun(char .filename, STU n) FILE *fp;/*/fp = fopen(filename, rb+”);/*d* */fseek(fp, -lL*sizeof(STU), SEEK_END);/*d*/fwrite(&n, si

5、zeof(STU), 1, fp);fclose(fp);)main() STU tN= 10001,MaChao 91, 92, 77, 10002,MCaoKai 75, 60, 88), 10003,LiSin, 85, 70, 78,10004,FangFangn, 90, 82, 87,10005,ZhangSanH, 95, 80, 88;STU n= 10006,ZhaoSiH, 55, 70, 68, ss|N;int i,j; FILE *fp;fp = fopen(student.dat, wb);fwrite(t, sizeof(STU), N, fp);fclose(f

6、p);fp = fopen(nstudent.datM, rb);fread(ss, sizeof(STU), N, fp);fclose(fp);printf(MnThe original data :nnM);for (j=0; jN; j+) printf(nnNo: %ld Name: %-8s Scores: n,ss|j.sno, ssj.name); for(i=0; i3; i+) printf(H%6.2fssj.scorei);printf(AnM);fun(Hstudent.datM, n);printfCAnThe data after modifing :nnM);f

7、p = fopen(,student.dat, nrbM);fread(ss, sizeof(STU), N, fp);fclose(fp);for(j=0; jN; j+) printf(nNo: %ld Name: %-8s Scores: n,ssj.sno, ssj.name); for (i=0; i3; i+) printf(H%6.2f, ssj.scorei);printfCAn*);4 .程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。所有学生数 据均以二进制方式输出到文件中。函数fun的功能是从形参filename所指的文件中读入学生 数据,并按照学号从小

8、到大排序后,再用二进制方式把排序后的学生数据输出到filename 所指的文件中,覆盖原来的文件内容。#include #define N 5typedef struct student long sno;char name10;float score3; STU;void fun(char * filename) FILE *fp; int i,j;STU sN, t;/* */fp = fopen(filename, rbn);fread(s, sizeof(STU), N, fp);fclose(fp);for(i=0; iN-l;i+)for (j=i+1; jsj.sno) t =

9、si; si = sj; s|j = t; fp = fopen(filename, Mwbn);/* */fwrite(s, sizeof(STU), N, fp);fclose(fp);main() STU tN= 10005,“ZhangSan”, 95, 80, 88, 10003,“LiSi”, 85, 70, 78),10002,CaoKai,; 75, 60, 88, 10004,FangFang*, 90, 82, 87,10001,MaChao, 91, 92, 77, ssN;int i,j; FILE *fp;fp = fopen(Mstudent.dat; MwbM);

10、fwrite(t, sizeof(STU), 5, fp);fclose(fp);printf(MnnThe original data :nnM);for(j=0; jN; j+) printf(MnNo: %ld Name: %-8s Scores: n,tjJ.sno, t|j|.name);for (i=0; i3; i+) printf(M%6.2ftj.scorei);printf(nH);)fun( student.dat*);printf(unnThe data after sorting :nn);fp = fopen(,student.datu, rb);fread(ss,

11、 sizeof(STU), 5, fp);fclose(fp);for(j=0; jN; j+) printf(HnNo: %ld Name: %-8s Scores: ssljl.sno, ssj.name);for (i=0; i3; i+) printf(n%6.2fssj.scorei);printf(,lnH);)5 .给定程序中,函数fun的功能是将参数给定的字符用、整数、浮点数写到文本文件中,再 用字符串方式从此文本文件中逐个读入,并调用库函数atoi和atof符字符串转换成相应的 整数、浮点数,然后将其显示在屏幕上。#include #include void fun(char

12、 *s, int a, double f)(/,* Qund* */FILE * fp;char str| 100, strl100|, str2100;int al; double fl;fp = fopen(Mfilel.txt; HwH);fprintf(fp, n%s %d %fn s, a, f);/* found* */fclose(fp);fp = fopen(filel.txt“,r);/* found* */fscanf(fp,M%s%s%sM, str, stri, str2);fclose(fp);al = atoi(strl);fl = atof(str2);print

13、f(nThe result :nn%s %d %fnM, str, al, fl); main() char a10=Heno!”; int b= 12345;double c= 98.76; fun(a,b,c);6 .给定程序中,函数fun的功能是根据形参i的值返回某个函数的值,当调用正确时,程序 输出:x 1=5.000000, x2=3.0()0000 xl*xl+xl*x2=40.000000#include double fl (double x) return x*x; double f2(double x, double y) return x*y; /豕* toun d */

14、double fun(int i, double x, double y) if (i=D /* found* */return fl(x); else/* )und* */ return f2(x, y);) main() double xl=5, x2=3, r;r = fun(l, xl, x2);r += fun(2, xl, x2);printf(nnxl=%f, x2=%f, xl*xl+xl*x2=%fnnM,xl, x2, r); )7 .程序通过定义并赋初值的方式,利用结构体变量存储了一名学生的信息。函数fun的功能 是输出这位学生的信息。#include typedef s

15、truct int num;char name 9;char sex;struct int year,month,day ; birthday;float score 3;STU;/* found* */void show(STU tt) int i;printf(An%d %s %c %d-%d-%dM, tt.num, tt.name, tt.sex, tt. birthday, ye ar, tt.birthday.month, tt.birthday.day);for(i=0; i3; i+)/* * */printf(n%5.1f, tt.scorei);printf(MnM);ma

16、in() STU std= 1 /ZhanghuaVM; 1961,10,8,76.5,78.0,82.0 ;printfCAnA student data:nu);/* four) ci* */show(std);8 .给定程序通过赋初值的方式,利用结构体变量存储了一名学生的学号、姓名和3门课的成 绩。函数fun的功能是将该学生的各科成绩者乘以一个系数a。#include typedef struct int num;char name9;float score 3;STU;void show(STU tt) int i;printf(n%d %s : tt.num,tt.name);for

17、(i=0; i3; i+)printf(H%5. If tt.scorei);printf(nnn);/* found* */ void modify(STU *ss,float a) int i;for(i=0; iscorei *=a;)main() STU std= IJZhanghua”,76.5,78.0,82.0 ;float a;printf(AnThe original number and name and scores :nM);show(std);printf(nlnput a number:); scanf(M%f&a);/* found* */ modify(&std

18、,a);printf(AnA result of modifying :nn); show(std);9 .给定程序中,涵数fun的功能是将不带头结点的单向链表结点数据域中的数据从小到大排 序。即若原链表结点数据域从头至尾的数据为:10、4、2、8、6,排序后链表结点数据域从 头至尾为:2、4、6、8、10o#include #include #define N 6typedef struct node int data;struct node *next; NODE;void fun(NODE *h) NODE *p, *q; int t;p = h;while (p) * * found*

19、 */q = p-next;while (q)if (p-data q-data) t = p-data; p-data = q-data; q-data = t; q = q-next;p = p-next;)NODE *creatlist(int a) NODE *h,*p,*q; int i;h=NULL;for(i=0; idata=ai;q-next = NULL;if(h = NULL) h = p = q;else p-next = q; p = q; return h;void outlist(NODE *h) NODE *p;p=h;if(p=NULL) printf(HTh

20、e list is NULL!nn);else printf(AnHead ); do printf(,-%d, p-data); p=p-next; while(p!=NULL);printf(-Endn);)10 .给定程序中,函数fun的功能是:叛定形参a所指的NXN (规定N为奇数)的矩阵是否 是幻方,若是,函数返回值为1;不是,函数返回值为0。“幻方”的叛定条件是:矩阵 每行、每列、主对角线及反对角线上元素之和都相等。例如:以下3X3的矩阵就是一个“幻 方:4 9 23 5 78 1 6#include #define N 3int fun(int (*a)N) int ij,ml,

21、m2,row,colum;m 1 =m2=0;for(i=0; iN; i+)j=N-i-l; ml+=aii; m2+=aij; if(ml !=m2) return 0;for(i=0; iN; i+) /* Qund* */row=colum= 0;for(j=0; jN; j+) row+=aij; colum+=aji; /* * found* */if( (row!=colum) II (row!=ml) return 0;)/* * found* */return 1;)main() int xNN,ij;printf(MEnter number for array:nu);fo

22、r(i=0; iN; i+)for(j=0; jvN; j+) scanf(,%d,&xij);printf(,Array:nn);for(i=0; iN; i+) for(j=0; jN; j+) printf(3d”,xj); printf(AnM);)if(fun(x) printf(HThe Array is a magic squareAn);else printf(rThe Array isnt a magic square.nn);11 .给定程序中,函数fun的功能是将带头结点的单向链表逆置。即若原链表中从头至尾结 点数据域依次为:2、4、6、8、10,逆置后,从头至尾结点数据

23、域依次为:10、8, 6、4、2。 #include #include #define N 5typedef struct node int data;struct node *next; NODE;void fun(NODE *h) NODE *p, *q, *r;/* f)und* */p = h-next;/* found* */if (p=0) return;q = p-next;p-next = NULL;while (q) r = q-next; q-next = p;/* pQund* */P = q;q = r;)h-next = p;)NODE *creatlist(int

24、a) NODE *h,*p,*q; int i;h = (NODE *)malloc(sizeof(NODE);h-next = NULL;for(i=0; idata=ai;q-next = NULL;if (h-next = NULL) h-next = p = q;else p-next = q; p = q; return h;)void outlist(NODE *h) NODE *p;p = h-next;if(p=NULL) printf(The list is NULL!n); else printf(*nHead );do printf(-%dH, p-data); p=p-

25、next; while(p!=NULL);printf(-Endn,);12 .给定程序中,函数fun的功能是将不带头结点的单向链表逆置。即若原链表中从头至尾 结点数据域依次为:2、4、6、8、10,逆置后,从头至尾结点数据域依次为:10、8、6、4、 2#include#include#definetypedef struct node int data;struct node *next; NODE;/* found* */NODE * fun(NODE *h) NODE *p, *q, *r;p = h;if(p= NULL)return NULL;q = p-next;p-next =

26、 NULL; while (q)/* 不 found* */r = q-next;q-next = p;p = q;/* found* */q = r;)return p;NODE *creatlist(int a) NODE *h,*p,*q; int i;h=NULL;for(i=0; idata=ai;q-next = NULL;if (h = NULL) h = p = q;else p-next = q; p = q; return h;)void outlist(NODE *h) NODE *p;p=h;if(p=NULL) printf(MThe list is NULL!nH)

27、; else printf(AnHead ); do printf(-%dH, p-data); p=p-next; while(p!=NULL);printf( ,-EndnH);13 .给定程序中,函数fun的功能是将带头结点的单向链表结点数据域中的数据从小到大排 序。即若原链表结点数据域从头至尾的数据为:10、4、2、8、6,排序后链表结点数据域从 头至尾的数据为:2、4、6, 8、10。#include #include #define N 6type def struct node int data;struct node *next; NODE;void fun(NODE *h)

28、NODE *p, *q; int t;/* tound* */p = h-next;while (p) q = p-next; while (q) /* found* */ if (pdata = q-data) t = p-data; p-data = q-xiata; q-data = t; q = q-next;)p = p-next;) ) NODE *creatlist(int a|) NODE *h,*p,*q; int i;h = (NODE *)malloc(sizeof(NODE);h-next = NULL;for(i=0; idata=ai;q-next = NULL;i

29、f (h-next = NULL) h-next = p = q; else p-next = q; p = q; )return h; void outlist(NODE *h) NODE *p;p = h-next;if (p=NULL) printf(The list is NULL!nu); else printf(HnHead ); do printf(n-%d, p-data); p=p-next; while(p!=NULL);printf(n-EndnH);)114 .给定程序中,函数fun的功能是用函数指针指向要调用的函数,并进行调用。规定在 2处使f指向函数fl,在3处使f

30、指向函数f2。当调用正确时,程序输出:X 1=5.000000, x2=3.000000, xl*xl+xl*x2=40.000000#include double fl (double x) return x*x; double f2(double x, double y) return x*y; double fun(double a, double b)double (*f)();double rl, r2;/* found* */f = fl ;/* point fountion fl */rl = f(a);/* found* */f = f2 ;/* point fountion

31、f2 */i2 = (*f)(a,b);return rl + r2;)main() double xl=5, x2=3, r;r = fun(xl, x2);printf(nxl=%f, x2=%f, xl*xl+xl*x2=%An,xl, x2, r); )15 .程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。所有学生数 据均以二进制方式输出到student.dat中。函数fun的功能是从指定文件中找出指定学号的学 生数据,读入此学生数据,对该生的分数进行修改,使每门课的分数加3分,修改后重写文 件中该学生的数据,即用该学生的新数据覆盖原数据,其它学生数据不变,若找不到

32、,则什 么都不做。#include #define N 5 typedef struct student long char float STU;sno;name10;score3;void fun(char *filename, long sno) FILE *fp;STU n;mt i;fp = fopen(fi lename, ,rb+,);/* * found* */while (!feof(fp) fread(&n, sizeof(STU), 1, fp);/* Qund* */if (n.sno=sno) break;)if(!feof(fp) for (i=0; i3; i+)

33、n.scorei += 3;/* found* */fseek(fp, -lL*sizeof(STU), SEEK_CUR);fwrite(&n, sizeof(STU), 1, fp);fclose(fp);)16 .给定程序中,函数fun的功能是:求出形参ss所指字符串数组中最长字符串的长度,将 其余字符串右边用字符*补齐,使其与最长字符串等长。ss所指字符串数组中共有N个字符 串,且串长vN。#include#include#define M 5#define N 20void fun(char (*ss)N) int i, j, n, len=0;for(i=0; in)n=len;f

34、or(i=0; iM; i+) /* found* */n=strlen(ssi);for(j=0; jlen-n; j+)/豕* toun d */ssin+j=*;* * found* */ssin+j+l=W;)main() char ssMN=shanghai”,“guangzhoir,“beijing”,tianjing”Jcchongqing);int i;printf(MThe original strings are :n);for(i=0; iM; i+) printf(%sn,ssi);printf(AnH);fun(ss);printf(The result is :n)

35、;for(i=0; iM; i+) printf( M%snss i);17 .程序通过定义学生结构体数组,存储了若干名学生的学号、姓名和3门课的成绩,函数 fun的功能是将存放学生数据的结构体数组,按照姓名的字典序(从小到大)排序。#include #include struct student long sno;char name 10;float score 3;);void fun(struct student a, int n) (/字 * und* */struct student t;int ij;/* * )und* */for (i=0; in-l;i+)for (j=i+l

36、; j 0) t = ai; ai = aj; aj =t; ) main() struct student s4= 10001 J,ZhangSan, 95, 80,88,10002,nLiSiM, 85, 70, 78,10003;CaoKaiH, 75, 60, 88, 10004,HFangFang; 90, 82, 87);int i, j;printf(nnThe original data :nnH); for(j=0; j4;j+)Scores: sj.sno, sj.name); printf(unNo: %ld Name: %-8sfor(i=0; i3; i+) prin

37、tf(n%6.2f, sj.scorei); printfCAn1);fun(s, 4);printf(nnnThe data after sorting :nn);for(j=0;j4;j+) printf(HnNo: %ld Name: %-8s Scores: n,s|j.sno, sj.name);for(i=0; i3; i+) printf(H%6.2f sj.scorei); printf(Ann);) )18 .给定程序中,函数ftm的功能是:将形参s所指字符中中的所有字母字符顺序前移,其他 字符顺序后移,处理后新字符串的首地址作为函数值返回。例如,s所指字符串为:asd123

38、fgh543df,处理后新字符串为asdfghdf123543。#include #include #include char *fun(char *s) int i,j, k, n; char *p, *t;n=strlen(s)+l;t=(char*)malloc(n*sizeof(char); p=( char* )mall oc( n*sizeof(char); j=0; k=0;for(i=0; i=,a)&(si=,A,)&(si=,Z,) /* found* */t|j=si; j+;)else pk=si; k+; /* * und* */fbr(i=O; ik; i+) t|j+i =pi;/* */tU+k=O;return t;)main() char s80;printf(nPIease input: M); scanf(M%sM,s);printf(nThe result is: %sn,furKs);19 .程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。函数fun的 功能是将形参a所指结构体变量s中的数据进行修改,并把a中地址作为函数值返回主函数, 在主函数中输出修改后的数据。例如:a所指变量中的学号、姓名、和三门课的成绩依次是:10001、 ZhangSan、95、80、 88,修

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com