版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、程序设计技术(第三版)习题参考答案及说明习题9单项选择题ADBBB CCCDC填空题显示器w指定文件内部记录指针移动的起始位置sizeof(student)阅读程序题younk文件拷贝1 3 5 7 9Name: Lilei Phone:123456 Score:100AAA18程序设计题/xt090401.cpp#include#includevoid main()struct personchar name20;char sex;int age;float height;person1;FILE *fp;char fname20;int i;printf(please input a fi
2、le name:n);scanf(%s,fname);if(fp=fopen(fname,wb)=NULL)printf(cannot open this file!n);exit(1);printf(please input three persons name,sex,age and height:n);for(i=0;i3;i+)scanf(%s %c%d%f,,&person1.sex,&person1.age,&person1.height);if(fwrite(&person1,sizeof(person1),1,fp)!=1)printf(write er
3、ror!n);exit(2);fclose(fp);if(fp=fopen(fname,rb)=NULL)printf(cannot open this file!n);exit(3);printf(nthe content of the file is:nNamettSextAgetHeightn);while(!feof(fp)if(fread(&person1,sizeof(person1),1,fp)=1)printf(%-16s%-8c%-8d%-10.2fn,,person1.sex,person1.age,person1.height);fclose(fp
4、);exit(0);/xt090402.cpp#include#include#includevoid main()int i;FILE *fp;if(fp=fopen(result.txt,w)=NULL)puts(OPen file failed!);exit(1);double result=0.0;for(i=0;i=32;i+)result=cos(double)(i*2)/32);printf(%lfn,result);fprintf(fp,%9.6f,result);fclose(fp);/xt090403.cpp#include#includevoid main()FILE *
5、fp;int i,j,n,ni;char c160,t,ch;if(fp=fopen(file1,r)=NULL)printf(file 1 cannot be openedn);exit(1);printf(n A contents are :n);for(i=0;(ch=fgetc(fp)!=EOF;i+)ci=ch;putchar(ci);fclose(fp);ni=i;if(fp=fopen(file2,r)=NULL)printf(file 2 cannot be openedn);exit(2);printf(n B contents are :n);for(;(ch=fgetc(
6、fp)!=EOF;i+)ci=ch;putchar(ci);fclose(fp);n=i;for(i=0;in;i+)for(j=i+1;jcj)t=ci;ci=cj;cj=t;if(fp=fopen(file3,w)=NULL)printf(file 3 cannot be openedn);exit(3);for(i=0;in;i+)putc(ci,fp);putchar(ci);fclose(fp);exit(0);/xt090404.cpp#include#include#includevoid main()FILE *fp;char str100;int i=0;if(fp=fope
7、n(test,w)=NULL)printf(cannot open the filen);exit(1);printf(please input a string:n);gets(str);while(stri!=0)if(stri=a&stri=z)stri=stri-32;fputc(stri,fp);i+;fclose(fp);fp=fopen(test,r);fgets(str,strlen(str)+1,fp);printf(%sn,str);fclose(fp);/xt090405.cpp#includeint main(int argc, char* argv)char file
8、name20;FILE *fpt;printf(Input filename:n); gets(filename);if(fpt=fopen(filename,w)=NULL)printf(Can not Open File!);return 1;char ch;ch=getchar();while(ch!=#)fputc(ch,fpt);ch=getchar();return 0;/xt090406.cpp#includechar Line2000;void clearLine()Line0=0;int getMaxLineNum(char filename20)int LineMax=0,
9、Count=0,LineNum=0,LenMaxLine=0;char ch;FILE *fpt;if(fpt=fopen(filename,r)=NULL)printf(Can not Open File!);return 1;ch=fgetc(fpt);while(ch!=EOF)putchar(ch);if(ch=10)if(LineMaxCount)LineMax=Count;LenMaxLine=LineNum;Count=0;LineNum+;elseCount+;ch=fgetc(fpt);fclose(fpt);return LenMaxLine;int printline(c
10、har filename,int line)int LineNum=0;char ch;FILE *fpt;if(fpt=fopen(filename,r)=NULL)printf(Can not Open File!);return 1;ch=fgetc(fpt);while(ch!=EOF)if(LineNumline)ch=fgetc(fpt);if(ch=10)LineNum+;if(LineNum=line)ch=fgetc(fpt);putchar(ch);if(ch=10)break;fclose(fpt);return 0;int main(int argc, char* ar
11、gv)char filename200;printf(Input filename:n);gets(filename);int linenum=0;linenum=getMaxLineNum(filename);printf(LineNum %dn,linenum); printline(filename,linenum);return 0;/xt090407.cpp#include struct Studentchar name20;char phone20;int score;struct Student inputStudent()struct Student Stu;printf(Na
12、me:n);gets(S);printf(Phone:n);gets(Stu.phone);printf(Score:n);scanf(%d,&Stu.score);getchar();return Stu;int writeStus(struct Student Stus,int num)char filename=class.txt;FILE *fpt;if(fpt=fopen(filename,w)=NULL)printf(Can not Open File!);return 1;fwrite(Stus,sizeof(struct Student),num,fpt);fcl
13、ose(fpt);return 0;void write()struct Student Stus5;for(int i=0;i5;i+)printf(Input Infomation of the %dth Student:n,i);Stusi=inputStudent(); writeStus(Stus,5);void main(int argc, char* argv)write();/xt090408.cpp#includestruct Studentchar name20;char phone20;int score;void outputStudent(struct Student
14、 Stu)printf(Name:%sn,S);printf(Phone:%sn,Stu.phone);printf(Score:%5dn,Stu.score);int readStudent(struct Student *stu,int num)char filename=class.txt;FILE *fpt;if(fpt=fopen(filename,r)=NULL)printf(Can not Open File!);return 1;fseek(fpt,sizeof(struct Student)*num,SEEK_SET);fread(stu,sizeof(stru
15、ct Student),1,fpt);fclose(fpt);return 0;void read()struct Student Stu;readStudent(&Stu,2);outputStudent(Stu);void main(int argc, char* argv)read();/xt090409.cpp#include#includestruct Studentchar name20;char phone20;int score;int readStudentArr(struct Student Stus,int num)char filename=class.txt;FILE
16、 *fpt;if(fpt=fopen(filename,r)=NULL)printf(Can not Open File!);return 1;fread(Stus,sizeof(struct Student),num,fpt);fclose(fpt);return 0;int writeStudentArr(struct Student Stus,int num)char filename=class.txt;FILE *fpt;if(fpt=fopen(filename,w)=NULL)printf(Can not Open File!);return 1;fwrite(Stus,size
17、of(struct Student),num,fpt);fclose(fpt);return 0;int SwapSudent(struct Student *StuOne,struct Student *StuTwo)struct Student StuTemp;StuTemp=*StuOne;*StuOne=*StuTwo;*StuTwo=StuTemp;return 0;void bubble(struct Student Stus,int num)for(int i=0;inum;i+)for(int j=0;j0)SwapSudent(&Stusj,&Stusj+1);void sh
18、owdata(struct Student Stus,int num)printf(Namet Phonet Scoretn);for(int i=0;inum;i+)printf(%s %s %8d n,S,Stusi.phone,Stusi.score);void restoreStudentArr()struct Student Stus5;readStudentArr(Stus,5);bubble(Stus,5);showdata(Stus,5);writeStudentArr(Stus,5);int main(int argc, char* argv)restore
19、StudentArr();return 0;/xt090410.cpp#include#includestruct Studentchar name20;char phone20;int score;int readStudentArr(struct Student Stus,int num)char filename=class.txt;FILE *fpt;if(fpt=fopen(filename,r)=NULL)printf(Can not Open File!);return 1;fread(Stus,sizeof(struct Student),num,fpt);fclose(fpt
20、);return 0;int writeStudentArr(struct Student Stus,int num)char filename=class.txt;FILE *fpt;if(fpt=fopen(filename,w)=NULL)printf(Can not Open File!);return 1;fwrite(Stus,sizeof(struct Student),num,fpt);fclose(fpt);return 0;int SwapSudent(struct Student *StuOne,struct Student *StuTwo)struct Student StuTemp;StuT
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论