2011计算机二级 c语言习题集

上传人:子 文档编号:43001949 上传时间:2018-06-04 格式:DOC 页数:12 大小:17.97KB
返回 下载 相关 举报
2011计算机二级 c语言习题集_第1页
第1页 / 共12页
2011计算机二级 c语言习题集_第2页
第2页 / 共12页
2011计算机二级 c语言习题集_第3页
第3页 / 共12页
2011计算机二级 c语言习题集_第4页
第4页 / 共12页
2011计算机二级 c语言习题集_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《2011计算机二级 c语言习题集》由会员分享,可在线阅读,更多相关《2011计算机二级 c语言习题集(12页珍藏版)》请在金锄头文库上搜索。

1、20112011 计算机二级计算机二级 C C 语言习题集语言习题集一、程序填空题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!= 0) x=x+t*i; i=i*10; *n =*n /10;/*fo

2、und*/*n=x;main() unsigned long n=-1;while(n99999999|nvoid fun(char *s, int a, double f)/*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

3、(ch); ch = fgetc(fp); putchar(n);fclose(fp);main() char a10=“Hello!“; int b=12345;double c= 98.76;fun(a,b,c);3程序通过定义学生结构体变量,存储了学生的学号、姓名和 3 门课的成绩。所有学生数据均以二进制方式输出 到文件中。函数 fun 的功能是重写形参 filename 所指文件中最后一个学生的数据,即用新的学生数据覆盖该学生 原来的数据,其它学生的数据不变。#include #define N 5typedef struct student long sno;char name10;

4、float score3; STU;void fun(char *filename, STU n) FILE *fp;/*found*/fp = fopen(filename, “rb+“);/*found*/fseek(fp, -1L*sizeof(STU), SEEK_END);/*found*/fwrite(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,

5、“ZhangSan“, 95, 80, 88;STU n=10006,“ZhaoSi“, 55, 70, 68, ssN;int i,j; FILE *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; j#define N 5typedef struct stude

6、nt long sno;char name10;float score3; STU;void fun(char *filename) FILE *fp; int i, j;STU sN, t;/*found*/fp = fopen(filename, “rb“);fread(s, sizeof(STU), N, fp);fclose(fp);for (i=0; isj.sno) t = si; si = sj; sj = t; fp = fopen(filename, “wb“);/*found*/fwrite(s, sizeof(STU), N, fp);fclose(fp);main()

7、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(“student.dat“, “wb“);fwrite(t, sizeof(STU), 5, fp);fclose(fp);printf(“nnThe original data :nn“);for (j=0; j#include voi

8、d 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%s%s“, str, str1, str2);fclose(fp);a1 = atoi(str1);f1 = atof(str2);

9、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) return x*x; double f2(double x, double y) return x*y; /*found*/

10、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.程序通过定义并赋初值的方式,利用结构体变量存储了一名学生的信息。函数 fun 的功能是输出这位学生的信 息。#include typedef struct int

11、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; itypedef struct int num;char name9;float score3;STU;void show(STU tt) int

12、 i;printf(“%d %s : “,tt.num,tt.name);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 number : “); scanf(“%f“,/*found*/modify(printf(“nA result of modifying :n“);show(std);9.给定程序中,涵数 fun 的功能是将不

13、带头结点的单向链表结点数据域中的数据从小到大排序。即若原链表结点数 据域从头至尾的数据为: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) NODE *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-

14、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; 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

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

当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号