版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、第十三章例 13.1 程序为: #include #include void main()FILE *fp;char ch,filename10; scanf(%s,filename); if(fp=fopen(filename,w)=NULL) printf(cannot open filen); exit(0);ch=getchar(); ch=getchar(); while(ch!=#) fputc(ch,fp);putchar(ch); ch=getchar();putchar(10); fclose(fp);例 13.2 程序为: #include #include void ma
2、in()FILE *in, *out;char ch,infile10,outfile10; printf(Enter the infile name:n); scanf(%s,infile);printf(Enter the infile name:n); scanf(%s,outfile); if(in=fopen(infile,r)=NULL) printf(cannot open filen); exit(0); if(out=fopen(outfile,w)=NULL) printf(cannot open filen);exit(0);while(!feof(in) fputc(f
3、getc(in),out); fclose(in);fclose(out);注:在该程序的运行中, 每复制一次就会产生一个乱码置于最后, 并依次叠加。这不是个优 秀的程序。13.5.1 rewind 函数:#include void main()FILE *fp1,*fp2; fp1=fopen(file1.c,r); fp2=fopen(file2.c,w);while(!feof(fp1) putchar(getc(fp1); rewind(fp1);while(!feof(fp1) putc(getc(fp1),fp2); fclose(fp1);fclose(fp2);例 13.5#i
4、nclude#includestruct student_typechar name10;int num;int age;char sex;stud10;void main()int i;FILE *fp; if(fp=fopen(file22.c,rb)=NULL)printf(can not open filen); exit(0);for(i=0;i10;i+=2)fseek(fp,i*sizeof(struct student_type),0);fread(&studi,sizeof(struct student_type),1,fp);printf(%s %d %d %cn,stud
5、,studi.num,studi.age,studi.sex);fclose(fp);注:这个程序是对的,但常用于处理二进制文件, 因为文本文件要发生字符转换, 计算位置 时常发生混乱。习题13.1 对 c 文件操作有些什么特点?什么是缓冲文件系统?什么是非缓冲文件系统?这两者 的缓冲区有什么区别?答:c语言把文件看作是一个字符的序列级,即由一个一个字符的数据顺序组成。根据数据 的组织形式,可以分为: ASCII 文件(文本文件)和二进制文件。一个 c 文件是一个字节流 或二进制流(流式文件) 。它允许对文件存取一个字符,这就增加了处理的灵活性。缓冲文件系统: 是指系统自动在内存中
6、为每一个正在使用的文件开辟一个缓冲区,如果从磁盘向内存读入数据, 则一次从磁盘文件将一批数据输入到内存缓冲区, 然后再从缓冲区逐个 地将数据送到程序数据区中去。非缓冲文件系统: 是指系统不自动开辟确定大小的缓冲区,而有程序为每个文件设定缓冲区。用缓冲文件系统进行的输入输出又称高级磁盘输入输出,用非缓冲文件系统进行的输入输出又称为低级输出输入系统。 ANSI C 标准不采用非缓冲文件系统,而只采用缓冲文件系统, 即既用缓冲文件系统处理文本文件, 也用它处理二进制文件, 也就是将缓冲文件系统扩充为 可以处理二进制文件。13.2 什么是文件型指针?通过文件型指针访问文件有什么好处?答:缓冲文件系统中
7、,关键的概念是“文件指针” 。每个被使用的文件都在内存中开辟一个 区,用来存放文件的有关信息(如文件的名字,文件的状态及文件当前位置等)。这些信息是保存在一个结构体变量中的。该结构体类型是由系统定义的,取名为FILE 。这和一般使用指针变量的好处是一样的。13.3 对文件的打开和关闭的含义是什么?为什么要打开和关闭文件?答:打开的含义:带回指向al文件的指针赋给fp,这样fp就和文件al联系上了。或者说,fp 指向 a1 文件。在使用完一个文件后应该关闭它, 以防止它再被误用。 “关闭”就是文件指 针变量不指向该文件, 也就是 文件指针变量与文件 “脱钩”,此后不能再通过该指针对原来 与其联系
8、的文件进行读写操作,除非再次打开,使该指针变量重新指向该文件。13.4 程序为:#include #include void main()FILE *fp;char ch,filename10;printf(please input the name of the file:n);scanf(%s,filename);if(fp=fopen(filename,w)=NULL)printf(cannot open filen);exit(0);ch=getchar();printf(please a string:n);ch=getchar();if(a=ch&ch=z)ch=ch-32;whi
9、le(ch!=!) fputc(ch,fp);putchar(ch); ch=getchar(); if(a=ch&ch=z) ch=ch-32;putchar(10);fclose(fp);13.5 程序为:#include #include #include void main()FILE *fp;char a100;if(fp=fopen(test.c,r)=NULL)printf( 文件打开错误 ); exit(0);int i=0;ai=fgetc(fp);while(ai!=EOF)putchar(ai);i+; ai=fgetc(fp);putchar(n);fclose(fp)
10、;if(fp=fopen(test1.c,r)=NULL) printf( 文件打开错误 ); exit(0);ai=fgetc(fp);while(ai!=EOF)putchar(ai);i+;ai=fgetc(fp);ai=0;putchar(n);fclose(fp);int n=i;int k;char t;for(i=0;in-1;i+)k=i;for(int j=i+1;jaj) k=j;if(k!=i)t=ai;ai=ak;ak=t;if(fp=fopen(test2.c,w)=NULL)printf( 文件打开错误 );exit(0);i=0;while(in)fputc(ai
11、,fp); putchar(ai); i+;fclose(fp);putchar(n); 注:这个程序将处理数组的排序问题和文件读取的问题结合起来了。从而解决了这个题目。13.6 程序为:(这个程序不难,自己编的,对其中文件类型和它和结构体数组之间信息的转 换有了较深的认识)#include#define SIZE 5struct student_typeint num;char name10;float score1;float score2;float score3;float avg;studSIZE;void save()FILE *fp;int i;if(fp=fopen(stu_l
12、ist.c,wb)=NULL)printf(cannot open filen);return; for(i=0;iSIZE;i+)if(fwrite(&studi,sizeof(student_type),1,fp)!=1) printf(file write error);fclose(fp);void main()FILE *fp;int i;for(i=0;iSIZE;i+)scanf(%d%s%f%f%f,&studi.num,,&studi.score1,&studi.score2,&studi.s core3);studi.avg=(studi.score1+
13、studi.score2+studi.score3)/3;save(); fp=fopen(stu_list.c,rb);for(i=0;iSIZE;i+) fread(&studi,sizeof(student_type),1,fp);printf(%-4d %-10s %-10.2f %-10.2f %-10.2f %-10.2fn,studi.num,,studi.sc ore1,studi.score2,studi.score3,studi.avg);13.7 程序为:#include#define SIZE 5struct student_typeint num;
14、char name10;float score1;float score2;float score3;float avg;studSIZE;void save()FILE *fp;int i;if(fp=fopen(stu_sort.c,wb)=NULL)printf(cannot open filen);return;for(i=0;iSIZE;i+)if(fwrite(&studi,sizeof(student_type),1,fp)!=1) printf(file write error);fclose(fp);void main()FILE *fp;int i,j;struct stu
15、dent_type temp;fp=fopen(stu_list.c,rb);for(i=0;iSIZE;i+) fread(&studi,sizeof(student_type),1,fp);for(i=0;iSIZE-1;i+) for(j=i+1;jstudj.avg)temp=studi;studi=studj; studj=temp;save();fp=fopen(stu_sort.c,rb);for(i=0;iSIZE;i+) fread(&studi,sizeof(student_type),1,fp);printf(%-4d %-10s %-10.2f %-10.2f %-10
16、.2f %-10.2fn,studi.num,,studi.sc ore1,studi.score2,studi.score3,studi.avg);13.9 程序为:(这个题的程序要建立在前面那些程序的基础之上)#include#define SIZE 5struct student_typeint num;char name10;float score1;float score2;float score3;float avg;studSIZE+1;void save()FILE *fp;int i;if(fp=fopen(stu_sort.c,wb)=NULL)prin
17、tf(cannot open filen);return; for(i=0;iSIZE+1;i+)if(fwrite(&studi,sizeof(student_type),1,fp)!=1) printf(file write error);fclose(fp);void main()FILE *fp;int i,j;struct student_type temp,another;scanf(%d%s%f%f%f,&another.num,,&another.score1,&another.score2,&another.s core3);another.avg=(
18、another.score1+another.score2+another.score3)/3; fp=fopen(stu_list.c,ab);fwrite(&another,sizeof(student_type),1,fp);fclose(fp);fp=fopen(stu_list.c,rb);for(i=0;iSIZE+1;i+) fread(&studi,sizeof(student_type),1,fp);for(i=0;iSIZE;i+) for(j=i+1;jstudj.avg)temp=studi;studi=studj; studj=temp;fclose(fp);save
19、();fp=fopen(stu_sort.c,rb);for(i=0;iSIZE+1;i+) fread(&studi,sizeof(student_type),1,fp);printf(%-4d %-10s %-10.2f %-10.2f %-10.2f %-10.2fn,studi.num,,studi.sc ore1,studi.score2,studi.score3,studi.avg); fclose(fp);13.8 程序为:(由上题易得该程序,这里建立的新文件的名字为:students.c)#include#define SIZE 5struct studen
20、t_typeint num;char name10;float score1;float score2;float score3;float avg;studSIZE+1;void save()FILE *fp;int i;if(fp=fopen(students.c,wb)=NULL)printf(cannot open filen);return; for(i=0;iSIZE+1;i+)if(fwrite(&studi,sizeof(student_type),1,fp)!=1) printf(file write error);fclose(fp);void main()FILE *fp
21、;int i,j;struct student_type temp,another;scanf(%d%s%f%f%f,&another.num,,&another.score1,&another.score2,&another.s core3);another.avg=(another.score1+another.score2+another.score3)/3; fp=fopen(stu_list.c,ab);fwrite(&another,sizeof(student_type),1,fp); fclose(fp);fp=fopen(stu_list.c,rb);
22、 for(i=0;iSIZE+1;i+) fread(&studi,sizeof(student_type),1,fp);for(i=0;iSIZE;i+) for(j=i+1;jstudj.avg)temp=studi;studi=studj; studj=temp;fclose(fp);save();fp=fopen(students.c,rb);for(i=0;iSIZE+1;i+) fread(&studi,sizeof(student_type),1,fp);printf(%-4d %-10s %-10.2f %-10.2f %-10.2f %-10.2fn,studi.num,st
23、,studi.sc ore1,studi.score2,studi.score3,studi.avg);fclose(fp);13.10 程序为:#include#define SIZE 1struct employee_type char name10;int num;int age;char sex5;char health10;int salary;char adress10;char edu10;empSIZE;struct wage_typechar name10;int salary;emp1SIZE;void save()FILE *fp;int i; if(fp
24、=fopen(employee_list.c,wb)=NULL)printf(cannot open filen);return; for(i=0;iSIZE;i+)if(fwrite(&empi,sizeof(employee_type),1,fp)!=1) printf(file write error);fclose(fp);void save1()FILE *fp;int i;if(fp=fopen(employee_list1.c,wb)=NULL)printf(cannot open filen);return; for(i=0;iSIZE;i+) if(fwrite(&emp1i
25、,sizeof(wage_type),1,fp)!=1) printf(file write error);fclose(fp);void main()FILE *fp;int i;for(i=0;iSIZE;i+)scanf(%s%d%d%d%s%s%s%s,,&empi.num,&empi.age,&empi.salary,em pi.adress,,empi.health,empi.sex);printf(%s %d %d %d %s %s %s %sn,,empi.num,empi.age,empi.salary,e mpi.adre
26、ss,,empi.health,empi.sex); save(); fp=fopen(employee_list.c,rb);for(i=0;iSIZE;i+)fread(&empi,sizeof(employee_type),1,fp);for(int j=0;j10;j+) j=j; emp1i.salary=empi.salary;save1();for(i=0;iSIZE;i+) fread(&emp1i,sizeof(wage_type),1,fp); printf(%s %dn,,emp1i.salary)
27、;注意:这个程序的编写不难,主要是在于用 scanf 函数读入数据时的一些细节问题。13.11 这个程序在上题的基础上比较简单,只需在从文件把数据到数组时,删掉一个即可 (略)。13.12 程序为:#include void main()int i,flag;char str80,c;FILE *fp; fp=fopen(text,w);flag=1;while(flag=1)printf(Input string:n); gets(str); fprintf(fp,%s,str); printf(Continue?); c=getchar(); if(c=N)|(c=n) flag=0;ge
28、tchar();fclose(fp); fp=fopen(text,r); while(fscanf(fp,%s,str)!=EOF) for(i=0;stri!=0;i+) if(stri=a)& (stri=z) stri-=32; printf(n%sn,str);fclose(fp);注:这个程序很简单,不用费多大功夫,只需关注这个程序的风格就行了。一个笔试中常见的程序:#include/ 是指标准库中输入输出流的头文件 , cout 就定义在这个头文件里 using namespace std;int main()int count=1;label: /标记 label 标签 cou
29、tcount+ ;if(count=100)goto label;/如果count的值不大于100则转到label标签处开始执行程序 coutendl;return 0;注意:整个程序中没有for,while,do.wihle 语句,但却实现了循环。用C+编写程序,完成从键盘录入学生成绩的同时,计算班级平均成绩和及格学生平均成绩。程序为:#in elude 是指标准库中输入输出流的头文件,cout就定义在这个头文件里using namespace std;int main()float score10;float sum1=0;float sum2=0;float avg1,avg2,coun
30、t1=0;for(int i = 0;i 10;i+)cout 请输入学生成绩: scorei;sum1+=scorei;if (scorei60 ) /若 scorei 不及格则退出本次循环 continue;sum2+=scorei;count1+=1;avg1 = sum1/10;avg2 = sum2/count1;cout 班级学生平均成绩为: avg1endl;cout 及格学生平均成绩为: avg2endl;return 0;自己编的一个程序:#include /是指标准库中输入输出流的头文件 , cout 就定义在这个头文件里using namespace std;int ma
31、in()int i,renshu,n=0,m=0;float sum1=0,sum2=0,score,aver1,aver2;coutrenshu;cout 请依次输入学生成绩: endl;for(i=0;iscore;if(score=60) sum1+=score; n+;sum2+=score;m+;aver1=sum1/n;aver2=sum2/m;cout 及格学生的人数和平均成绩 :n,aver1endl 全体学生的人数和平均 成绩 :m,aver2endl;return 0;功能和上面那个程序是一样的! 基于 C+ 的简单计算器程序:#include /是指标准库中输入输出流的头
32、文件, cout 就定义在这个头文件里using namespace std;int main()double displayed_value; / 设置显示当前值变量 double new_entry; / 定义参与运算的另一个变量 char command_character; /设置命令字符变量,用来代表+、-、* 、/运算displayed_value = 0;/设置当前值为 0cout 简单计算器程序 endl endl;cout 提示后输入一个命令字符 endl;/输出提示信息cout Value : displayed_value endl; /输出当前值 cout ;cin c
33、ommand_character; /输入命令类型如 +、-、* 、/、 C、Q while (command_character != Q)/当接收 Q 命令时终止程序运行switch(command_character)/判断 switch 语句的处理命令case C:displayed_value = 0;当输入命令为C”时,表示清除命令设置当前值为0break; /转向 switch 语句的下一条语句 case +:cout ; /当输入命令为“ +”时,执行如下语句 cin new_entry;/输入一起运算的第二个数displayed_value += new_entry;/进行加
34、法运算break; /转向 switch 语句的下一条语句 case -: /当输入命令为“ -”时,执行如下语句 cout ; /输入一起运算的第二个数 cin new_entry;displayed_value -= new_entry;/进行减法运算break; /转向 switch 语句的下一条语句 case *: /当输入命令为“ *”时,执行如下语句 cout ;cin new_entry;/输入一起运算的第二个数displayed_value *= new_entry;/进行乘法运算break; /转向 switch 语句的下一条语句 case /: /当输入命令为“ /”时,执
35、行如下语句 cout ;cin new_entry;/输入一起运算的第二个数displayed_value /= new_entry;/进行除法运算break; /转向 switch 语句的下一条语句default:cout 无效输入,请重新输入命令类型 !endl;/当输入命令为其他字符时,执行如下语句cin.ignore(100,n); / 在计数值达到 100 之前忽略提取的字符/结束 switch 语句cout Value : displayed_value endl;cout ;cin command_character; /输入命令类型如 +、-、* 、/、 C、Q/结束 whil
36、e 循环语句return 0;算法举例 2求和 :#include /是指标准库中输入输出流的头文件, cout 就定义在这个头文件里using namespace std;int main()int num,sum=0;cout请输入整数,(直到输入0时结束)num;while(num!=0)sum+=num;cinnum;coutn 这些整数的总和为: sumendl;return 0;算法举例 1求和 :#include /是指标准库中输入输出流的头文件, cout 就定义在这个头文件里using namespace std;int main()int x,y,z,temp;cout 请
37、输入三个整数: xyz;if(xy)temp=y; y=x;x=temp;if(xz)temp=z;z=x;x=temp;if(yz)temp=z;z=y;y=temp;cout 把这三个数按从大到小排序后为: x y zendl; return 0;输出矩阵对角线上的元素和,程序为:#include /是指标准库中输入输出流的头文件 , cout 就定义在这个头文件里using namespace std;int main()int a44,i,j,input_a=1,sum_diag1=0,sum_diag2=0; for(i=0;i4;i+)for(j=0;j4;j+) aij=inpu
38、t_a+;for(i=0;i4;i+) for(j=0;j4;j+)coutaij ;coutendl;for(i=0;i4;i+) for(j=0;j4;j+)if(i=j) sum_diag1+=aij;if(i+j=3) sum_diag2+=aij;cout 主对角线上的元素和为: sum_diag1endl;cout 次对角线上的元素和为: sum_diag2 k=0:N; x=cos(2*N+1-2*k)*pi/(2*(N+1); y=1./(1+8*x92); c=newtonp(x,y); xx=-1:0.02:1; yy=1./(1+8*xx.A2); yy1=polyval
39、(c,xx); plot(xx,yy,k-,x,y,o) plot(xx,yy,k-,x,y,o) hold on plot(xx,yy1,b,Linewidth,1.5) % 计算 4 阶插值分析 N=8; k=0:N; x=cos(2*N+1-2*k)*pi/(2*(N+1); y=1./(1+8*x.A2); c1=newtonp(x,y); yy2=polyval(c1,xx); plot(x,y,*) plot(x,y,r*) plot(xx,yy2,m,Linewidth,1.5) % 计算 8 阶插值分析 N=10; k=0:N; x=cos(2*N+1-2*k)*pi/(2*(
40、N+1); y=1./(1+8*x.A2); c2=newtonp(x,y); yy3=polyval(c2,xx); hold on plot(x,y,d) plot(x,y,gd) hold on plot(xx,yy3,g,Linewidth,1.5) plot(xx,yy3,r,Linewidth,1.5) plot(xx,yy3,g,Linewidth,1.5) grid on title(Chebyshev Interpolation) figure plot(xx,yy1-yy,b,Linewidth,1.5),hold on plot(xx,yy2-yy,m,Linewidth
41、,1.5),hold on plot(xx,yy3-yy,g,Linewidth,1.5),hold on grid on title(The error of Chebyshev Interpolation) legend(4 阶,8阶,10 阶) %根据上面的实例结果可以看出,当前用户使用 可以十分有效的清除 Runge 现象,提高插值效率Chebyshev 方法来选择基础数据点时,函数ma in ()参数的使用程序为:#include using namespace std;int main(int argc, char* argv) int i=0; coutmain 参数 :endl
42、; coutargc=argcendl; for(i=0;iargc;i+) coutargvi=argviendl;/声明变量/输出参数的个数/遍历每个参数 输出各个参数的内容return 0;内联函数的使用的例子程序为:#include using namespace std;inline int max(int ,int);int main(int argc, char* argv)int a10,i;cout 输入 10 个数据: endl; for(i=0;iai;/ 为元素输入数据int temp=a0; /temp 保存第一个元素的值 for( i=0;i10;i+)/ 循环 1
43、0 次temp=max(temp,ai);/ 调用 max 函数cout10 个数据中的最大数为: temp=y?x:y;/返回大数C+程序(计算两个整数的最大公约数和最小公倍数)#include stdafx.h#include int main(int argc, char* argv)int gy(int n1,int n2);int gb(int n1,int n2);int n1,n2,m1,m2;cout 请依次输入两个整数: n1,n2n1n2;m1=gy(n1,n2);m2=gb(n1,n2);cout 这两个数的最大公约数是: m1endl 这两个数的最小公倍数是: m2en
44、dl;return 0;int gy(int n1,int n2)int i,z=1;for(i=2;i=n1 & iz)z=i;return z;int gb(int n1,int n2)int z;z=(n1*n2)/gy(n1,n2);return z;包含数学思想的程序:#include stdafx.h#include int leasemul;/ 定义全局变量void mul(int m,int n)int temp;/ 定义局部变量if(mn)mul(n,m);/ 函数的嵌套调用elsewhile(n!=0)/ 不为 0 则循环temp=m%n;/ 取余数 m=n;n=temp;
45、leasemul=m;/ 设置全局变量的值int divisor(int m,int n)int temp=m*n;/m 与 n 的积 temp=temp/leasemul;/ 引用全局变量 return temp;/ 返回全局变量的值int main(int argc, char* argv)int m,n;/ 定义局部变量coutmn;/ 输入两个数据mul(m,n);/ 调用函数jendl;coutm 与 n 最大公约数是: ; coutleasemulendl;/ 使用全局变量 int j=divisor(m,n);/ 调用函数求最小公倍数 coutm 与 n 最小公倍数数是: ret
46、urn 0;C+primer :1.21 第一次用类写的程序: #include stdafx.h #include #include Sales_item.h int main() Sales_item book5; for(int i=0;ibooki;for(i=0;i5;i+) std:coutbookistd:endl;return 0; 3.2 节中的例子: #include #include #include using namespace std; int main() string s(Hello world!); string:size_type punct_cnt=0;/count number of punctuation characters in s for(string:size_type inde
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 安全副社长安全生产责任制培训
- 责任回款协议书
- 质保转让协议书范本
- 购房共同还贷协议书
- 2026年安全生产月安全生产工作总结与计划模板
- 2025年电气检修安全操作规程培训
- 阑尾损伤护理查房
- 景区运营方案整体思路
- 4s站数字化运营方案
- 华为智慧机场运营方案
- 肝移植术后感染防控指南(2025版)
- 血管外科科普教育
- 急救物品管理
- 燃料电池关键材料
- 2025高考理综新疆真题试卷+参考答案
- 占道施工安全教育培训课件
- 2025年中国康养产业消费趋势报告
- 影视摄影实务课件
- 2025贵州铜仁市“千名英才·智汇铜仁”本地引才413人笔试考试备考试题及答案解析
- 山东省日照市2025-2026学年高一上学期期中校际联合考试日语试卷(含答案)
- 高处作业吊篮安装、拆卸、使用技术规程(2025版)
评论
0/150
提交评论