




已阅读5页,还剩263页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
一、程序填空题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; /*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)/*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 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, rb+);/*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; 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; jN; j+) printf(nNo: %ld Name: %-8s Scores: ,ssj.sno, ); for (i=0; i3; i+) printf(%6.2f , ssj.scorei); 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, ); for (i=0; i3; i+) printf(%6.2f , ssj.scorei); printf(n); 4程序通过定义学生结构体变量,存储了学生的学号、姓名和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); fread(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, 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; jN; j+) printf(nNo: %ld Name: %-8s Scores: ,tj.sno, ); for (i=0; i3; i+) printf(%6.2f , tj.scorei); printf(n); fun(student.dat); 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, ); for (i=0; i3; i+) printf(%6.2f , ssj.scorei); printf(n); 5.给定程序中,函数fun的功能是将参数给定的字符串、整数、浮点数写到文本文件中,再用字符串方式从此文 本文件中逐个读入,并调用库函数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%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) 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.程序通过定义并赋初值的方式,利用结构体变量存储了一名学生的信息。函数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.sex, tt.birthday.year, tt.birthday.month, tt.birthday.day); for(i=0; i3; 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(STU tt) int i; printf(%d %s : ,tt.num,); 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 number : ); 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) 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-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=p-next; while(p!=NULL); printf(-Endn); 10.给定程序中,函数fun的功能是:叛定形参a所指的NN(规定N为奇数)的矩阵是否是“幻方”,若是,函数 返回值为1;不是,函数返回值为0。“幻方”的叛定条件是:矩阵每行、每列、主对角线及反对角线上元素之和 都相等。例如:以下33的矩阵就是一个“幻方”: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*/ 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给定程序中,函数fun的功能是将带头结点的单向链表逆置。即若原链表中从头至尾结点数据域依次为: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 = 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 list 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) /*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(The list is NULL!n); else printf(nHead ); do printf(-%d, p-data); p=p-next; while(p!=NULL); printf(-Endn); 13.给定程序中,函数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) NODE *p, *q; int t;/*found*/ p = h-next ; while (p) /*found*/ q = p-next ; while (q) /*found*/ 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 = (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(-%d, p-data); p=p-next; while(p!=NULL); printf(-Endn); 14.给定程序中,函数fun的功能是用函数指针指向要调用的函数,并进行调用。规定在_2_处使f指向函数 f1,在_3_处使f指向函数f2。当调用正确时,程序输出: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; double fun(double a, double b)/*found*/ double (*f)(); double r1, r2;/*found*/ f = f1 ; /* point fountion f1 */ r1 = f(a);/*found*/ f = f2 ; /* point fountion f2 */ r2 = (*f)(a, b); return r1 + r2;main() double x1=5, x2=3, r; r = fun(x1, x2); printf(nx1=%f, x2=%f, x1*x1+x1*x2=%fn,x1, x2, r); 15.程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。所有学生数据均以二进制方式输出 到student.dat中。函数fun 的功能是从指定文件中找出指定学号的学生数据,读入此学生数据,对该生的分数 进行修改,使每门课的分数加3分,修改后重写文件中该学生的数据,即用该学生的新数据覆盖原数据,其它学 生数据不变,若找不到,则什么都不做。#include #define N 5typedef struct student long sno; char name10; float score3; STU;void fun(char *filename, long sno) FILE *fp; STU n; int i; fp = fopen(filename,rb+);/*found*/ while (!feof(fp) fread(&n, sizeof(STU), 1, fp);/*found*/ if (n.sno=sno) break; if (!feof(fp) for (i=0; i3; i+) n.scorei += 3;/*found*/ fseek(fp, -1L*sizeof(STU), SEEK_CUR); fwrite(&n, sizeof(STU), 1, fp); fclose(fp);16.给定程序中,函数fun的功能是:求出形参ss所指字符串数组中最长字符串的长度,将其余字符串右边用字符 *补齐,使其与最长字符串等长。ss所指字符串数组中共有N个字符串,且串长N。#include #include #define M 5#define N 20void fun(char (*ss)N) int i, j, n, len=0; for(i=0; in)n=len; for(i=0; iM; i+) /*found*/ n=strlen(ssi); for(j=0; jlen-n; j+)/*found*/ ssin+j=*;/*found*/ ssin+j+1=0; main() char ssMN=shanghai,guangzhou,beijing,tianjing,cchongqing; int i; printf(The original strings are :n); for(i=0; iM; i+) printf(%sn,ssi); printf(n); fun(ss); printf(The result is :n); for(i=0; iM; i+) printf(%sn,ssi); 17.程序通过定义学生结构体数组,存储了若干名学生的学号、姓名和3门课的成绩,函数fun的功能是将存放学 生数据的结构体数组,按照姓名的字典序(从小到大)排序。#include #include struct student long sno; char name10; float score3;void fun(struct student a, int n)/*found*/ struct student t; int i, j;/*found*/ for (i=0; in-1; i+) for (j=i+1; j 0) t = ai; ai = aj; aj = t; main() struct student s4=10001,ZhangSan, 95, 80, 88,10002,LiSi, 85, 70, 78, 10003,CaoKai, 75, 60, 88, 10004,FangFang, 90, 82, 87; int i, j; printf(nnThe original data :nn); for (j=0; j4; j+) printf(nNo: %ld Name: %-8s Scores: ,sj.sno, ); for (i=0; i3; i+) printf(%6.2f , sj.scorei); printf(n); fun(s, 4); printf(nnThe data after sorting :nn); for (j=0; j4; j+) printf(nNo: %ld Name: %-8s Scores: ,sj.sno, ); for (i=0; i3; i+) printf(%6.2f , sj.scorei); printf(n); 18.给定程序中,函数fun的功能是:将形参s所指字符中中的所有字母字符顺序前移,其他字符顺序后移,处理 后新字符串的首地址作为函数值返回。例如,s所指字符串为:asd123fgh543df,处理后新字符串为asdfghdf123543。#include #include #include char *fun(char *s) int i, j, k, n; char *p, *t; n=strlen(s)+1; t=(char*)malloc(n*sizeof(char); p=(char*)malloc(n*sizeof(char); j=0; k=0; for(i=0; i=a)&(si=A)&(si=Z) /*found*/ tj=si; j+; else pk=si; k+; /*found*/ for(i=0; ik; i+) tj+i=pi;/*found*/ tj+k= 0; return t;main() char s80; printf(Please input: ); scanf(%s,s); printf(nThe result is: %sn,fun(s); 19.程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。函数fun的功能是将形参a所指结构 体变量s中的数据进行修改,并把a中地址作为函数值返回主函数,在主函数中输出修改后的数据。例如:a所指变量中的学号、姓名、和三门课的成绩依次是:10001、”ZhangSan”、95、80、88,修改后输出t 中的数据应为:10002、”LiSi”、96、81、89。#include #include struct student long sno; char name10; float score3;/*found*/struct student * fun(struct student *a) int i; a-sno = 10002; strcpy(a-name, LiSi);/*found*/ for (i=0; iscorei += 1;/*found*/ return a ;main() struct student s=10001,ZhangSan, 95, 80, 88, *t; int i; printf(nnThe original data :n); printf(nNo: %ld Name: %snScores: ,s.sno, );
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 年产20万吨氟化系列产品生产项目实施方案(参考模板)
- 锂电池回收利用项目可行性研究报告
- 《傲慢与偏见》读书心得 15篇
- 光伏装配示范复合项目可行性研究报告(参考范文)
- 河北省部分高中2023-2024学年高三上学期12月期末考语文含解析
- 安徽省名校联盟2023-2024学年高三上学期实验班12月大联考英语含解析
- 江苏航运职业技术学院《建筑与装饰工程施工》2023-2024学年第二学期期末试卷
- 喀什理工职业技术学院《城乡生态与环境规划》2023-2024学年第二学期期末试卷
- 安徽广播影视职业技术学院《纺织品实验与设计》2023-2024学年第二学期期末试卷
- 湖南工业大学《机械控制工程基础》2023-2024学年第二学期期末试卷
- 普惠金融专员试题及答案
- 《心电图机操作与应用》课件
- 《金属疲劳与断裂》课件
- 办公楼清洁服务工作外包合同5篇
- 剧场协议合同范例
- 2025中小学学校校服采购工作方案
- 2024年烟台龙口市卫生健康局所属事业单位招聘工作人员笔试真题
- 西部计划考试考题及答案
- 《中国溃疡性结肠炎诊治指南(2023年)》解读
- 《Something Just Like This》歌词
- 初中数学学科90学时培训方案及日程安排
评论
0/150
提交评论