1、第 13 章 例 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 main()FILE*in
2、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(fgetc(in),out);fclose(i
3、n);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#include#includestruct student_type
4、char 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,studi.name,studi.num,studi.age,studi.sex);f
5、close(fp);注:这个程序是对的,但常用于处理二进制文件,因为文本文件要发生字符转换,计算位置时常发生混乱。习题13.1 对 c 文件操作有些什么特点?什么是缓冲文件系统?什么是非缓冲文件系统?这两者的缓冲区有什么区别?答:c 语言把文件看作是一个字符的序列级,即由一个一个字符的数据顺序组成。根据数据的组织形式,可以分为:ASCII 文件(文本文件)和二进制文件。一个 c 文件是一个字节流或二进制流(流式文件)。它允许对文件存取一个字符,这就增加了处理的灵活性。缓冲文件系统:是指系统自动在内存中为每一个正在使用的文件开辟一个缓冲区,如果从磁盘向内存读入数据,则一次从磁盘文件将一批数据输入
6、到内存缓冲区,然后再从缓冲区逐个地将数据送到程序数据区中去。非缓冲文件系统:是指系统不自动开辟确定大小的缓冲区,而有程序为每个文件设定缓冲区。用缓冲文件系统进行的输入输出又称高级磁盘输入输出,用非缓冲文件系统进行的输入输出又称为低级输出输入系统。ANSI C 标准不采用非缓冲文件系统,而只采用缓冲文件系统,即既用缓冲文件系统处理文本文件,也用它处理二进制文件,也就是将缓冲文件系统扩充为可以处理二进制文件。13.2 什么是文件型指针?通过文件型指针访问文件有什么好处?答:缓冲文件系统中,关键的概念是“文件指针”。每个被使用的文件都在内存中开辟一个区,用来存放文件的有关信息(如文件的名字,文件的状
7、态及文件当前位置等)。这些信息是保存在一个结构体变量中的。该结构体类型是由系统定义的,取名为 FILE。这和一般使用指针变量的好处是一样的。13.3 对文件的打开和关闭的含义是什么?为什么要打开和关闭文件?答:打开的含义:带回指向 a1 文件的指针赋给 fp,这样 fp 就和文件 a1 联系上了。或者说,fp 指向 a1 文件。在使用完一个文件后应该关闭它,以防止它再被误用。“关闭”就是文件指针变量不指向该文件,也就是 文件指针变量与文件“脱钩”,此后不能再通过该指针对原来与其联系的文件进行读写操作,除非再次打开,使该指针变量重新指向该文件。13.4 程序为:#include#include
8、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;while(ch!=!)fputc(ch,fp);putchar(ch);ch=getchar();if(a=ch&ch=z
9、)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);if(fp=fopen(test1.c,r)=NULL)printf(文件打开错误);exit(0);ai=fgetc(fp);while
10、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,fp);putchar(ai);i+;fclose(fp);putchar(n);注:这个程序将处理数组的排序问题和文件读取的问题结合起来了。从而解决了
11、这个题目。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_list.c,wb)=NULL)printf(cannot open filen);return;for(i=0;iSIZE;i+)if(fwrite(&studi,
12、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.name,&studi.score1,&studi.score2,&studi.score3);studi.avg=(studi.score1+studi.score2+studi.score3)/3;save();fp=fopen(stu_list.c,rb);for(i=0;iSIZE;i+)fread(&st
13、udi,sizeof(student_type),1,fp);printf(%-4d%-10s%-10.2f%-10.2f%-10.2f%-10.2fn,studi.num,studi.name,studi.score1,studi.score2,studi.score3,studi.avg);13.7 程序为:#include#define SIZE 5struct student_typeint num;char name10;float score1;float score2;float score3;float avg;studSIZE;void save()FILE*fp;int i
14、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 student_type temp;fp=fopen(stu_list.c,rb);for(i=0;iSIZE;i+)fread(&studi,sizeof(student_type),1,fp);f
15、or(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.2f%-10.2fn,studi.num,studi.name,studi.score1,studi.score2,studi.score3,studi.avg);13.9 程序为:(这个题的程序要建立在前面
16、那些程序的基础之上)#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)printf(cannot open filen);return;for(i=0;iSIZE+1;i+)if(fwrite(&studi,sizeof(student_type),1,fp)!=1)printf(file w
17、rite error);fclose(fp);void main()FILE*fp;int i,j;struct student_type temp,another;scanf(%d%s%f%f%f,&another.num,another.name,&another.score1,&another.score2,&another.score3);another.avg=(another.score1+another.score2+another.score3)/3;fp=fopen(stu_list.c,ab);fwrite(&another,sizeof(student_type),1,f
18、p);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();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
19、10.2f%-10.2fn,studi.num,studi.name,studi.score1,studi.score2,studi.score3,studi.avg);fclose(fp);13.8 程序为:(由上题易得该程序,这里建立的新文件的名字为:students.c)#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(s
20、tudents.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;int i,j;struct student_type temp,another;scanf(%d%s%f%f%f,&another.num,another.name,&another.score1,&another.score2,&another.sc
21、ore3);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);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;
22、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,studi.name,studi.score1,studi.score2,studi.score3,studi.avg);fclose(fp);13.10 程序为:#include#define SIZE 1struct employee_type char name10;int nu
23、m;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=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 wri
24、te 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,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.name,&empi.num,
25、empi.age,&empi.salary,empi.adress,empi.edu,empi.health,empi.sex);printf(%s%d%d%d%s%s%s%sn,empi.name,empi.num,empi.age,empi.salary,empi.adress,empi.edu,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+)emp1i.namej=em
26、pi.namej;emp1i.salary=empi.salary;save1();for(i=0;iSIZE;i+)fread(&emp1i,sizeof(wage_type),1,fp);printf(%s%dn,emp1i.name,emp1i.salary);注意:这个程序的编写不难,主要是在于用 scanf 函数读入数据时的一些细节问题。13.11 这个程序在上题的基础上比较简单,只需在从文件把数据到数组时,删掉一个即可(略)。13.12 程序为:#includevoid main()int i,flag;char str80,c;FILE*fp;fp=fopen(text,w);f
27、lag=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;getchar();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);注:这个程序很简单,不用费多大功夫,只需关注这个程序的风格就行了。一个笔试
28、中常见的程序:#include/是指标准库中输入输出流的头文件,cout 就定义在这个头文件里using namespace std;int main()int count=1;label:/标记 label 标签coutcount+;if(count=100)goto label;/如果 count 的值不大于 100 则转到 1abel 标签处开始执行程序coutendl;return 0;注意:整个程序中没有 for,while,do.wihle 语句,但却实现了循环。用 C+编写程序,完成从键盘录入学生成绩的同时,计算班级平均成绩和及格学生平均成绩。程序为:#include/是指标准库
29、中输入输出流的头文件,cout 就定义在这个头文件里using namespace std;int main()float score10;float sum1=0;float sum2=0;float avg1,avg2,count1=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及格学生平均
30、成绩为:avg2endl;return 0;自己编的一个程序:#include/是指标准库中输入输出流的头文件,cout 就定义在这个头文件里using namespace std;int main()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,aver1end
31、l全体学生的人数和平均成绩:m,aver2endl;return 0;功能和上面那个程序是一样的!基于 C+的简单计算器程序:#include/是指标准库中输入输出流的头文件,cout 就定义在这个头文件里using namespace std;int main()double displayed_value;/设置显示当前值变量 double new_entry;/定义参与运算的另一个变量 char command_character;/设置命令字符变量,用来代表+、-、*、/运算 displayed_value=0;/设置当前值为 0 cout 简单计算器程序 endl-endl;cout
32、 提示后输入一个命令字符 endl;/输出提示信息 cout Value:displayed_value endl;/输出当前值 cout;cin command_character;/输入命令类型如+、-、*、/、C、Q while(command_character!=Q)/当接收 Q 命令时终止程序运行 switch(command_character)/判断 switch 语句的处理命令 case C:displayed_value=0;/当输入命令为“C”时,表示清除命令设置当前值为 0 break;/转向 switch 语句的下一条语句 case+:cout;/当输入命令为“+”时
33、执行如下语句 cin new_entry;/输入一起运算的第二个数 displayed_value+=new_entry;/进行加法运算 break;/转向 switch 语句的下一条语句 case-:/当输入命令为“-”时,执行如下语句 cout;/输入一起运算的第二个数 cin new_entry;displayed_value-=new_entry;/进行减法运算 break;/转向 switch 语句的下一条语句 case*:/当输入命令为“*”时,执行如下语句 cout;cin new_entry;/输入一起运算的第二个数 displayed_value*=new_entry;/进
34、行乘法运算 break;/转向 switch 语句的下一条语句 case/:/当输入命令为“/”时,执行如下语句 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 com
35、mand_character;/输入命令类型如+、-、*、/、C、Q/结束 while 循环语句 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 namespa
36、ce std;int main()int x,y,z,temp;cout请输入三个整数: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;
37、i4;i+)for(j=0;j4;j+)aij=input_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*x.2);c=newtonp(x,y);xx=-1:0.02:1;yy=1./(1+8*xx.2)
38、yy1=polyval(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.2);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*(N+1);y
39、1./(1+8*x.2);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,1.5),hold on plo
40、t(xx,yy3-yy,g,Linewidth,1.5),hold on grid on title(The error of Chebyshev Interpolation)legend(4 阶,8 阶,10 阶)%根据上面的实例结果可以看出,当前用户使用 Chebyshev 方法来选择基础数据点时,可以十分有效的清除 Runge 现象,提高插值效率函数 main()参数的使用程序为:#include using namespace std;int main(int argc,char*argv)int i=0;/声明变量coutmain 参数:endl;coutargc=argcendl;
41、/输出参数的个数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+)/循环 10 次temp=max(temp,ai);/调
42、用 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这两个数的最小公倍数是:m2endl;return 0;int gy(int n1,int n2)
43、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);/函数的嵌套调用else while(n!=0)/不为 0 则循环temp=m%n;/取余数m=n;n=temp;leasemul=m;/设置全局变量的值int divisor(int m,in
44、t 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);/调用函数 coutm与n最大公约数是:;coutleasemulendl;/使用全局变量 int j=divisor(m,n);/调用函数求最小公倍数 coutm与n最小公倍数数是:jendl;return 0;C+primer:1.21 第一次用类写的程序:#include stdafx.h#include#in
45、clude Sales_item.hint 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 sfor(string:size_type index=0;i
46、ndex!=s.size();+index)if(ispunct(sindex)+punct_cnt;coutpunct_cnt punctuation characters in sendl;/convert s to lowercasefor(index=0;index!=s.size();+index)sindex=toupper(sindex);coutsendl;return 0;3.13 的程序为:#include/#include#include/#include using namespace std;int main()vector ivec;int ival;coutPle
47、ase enter some integrated numbers(end to Ctrl+Z):ival)ivec.push_back(ival);if(ivec.size()=0)coutNo element?!endl;return-1;coutSum of each pair of counterpart elements in the vector is:endl;for(vector:size_type cnt1=0,cnt2=ivec.size()-1;cnt1ivec.size()/2;cnt1+,cnt2-)coutiveccnt1+iveccnt2;if(ivec.size
48、)%2=1)coutendlThe middle number has not been summed and its value is:ivecivec.size()/2endl;coutendl;return 0;习题 3.17 的程序:#include#include#include#include using namespace std;int main()vector svec;string str;coutEnter numbers(end to Ctrl+Z)str)svec.push_back(str);if(svec.size()=0)coutNo elements?!en
49、dl;return-1;coutTransformed elements in the vector are:endl;vector:size_type cnt=0;for(vector:iterator iter=svec.begin();itersvec.end();+iter)for(string:size_type index=0;index(*iter).size();index+)if(islower(*iter)index)(*iter)index=toupper(*iter)index);cout*iter;cnt+;if(cnt%8=0)coutendl;coutendl;r
50、eturn 0;习题 5.18 答案#include/#include#include/#include#include/#include/#include using namespace std;int main()vector spvec;string str;coutEnter strings:(Ctrl+Z to end)str)string*pstr=new string;*pstr=str;spvec.push_back(pstr);vector:iterator iter=spvec.begin();while(iter!=spvec.end()size_t k=(*iter)-






