課用習(xí)題參考答案第09章_第1頁
課用習(xí)題參考答案第09章_第2頁
課用習(xí)題參考答案第09章_第3頁
課用習(xí)題參考答案第09章_第4頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、程序設(shè)計(jì)技術(shù)(第三版)習(xí)題參考答案及說明習(xí)題9單項(xiàng)選擇題ADBBB CCCDC填空題顯示器w指定文件內(nèi)部記錄指針移動(dòng)的起始位置sizeof(student)閱讀程序題younk文件拷貝1 3 5 7 9Name: Lilei Phone:123456 Score:100AAA18程序設(shè)計(jì)題/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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論