




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、第13章例13.1 程序为:#include <stdio.h>#include <stdlib.h>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=getc
2、har();putchar(10);fclose(fp);例13.2 程序为:#include <stdio.h>#include <stdlib.h>void main()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
3、(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(in);fclose(out);注:在该程序的运行中,每复制一次就会产生一个乱码置于最后,并依次叠加。这不是个优秀的程序。13.5.1 rewind函数:#include <stdi
4、o.h>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<stdlib.h>#include<stdio.h>struct student_t
5、ypechar 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;i<10;i+=2) fseek(fp,i*sizeof(struct student_type),0);fread(&studi,sizeof(struct student_type),1,fp);printf(&quo
6、t;%s %d %d %cn",,studi.num,studi.age,studi.sex);fclose(fp);注:这个程序是对的,但常用于处理二进制文件,因为文本文件要发生字符转换,计算位置时常发生混乱。习题13.1 对c文件操作有些什么特点?什么是缓冲文件系统?什么是非缓冲文件系统?这两者的缓冲区有什么区别?答:c语言把文件看作是一个字符的序列级,即由一个一个字符的数据顺序组成。根据数据的组织形式,可以分为:ASCII文件(文本文件)和二进制文件。一个c文件是一个字节流或二进制流(流式文件)。它允许对文件存取一个字符,这就增加了处理的灵活性。缓冲文件系统
7、:是指系统自动在内存中为每一个正在使用的文件开辟一个缓冲区,如果从磁盘向内存读入数据,则一次从磁盘文件将一批数据输入到内存缓冲区,然后再从缓冲区逐个地将数据送到程序数据区中去。非缓冲文件系统:是指系统不自动开辟确定大小的缓冲区,而有程序为每个文件设定缓冲区。用缓冲文件系统进行的输入输出又称高级磁盘输入输出,用非缓冲文件系统进行的输入输出又称为低级输出输入系统。ANSI C标准不采用非缓冲文件系统,而只采用缓冲文件系统,即既用缓冲文件系统处理文本文件,也用它处理二进制文件,也就是将缓冲文件系统扩充为可以处理二进制文件。13.2 什么是文件型指针?通过文件型指针访问文件有什么好处?答:缓冲文件系统
8、中,关键的概念是“文件指针”。每个被使用的文件都在内存中开辟一个区,用来存放文件的有关信息(如文件的名字,文件的状态及文件当前位置等)。这些信息是保存在一个结构体变量中的。该结构体类型是由系统定义的,取名为FILE。这和一般使用指针变量的好处是一样的。13.3 对文件的打开和关闭的含义是什么?为什么要打开和关闭文件?答:打开的含义:带回指向a1文件的指针赋给fp,这样fp就和文件a1联系上了。或者说,fp指向a1文件。在使用完一个文件后应该关闭它,以防止它再被误用。“关闭”就是文件指针变量不指向该文件,也就是 文件指针变量与文件“脱钩”,此后不能再通过该指针对原来与其联系的文件进行读写操作,除
9、非再次打开,使该指针变量重新指向该文件。13.4 程序为:#include <stdio.h>#include <stdlib.h>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=getcha
10、r();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')ch=ch-32;putchar(10);fclose(fp);13.5 程序为:#include <stdio.h>#include &
11、lt;stdlib.h>#include <string.h>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"
12、;)=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;i<n-1;i+)k=i;for(int j=i+1;j<n;j+)if(ak>aj) k=j;if(k!=i)t=ai;ai=ak;ak=t;if(fp=fopen("test2.c","w"
13、)=NULL)printf("文件打开错误");exit(0);i=0;while(i<n) fputc(ai,fp);putchar(ai);i+; fclose(fp); putchar('n');注:这个程序将处理数组的排序问题和文件读取的问题结合起来了。从而解决了这个题目。13.6 程序为:(这个程序不难,自己编的,对其中文件类型和它和结构体数组之间信息的转换有了较深的认识)#include<stdio.h>#define SIZE 5struct student_typeint num;char name10;float scor
14、e1;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;i<SIZE;i+)if(fwrite(&studi,sizeof(student_type),1,fp)!=1)printf("file write error"); fclose(fp);void
15、 main() FILE *fp;int i; for(i=0;i<SIZE;i+) scanf("%d%s%f%f%f",&studi.num,,&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;i<SIZE;i+) fread(&stu
16、di,sizeof(student_type),1,fp);printf("%-4d %-10s %-10.2f %-10.2f %-10.2f %-10.2fn",studi.num,,studi.score1,studi.score2,studi.score3,studi.avg);13.7 程序为:#include<stdio.h>#define SIZE 5struct student_typeint num;char name10;float score1;float score2;float score3;float avg;st
17、udSIZE;void save()FILE *fp;int i;if(fp=fopen("stu_sort.c","wb")=NULL)printf("cannot open filen");return;for(i=0;i<SIZE;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_t
18、ype temp;fp=fopen("stu_list.c","rb"); for(i=0;i<SIZE;i+) fread(&studi,sizeof(student_type),1,fp);for(i=0;i<SIZE-1;i+)for(j=i+1;j<SIZE;j+)if(studi.avg>studj.avg)temp=studi;studi=studj;studj=temp;save();fp=fopen("stu_sort.c","rb");for(i=0;i<SI
19、ZE;i+) fread(&studi,sizeof(student_type),1,fp);printf("%-4d %-10s %-10.2f %-10.2f %-10.2f %-10.2fn",studi.num,,studi.score1,studi.score2,studi.score3,studi.avg);13.9程序为:(这个题的程序要建立在前面那些程序的基础之上)#include<stdio.h>#define SIZE 5struct student_typeint num;char name10;float sc
20、ore1;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;i<SIZE+1;i+)if(fwrite(&studi,sizeof(student_type),1,fp)!=1)printf("file write error"); fclose(fp
21、);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.score3);another.avg=(another.score1+another.score2+another.score3)/3; fp=fopen("stu_list.c","ab"); fwri
22、te(&another,sizeof(student_type),1,fp);fclose(fp);fp=fopen("stu_list.c","rb"); for(i=0;i<SIZE+1;i+) fread(&studi,sizeof(student_type),1,fp);for(i=0;i<SIZE;i+)for(j=i+1;j<SIZE+1;j+)if(studi.avg>studj.avg)temp=studi;studi=studj;studj=temp;fclose(fp);save();fp=fo
23、pen("stu_sort.c","rb");for(i=0;i<SIZE+1;i+) fread(&studi,sizeof(student_type),1,fp);printf("%-4d %-10s %-10.2f %-10.2f %-10.2f %-10.2fn",studi.num,,studi.score1,studi.score2,studi.score3,studi.avg);fclose(fp);13.8 程序为:(由上题易得该程序,这里建立的新文件的名字为:students.c)#
24、include<stdio.h>#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("students.c","wb")=NULL)printf("cannot open filen");return;for(i=0;i<SIZE+1;i+)if(fwrite(&
25、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.score3);another.avg=(another.score1+anot
26、her.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;i<SIZE+1;i+) fread(&studi,sizeof(student_type),1,fp);for(i=0;i<SIZE;i+)for(j=i+1;j<SIZE+1;j+)
27、if(studi.avg>studj.avg)temp=studi;studi=studj;studj=temp;fclose(fp);save();fp=fopen("students.c","rb");for(i=0;i<SIZE+1;i+) fread(&studi,sizeof(student_type),1,fp);printf("%-4d %-10s %-10.2f %-10.2f %-10.2f %-10.2fn",studi.num,,studi.score1,studi.sco
28、re2,studi.score3,studi.avg);fclose(fp);13.10 程序为:#include<stdio.h>#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=fopen("emp
29、loyee_list.c","wb")=NULL)printf("cannot open filen");return;for(i=0;i<SIZE;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)print
30、f("cannot open filen");return;for(i=0;i<SIZE;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;i<SIZE;i+) scanf("%s%d%d%d%s%s%s%s",,&empi.num,&empi.age,&empi.salary,em
31、pi.adress,,empi.health,empi.sex);printf("%s %d %d %d %s %s %s %sn",,empi.num,empi.age,empi.salary,empi.adress,,empi.health,empi.sex); save();fp=fopen("employee_list.c","rb");for(i=0;i<SIZE;i+) fread(&empi,sizeof(employee_type),1,fp);for(i
32、nt j=0;j<10;j+) j=j;emp1i.salary=empi.salary; save1(); for(i=0;i<SIZE;i+) fread(&emp1i,sizeof(wage_type),1,fp);printf("%s %dn",,emp1i.salary);注意:这个程序的编写不难,主要是在于用scanf函数读入数据时的一些细节问题。13.11 这个程序在上题的基础上比较简单,只需在从文件把数据到数组时,删掉一个即可(略)。13.12 程序为:#include<st
33、dio.h>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; getchar(); fclos
34、e(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<iostream>/是指标
35、准库中输入输出流的头文件, cout就定义在这个头文件里using namespace std;int main()int count=1; label: /标记label标签cout<<count+<<" "if(count<=100)goto label;/如果count的值不大于100则转到1abel标签处开始执行程序cout<<endl;return 0;注意:整个程序中没有for,while,do.wihle语句,但却实现了循环。用C+编写程序,完成从键盘录入学生成绩的同时,计算班级平均成绩和及格学生平均成绩。程序为:#i
36、nclude <iostream>/是指标准库中输入输出流的头文件, 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<<"请输入学生成绩:"<<endl; cin>>scorei; sum1+=scorei; if (scorei<60 ) /若scorei不及格则退出本次循环 c
37、ontinue; sum2+=scorei; count1+=1; avg1 = sum1/10; avg2 = sum2/count1; cout<<"班级学生平均成绩为:"<<avg1<<endl; cout<<"及格学生平均成绩为:"<<avg2<<endl; return 0;自己编的一个程序:#include <iostream>/是指标准库中输入输出流的头文件, cout就定义在这个头文件里using namespace std;int main()int i
38、,renshu,n=0,m=0; float sum1=0,sum2=0,score,aver1,aver2;cout<<"请输入学生人数:"cin>>renshu; cout<<"请依次输入学生成绩:"<<endl;for(i=0;i<renshu;i+)cin>>score;if(score>=60)sum1+=score;n+;sum2+=score;m+;aver1=sum1/n;aver2=sum2/m;cout<<"及格学生的人数和平均成绩:&quo
39、t;<<n<<","<<aver1<<endl<<"全体学生的人数和平均成绩:"<<m<<","<<aver2<<endl; return 0;功能和上面那个程序是一样的!基于C+的简单计算器程序:#include <iostream>/是指标准库中输入输出流的头文件, cout就定义在这个头文件里using namespace std;int main()double displayed_value; /设置显示当
40、前值变量 double new_entry; /定义参与运算的另一个变量 char command_character; /设置命令字符变量,用来代表+、-、*、/运算 displayed_value = 0; /设置当前值为0 cout << "简单计算器程序" << endl<< "-" << endl; cout << "在'>' 提示后输入一个命令字符"<< endl; /输出提示信息 cout << "Valu
41、e : " << displayed_value << endl; /输出当前值 cout << "command>" cin >> command_character; /输入命令类型如+、-、*、/、C、Q while (command_character != 'Q')/当接收Q命令时终止程序运行 switch(command_character)/判断switch语句的处理命令 case 'C': displayed_value = 0; /当输入命令为“C”时,表示清除
42、命令设置当前值为0 break; /转向switch语句的下一条语句 case '+': cout << "number>" /当输入命令为“+”时,执行如下语句 cin >> new_entry; /输入一起运算的第二个数 displayed_value += new_entry; /进行加法运算 break; /转向switch语句的下一条语句 case '-': /当输入命令为“-”时,执行如下语句 cout << "number>"/输入一起运算的第二个数 cin &
43、gt;> new_entry; displayed_value -= new_entry; /进行减法运算 break; /转向switch语句的下一条语句 case '*': /当输入命令为“*”时,执行如下语句 cout << "number>" cin >> new_entry; /输入一起运算的第二个数 displayed_value *= new_entry; /进行乘法运算 break; /转向switch语句的下一条语句 case '/': /当输入命令为“/”时,执行如下语句 cout &l
44、t;< "number>" cin >> new_entry; /输入一起运算的第二个数 displayed_value /= new_entry; /进行除法运算 break; /转向switch语句的下一条语句 default: cout << "无效输入,请重新输入命令类型!"<<endl; /当输入命令为其他字符时,执行如下语句 cin.ignore(100,'n');/在计数值达到100之前忽略提取的字符 /结束switch语句 cout << "Value
45、: " << displayed_value << endl; cout << "command>" cin >> command_character; /输入命令类型如+、-、*、/、C、Q /结束while循环语句 return 0;算法举例2求和:#include <iostream>/是指标准库中输入输出流的头文件, cout就定义在这个头文件里using namespace std;int main()int num,sum=0;cout<<"请输入整数,(直到输入0
46、时结束)"<<endl;cin>>num;while(num!=0)sum+=num;cin>>num;cout<<"n这些整数的总和为:"<<sum<<endl; return 0; 算法举例1求和:#include <iostream>/是指标准库中输入输出流的头文件, cout就定义在这个头文件里using namespace std;int main()int x,y,z,temp;cout<<"请输入三个整数:"<<endl;c
47、in>>x>>y>>z;if(x<y)temp=y;y=x;x=temp;if(x<z)temp=z;z=x;x=temp;if(y<z)temp=z;z=y;y=temp;cout<<"把这三个数按从大到小排序后为:"<<x<<" "<<y<<" "<<z<<endl; return 0;输出矩阵对角线上的元素和,程序为:#include <iostream> /是指标准库中输入输出流
48、的头文件, cout就定义在这个头文件里using namespace std;int main()int a44,i,j,input_a=1,sum_diag1=0,sum_diag2=0;for(i=0;i<4;i+)for(j=0;j<4;j+)aij=input_a+; for(i=0;i<4;i+)for(j=0;j<4;j+)cout<<aij<<" "cout<<endl; for(i=0;i<4;i+)for(j=0;j<4;j+)if(i=j)sum_diag1+=aij;if(i+j
49、=3)sum_diag2+=aij;cout<<"主对角线上的元素和为:"<<sum_diag1<<endl;cout<<"次对角线上的元素和为:"<<sum_diag2<<endl; return 0;矩阵为:1 2 3 45 6 7 89 10 11 1213 14 15 16MATLAB程序:用来清除插值分析中龙格现象的方法(chebyshev方法)程序为: N=4;>> k=0:N;>> x=cos(2*N+1-2*k)*pi/(2*(N+1);>
50、;> y=1./(1+8*x.2);>> c=newtonp(x,y);>> xx=-1:0.02:1;>> yy=1./(1+8*xx.2);>> 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)>>
51、%计算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=1
52、0;>> k=0:N;>> x=cos(2*N+1-2*k)*pi/(2*(N+1);>> y=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,y
53、y3,'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',
54、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阶')>> %根据上面的实例结果可以看出,当前用户使用Chebyshev方法来选择基础数据点时,可以十分有效的清除Runge现象,提高插值效率函数main()参数的使
55、用程序为:#include <iostream>using namespace std;int main(int argc, char* argv)int i=0; /声明变量cout<<"main参数:"<<endl;cout<<"argc="<<argc<<endl; /输出参数的个数for(i=0;i<argc;i+) /遍历每个参数cout<<"argv"<<i<<"="<<argv
56、i<<endl;/输出各个参数的内容return 0;内联函数的使用的例子程序为:#include <iostream>using namespace std;inline int max(int ,int); /声明内联函数int main(int argc, char* argv)int a10,i; /声明数组和变量cout<<"输入10个数据:"<<endl;for(i=0;i<10;i+) /循环10次cin>>ai; /为元素输入数据int temp=a0; /temp保存第一个元素的值for(
57、i=0;i<10;i+) /循环10次temp=max(temp,ai); /调用max函数cout<<"10个数据中的最大数为:"<<temp<<endl;return 0;inline int max(int x,int y) /即使没有inline,仍然视为内联函数return x>=y?x:y; /返回大数C+程序(计算两个整数的最大公约数和最小公倍数)#include "stdafx.h"#include <iostream.h>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,n2"&
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 解析卷安徽省天长市七年级上册有理数及其运算单元测评试题(含解析)
- 医院感染管理应知应会考试试题(附答案)
- 耳鼻喉口腔颌面外科院感试题(附答案)
- 2025年生态修复中生物多样性保护与生态修复工程可持续发展研究报告
- 2025年老年健康管理领域长期照护服务模式政策法规解读报告
- 2025年海洋生态修复项目环境影响评价报告
- 2025年文化娱乐行业消费者行为分析:细分市场细分与用户体验报告
- 2025年工业互联网平台射频识别(RFID)在图书管理系统的应用与效率提升报告
- 2025至2030年中国猫粮市场竞争格局及投资战略规划报告
- 诉讼和解协议书示例
- 2025年高考真题语文全国一卷试题试卷及参考答案
- (高清版)DB1301∕T 345-2020 优 质冬小麦石农952栽培技术规程
- 乡土中国读书分享课件
- 2025年反假币知识竞赛题库及答案
- 餐饮技术入干股协议书
- 教科版五年级上册科学教学计划附进度表
- 水果和蔬菜深加工技术项目可行性研究报告
- 老年患者体位护理
- 2025新译林版英语八上单词默写表(先鸟版)
- 2025年执业医师考试临床技能试题及答案
- 洽谈互赔协议书
评论
0/150
提交评论