1、计算机二级 C语言习题集2011计算机二级 C语言习题集.txt“我羡慕内些老人 羡慕他们手牵手一直走到最后。交话费的时候,才发现自己的话那么值钱。一、程序填空题1.给定程序中,函数fun的功能是:将形参n所指变量中,各位上为偶数的去除,剩余的数按原来从高到低的顺序 组成一个新的数,并通过形参指针n传回所指变量。例如:输入一个数:27638496,新的数为:739。#include void fun(unsigned long *n) unsigned long x=0, i; int t; i=1; while(*n)/*found*/ t=*n % 10;/*found*/ if(t%2!
2、= 0) x=x+t*i; i=i*10; *n =*n /10; /*found*/ *n=x;main() unsigned long n=-1; while(n99999999|n0) printf(Please input(0n100000000): ); scanf(%ld,&n); fun(&n); printf(nThe result is: %ldn,n);2.给定程序中,函数fun的功能是将形参给定的字符串、整数、浮点数写到文本文件中,再用字符方式从此文本文 件中逐个读入并显示在终端屏幕上。#include void fun(char *s, int a, double f)
3、/*found*/ FILE * fp; char ch; fp = fopen(file1.txt, w); fprintf(fp, %s %d %fn, s, a, f); fclose(fp); fp = fopen(file1.txt, r); printf(nThe result :nn); ch = fgetc(fp);/*found*/ while (!feof(fp) /*found*/ putchar(ch); ch = fgetc(fp); putchar(n); fclose(fp);main() char a10=Hello!; int b=12345; double
4、c= 98.76; fun(a,b,c); 3程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。所有学生数据均以二进制方式输出 到文件中。函数fun的功能是重写形参filename所指文件中最后一个学生的数据,即用新的学生数据覆盖该学生 原来的数据,其它学生的数据不变。#include #define N 5typedef struct student long sno; char name10; float score3; STU;void fun(char *filename, STU n) FILE *fp;/*found*/ fp = fopen(filename, r
5、b+);/*found*/ fseek(fp, -1L*sizeof(STU), SEEK_END);/*found*/ fwrite(&n, sizeof(STU), 1, fp); fclose(fp);main() STU tN= 10001,MaChao, 91, 92, 77, 10002,CaoKai, 75, 60, 88, 10003,LiSi, 85, 70, 78, 10004,FangFang, 90, 82, 87, 10005,ZhangSan, 95, 80, 88; STU n=10006,ZhaoSi, 55, 70, 68, ssN; int i,j; FIL
6、E *fp; fp = fopen(student.dat, wb); fwrite(t, sizeof(STU), N, fp); fclose(fp); fp = fopen(student.dat, rb); fread(ss, sizeof(STU), N, fp); fclose(fp); printf(nThe original data :nn); for (j=0; jN; j+) printf(nNo: %ld Name: %-8s Scores: ,ssj.sno, ssj.name); for (i=0; i3; i+) printf(%6.2f , ssj.scorei
7、); printf(n); fun(student.dat, n); printf(nThe data after modifing :nn); fp = fopen(student.dat, rb); fread(ss, sizeof(STU), N, fp); fclose(fp); for (j=0; jN; j+) printf(nNo: %ld Name: %-8s Scores: ,ssj.sno, ssj.name); for (i=0; i3; i+) printf(%6.2f , ssj.scorei); printf(n); 4程序通过定义学生结构体变量,存储了学生的学号、
8、姓名和3门课的成绩。所有学生数据均以二进制方式输出 到文件中。函数fun的功能是从形参filename所指的文件中读入学生数据,并按照学号从小到大排序后,再用二 进制方式把排序后的学生数据输出到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;/*found*/ fp = fopen(filename, rb); fre
9、ad(s, sizeof(STU), N, fp); fclose(fp); for (i=0; iN-1; i+) for (j=i+1; jsj.sno) t = si; si = sj; sj = t; fp = fopen(filename, wb);/*found*/ 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, 8
10、7, 10001,MaChao, 91, 92, 77, ssN; int i,j; FILE *fp; fp = fopen(student.dat, wb); fwrite(t, sizeof(STU), 5, fp); fclose(fp); printf(nnThe original data :nn); for (j=0; jN; j+) printf(nNo: %ld Name: %-8s Scores: ,tj.sno, tj.name); for (i=0; i3; i+) printf(%6.2f , tj.scorei); printf(n); fun(student.da
11、t); printf(nnThe data after sorting :nn); fp = fopen(student.dat, rb); fread(ss, sizeof(STU), 5, fp); fclose(fp); for (j=0; jN; j+) printf(nNo: %ld Name: %-8s Scores: ,ssj.sno, ssj.name); for (i=0; i3; i+) printf(%6.2f , ssj.scorei); printf(n); 5.给定程序中,函数fun的功能是将参数给定的字符串、整数、浮点数写到文本文件中,再用字符串方式从此文 本文件
12、中逐个读入,并调用库函数atoi和atof将字符串转换成相应的整数、浮点数,然后将其显示在屏幕上。#include #include void fun(char *s, int a, double f)/*found*/ FILE * fp; char str100, str1100, str2100; int a1; double f1; fp = fopen(file1.txt, w); fprintf(fp, %s %d %fn, s, a, f);/*found*/ fclose(fp) ; fp = fopen(file1.txt, r);/*found*/ fscanf(fp,%s
13、%s%s, str, str1, str2); fclose(fp); a1 = atoi(str1); f1 = atof(str2); printf(nThe result :nn%s %d %fn, str, a1, f1);main() char a10=Hello!; int b=12345; double c= 98.76; fun(a,b,c);6.给定程序中,函数fun的功能是根据形参i的值返回某个函数的值,当调用正确时,程序输出:x1=5.000000, x2=3.000000 x1*x1+x1*x2=40.000000#include double f1(double x)
14、 return x*x; double f2(double x, double y) return x*y; /*found*/double fun(int i, double x, double y) if (i=1)/*found*/ return f1(x); else/*found*/ return f2(x, y);main() double x1=5, x2=3, r; r = fun(1, x1, x2); r += fun(2, x1, x2); printf(nx1=%f, x2=%f, x1*x1+x1*x2=%fnn,x1, x2, r); 7.程序通过定义并赋初值的方式
15、,利用结构体变量存储了一名学生的信息。函数fun的功能是输出这位学生的信 息。#include typedef struct int num; char name9; char sex; struct int year,month,day ; birthday; float score3;STU;/*found*/void show(STU tt) int i; printf(n%d %s %c %d-%d-%d, tt.num, tt.name, tt.sex, tt.birthday.year, tt.birthday.month, tt.birthday.day); for(i=0; i
16、3; i+)/*found*/ printf(%5.1f, tt.scorei); printf(n);main( ) STU std= 1,Zhanghua,M,1961,10,8,76.5,78.0,82.0 ; printf(nA student data:n);/*found*/ show(std);8.给定程序通过赋初值的方式,利用结构体变量存储了一名学生的学号、姓名和3门课的成绩。函数fun的功能是 将该学生的各科成绩者乘以一个系数a。#include typedef struct int num; char name9; float score3;STU;void show(ST
17、U tt) int i; printf(%d %s : ,tt.num,tt.name); for(i=0; i3; i+) printf(%5.1f,tt.scorei); printf(n);/*found*/void modify(STU *ss,float a) int i; for(i=0; iscorei *=a;main( ) STU std= 1,Zhanghua,76.5,78.0,82.0 ; float a; printf(nThe original number and name and scores :n); show(std); printf(nInput a nu
18、mber : ); scanf(%f,&a);/*found*/ modify(&std,a); printf(nA result of modifying :n); show(std); 9.给定程序中,涵数fun的功能是将不带头结点的单向链表结点数据域中的数据从小到大排序。即若原链表结点数 据域从头至尾的数据为:10、4、2、8、6,排序后链表结点数据域从头至尾为:2、4、6、8、10。#include #include #define N 6typedef struct node int data; struct node *next; NODE;void fun(NODE *h) NO
19、DE *p, *q; int t; p = h; while (p) /*found*/ q = p-next ;/*found*/ while (q) if (p-data q-data) t = p-data; p-data = q-data; q-data = t; q = q-next; /*found*/ 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;
20、 p = q; return h;void outlist(NODE *h) NODE *p; p=h; 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); 10.给定程序中,函数fun的功能是:叛定形参a所指的NN(规定N为奇数)的矩阵是否是“幻方”,若是,函数 返回值为1;不是,函数返回值为0。“幻方”的叛定条件是:矩阵每行、每列、主对角线及反对角线上元素之和 都相等。例如:以下33的矩阵就是
21、一个“幻方”:4 9 23 5 78 1 6#include #define N 3int fun(int (*a)N) int i,j,m1,m2,row,colum; m1=m2=0; for(i=0; iN; i+) j=N-i-1; m1+=aii; m2+=aij; if(m1!=m2) return 0; for(i=0; iN; i+) /*found*/ row=colum= 0; for(j=0; jN; j+) row+=aij; colum+=aji; /*found*/ if( (row!=colum) | (row!=m1) ) return 0; /*found*/
22、 return 1;main() int xNN,i,j; printf(Enter number for array:n); for(i=0; iN; i+) for(j=0; jN; j+) scanf(%d,&xij); printf(Array:n); for(i=0; iN; i+) for(j=0; jN; j+) printf(%3d,xij); printf(n); if(fun(x) printf(The Array is a magic square.n); else printf(The Array isnt a magic square.n); 11给定程序中,函数fu
23、n的功能是将带头结点的单向链表逆置。即若原链表中从头至尾结点数据域依次为:2、 4、6、8、10,逆置后,从头至尾结点数据域依次为: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;/*found*/ p = h-next;/*found*/ if (p=0) return; q = p-next; p-next = NULL; while (q) r = q-next; q-next =
24、p;/*found*/ p = q; q = r; h-next = p;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; 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 li
25、st is NULL!n); else printf(nHead ); do printf(-%d, p-data); p=p-next; while(p!=NULL); printf(-Endn); 12.给定程序中,函数fun的功能是将不带头结点的单向链表逆置。即若原链表中从头至尾结点数据域依次为:2 、4、6、8、10,逆置后,从头至尾结点数据域依次为:10、8、6、4、2#include #include #define N 5typedef 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 = NULL; while (q) /*
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1