c/c++练习–20
来源:互联网 发布:二维码抽奖软件 编辑:程序博客网 时间:2024/06/06 02:36
c/c++练习–20
- 习题来源:C语言经典编程282例
191.使用宏定义实现数组值的互换
#include <stdio.h>#include <stdlib.h>#define swap(a,b) int c;c=a,a=b,b=cint main(void){ int a=1,b=2; swap(a,b); printf("%d\t%d\n",a,b); return(EXIT_SUCCESS);}
192.利用宏定义求偶数和
利用宏定义求1~100的偶数和,定义一个宏判断一个数时候为偶数。
#include <stdio.h>#include <stdlib.h>#define num(a) (a%2==0?1:0)int main(void){ int sum=0; for(int i=0;i<100;i++){ if(num(i))sum+=i; } printf("%d\n",sum); return(EXIT_SUCCESS);}
193.关闭所有打开的文件
在程序中打开已有的3个文件,读取文件内容并显示在屏幕上,要求调用fcloseall函数一次关闭三个文件。
#include <stdio.h>#include <stdlib.h>int main(void){ FILE *fp1,*fp2,*fp3; int a[3][3]={0}; fp1 = fopen("1.txt", "r"); fp2 = fopen("2.txt", "r"); fp3 = fopen("3.txt", "r"); fscanf(fp1,"%d %d %d",*a,*a+1,*a+2); fscanf(fp2,"%d %d %d",*(a+1),*(a+1)+1,*(a+1)+2); fscanf(fp3,"%d %d %d",*(a+2),*(a+2)+1,*(a+2)+2); fcloseall(); for(int i=0;i<3;i++) for(int j=0;j<3;j++) printf("%d\t",a[i][j]); return(EXIT_SUCCESS);}
194.同时显示两个文件的内容
将两个不同文件的内容在屏幕的指定位置显示出来,第5行第3列显示“file1:”,从第6行第1列开始显示第1个文件的内容,第13行第3列显示“file2:”第14行第1列开始显示第2个文件的内容。
#include <stdio.h>#include <stdlib.h>int main(void){ FILE *fp1,*fp2; int a[3][3]={0}; fp1 = fopen("1.txt", "r"); fp2 = fopen("2.txt", "r"); fscanf(fp1,"%d %d %d",*a,*a+1,*a+2); fscanf(fp2,"%d %d %d",*(a+1),*(a+1)+1,*(a+1)+2); fcloseall(); for(int i=0;i<15;i++){ if(i<4) { printf("\n"); continue; } if(i==4){ printf("file1:\n"); continue; } if(i==5){ printf("%d %d %d\n",a[0][0], a[0][1], a[0][2]); continue; } if(i<13) { printf("\n"); continue; } if(i==13){ printf("file2:\n"); continue; } if(i==14){ printf("%d %d %d\n",a[1][0], a[1][1], a[1][2]); continue; } } return(EXIT_SUCCESS);}
195.创建文件
从键盘中输入名称,无论创建成功与否军显示提示信息。
#include <stdio.h>#include <stdlib.h>int main(void){ FILE *fp1; char name[10]; printf("please input your file name:"); scanf("%s",name); fp1 = fopen(name,"w"); if(fp1) printf("%s create sucess\n",name); else printf("%s create failure\n",name); fcloseall(); return(EXIT_SUCCESS);}
196.格式化文件读写
将输入的小写字母写入磁盘文件,再讲磁盘文件的内容读出,并以小写字母的形式显示在屏幕上。
#include <stdio.h>#include <stdlib.h>#include <ctype.h>int main(void){ FILE *fp1; char ch,name[10]; printf("please input your file name:"); scanf("%s",name); fp1 = fopen(name,"w"); printf("please input your char:"); while((ch=getchar())!=EOF && ch != '\n'); while((ch=getchar())!=EOF && ch != '\n') fprintf(fp1,"%c",ch); fclose(fp1); fp1 = fopen(name,"r"); while(ch != 'n'){ fscanf(fp1,"%c",&ch); putchar(ch); } fcloseall(); return(EXIT_SUCCESS);}
197.成块读写操作
学生成绩信息的输入与输出,通过键盘输入学生成绩信息,保存到指定磁盘文件中,输入全部信息后,将磁盘文件中保存的信息输出到屏幕。
#include <stdio.h>#include <stdlib.h>#include <ctype.h>struct student{ int num; char name[10]; float chi; float eng; float math;};int main(void){ struct student s; char name[10]; FILE *fp1; printf("please input your filename:"); gets(name); printf("please input the information of your students:"); scanf("%d %s %f %f %f",&(s.num),s.name,&(s.chi),&(s.eng),&(s.math)); fp1 = fopen(name,"w"); fwrite(&s,sizeof(s),1,fp1); fcloseall(); printf("%d %s %f %f %f\n",s.num,s.name,s.chi,s.eng,s.math); return(EXIT_SUCCESS);}
198.随机读写文件
输入若干学生信息,保存到指定磁盘文件中,要求将奇数条学生信息从磁盘中读入并显示在屏幕上
#include <stdio.h>#include <stdlib.h>#include <ctype.h>#include <string.h>struct student{ int num; char name[10]; float chi; float eng; float math;};int main(void){ struct student s; char name[10]; FILE *fp1; int i=0; printf("please input your filename:"); gets(name); fp1 = fopen(name,"w"); if(!fp1)printf("w\n"); while(i<5){ printf("please input the information of your students:"); scanf("%d %s %f %f %f",&(s.num),s.name,&(s.chi),&(s.eng),&(s.math)); printf("%d %s %f %f %f\n",s.num,s.name,s.chi,s.eng,s.math); fwrite(&s,sizeof(student),1,fp1); i++; } fcloseall(); i=0; fp1 = fopen(name,"r"); while(i<5){ fread(&s,sizeof(student),1,fp1); fseek(fp1,sizeof(student),SEEK_CUR); printf("%d %s %f %f %f\n",s.num,s.name,s.chi,s.eng,s.math); i+=2; } fcloseall(); return(EXIT_SUCCESS);}
199.以行为单位读写文件
从键盘中输入字符串“同一个世界,同一个梦想”,要求将字符串内容输出到磁盘文件,在从磁盘文件中读取字符串到数组s,最终将其输出在屏幕上。
#include <stdio.h>#include <stdlib.h>#include <ctype.h>#include <string.h>int main(void){ char name[10]; FILE *fp1; char str[100]; printf("please input your filename:"); gets(name); fp1 = fopen(name,"w"); if(!fp1)printf("w\n"); gets(str); fprintf(fp1,"%s",str); fcloseall(); fp1 = fopen(name,"r"); fscanf(fp1,"%s",str); puts(str); fcloseall(); return(EXIT_SUCCESS);}
200.删除文件中的记录
输入文件名,输入员工姓名及工资,输入完毕显示文件中的内容,输入要删除的员工姓名,进行删除操作,最后将删除后的内容显示在屏幕上。
#include <stdio.h>#include <stdlib.h>#include <ctype.h>#include <string.h>struct student{ char name[10]; float sal;};int main(void){ struct student s; char name[10]; FILE *fp1; char ch; int i=0; printf("please input your filename:"); gets(name); fp1 = fopen(name,"w"); if(!fp1)printf("w\n"); while(i<5){ printf("please input the information of your students:"); scanf("%f %s",&(s.sal),s.name); printf("%f %s\n",s.sal,s.name); fwrite(&s,sizeof(student),1,fp1); i++; } fcloseall(); i=0; fp1 = fopen(name,"r"); printf("please input name you want to delete:"); while((ch=getchar())!='\c'); gets(name); while(i<5){ fread(&s,sizeof(student),1,fp1); if(strcmp(s.name,name)==0){ printf("%f %s\n",s.sal,s.name); fread(&s,sizeof(student),1,fp1); fseek(fp1,-2*sizeof(student),SEEK_CUR); fwrite(&s,sizeof(student),1,fp1); fseek(fp1,sizeof(student),SEEK_CUR); break; } i++; } fcloseall(); return(EXIT_SUCCESS);}
0 0
- c/c++练习–20
- C练习
- C练习
- C 练习
- c练习
- C练习
- C练习
- c 练习
- c/c++练习–03
- c/c++练习–04
- c/c++练习–05
- c/c++练习–06
- c/c++练习–07
- c/c++练习–08
- c/c++练习–09
- c/c++练习–10
- c/c++练习–11
- c/c++练习–12
- android滑动TAB页面
- python学习之编码问题
- 润乾报表在Jboss下的部署方法
- JVM
- Android ListView怎么取消item的点击效果
- c/c++练习–20
- 什么是DRM
- 谈谈为什么写单元测试
- Hibernate学习之----环境搭建
- 内嵌驱动入口简单解释
- console类型
- 基于【matplotlib】【imshow】【cmap】绘制【numpy.ndarray】二维数组的“二维码”
- eclipse中优化导入项目花费的时间
- Linux下的platform总线驱动代码分析