c++上機(jī)實(shí)驗(yàn)答案實(shí)驗(yàn)十(共5頁)_第1頁
c++上機(jī)實(shí)驗(yàn)答案實(shí)驗(yàn)十(共5頁)_第2頁
c++上機(jī)實(shí)驗(yàn)答案實(shí)驗(yàn)十(共5頁)_第3頁
c++上機(jī)實(shí)驗(yàn)答案實(shí)驗(yàn)十(共5頁)_第4頁
c++上機(jī)實(shí)驗(yàn)答案實(shí)驗(yàn)十(共5頁)_第5頁
全文預(yù)覽已結(jié)束

下載本文檔

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

文檔簡(jiǎn)介

1、中國(guó)礦業(yè)大學(xué)計(jì)算機(jī)學(xué)院實(shí)驗(yàn)報(bào)告課程名稱 高級(jí)程序設(shè)計(jì)語言 實(shí)驗(yàn)名稱_實(shí)驗(yàn)十 C+ I/O流實(shí)驗(yàn)報(bào)告要求:1.實(shí)驗(yàn)?zāi)康?2.實(shí)驗(yàn)內(nèi)容 3.實(shí)驗(yàn)步驟 4.運(yùn)行結(jié)果 5.流程圖 6.實(shí)驗(yàn)體會(huì) 一、實(shí)驗(yàn)?zāi)康? 熟悉流類庫中常用的類及其成員函數(shù)的用法;2 掌握磁盤文件的打開、關(guān)閉及使用的使用方法;3 掌握文本文件流與二進(jìn)制文件流在操作上的區(qū)別。二、實(shí)驗(yàn)要求1.結(jié)合上課內(nèi)容,寫出程序,并調(diào)試程序,要給出測(cè)試數(shù)據(jù)和實(shí)驗(yàn)結(jié)果。2.整理上機(jī)步驟,總結(jié)經(jīng)驗(yàn)和體會(huì)。3.完成實(shí)驗(yàn)報(bào)告和上交程序。三、實(shí)驗(yàn)內(nèi)容1. 數(shù)組中的數(shù)據(jù)經(jīng)??梢杂枚喾N不同的格式顯示。當(dāng)用戶向數(shù)組輸入整數(shù)時(shí),則分別用二進(jìn)制、八進(jìn)制、十進(jìn)制和十六進(jìn)制

2、的格式輸出表中的數(shù)。編寫函數(shù)convertBinary來實(shí)現(xiàn)將一個(gè)數(shù)轉(zhuǎn)換為其二進(jìn)制表示的函數(shù)這一功能。#includeusing namespace std;void main()int a50,b50;int i=0,j,x;cinx;while(ai-1!=0)ai=x/2;bi=x%2;x=ai;i+;for(j=i-1;j=0;j-)coutbj;coutendl;2. 建立兩個(gè)磁盤文件f1.dat和f2.dat要求:(1)從鍵盤輸入20個(gè)整數(shù),存放在磁盤文件f1.dat中;(2)從f1.dat中讀入10個(gè)數(shù),存放到f2.dat文件中;(3)將f2.dat中的數(shù)據(jù)按由小到大的順序存放,

3、不保留原有文件內(nèi)容#include #include using namespace std; void fun1()int a10; ofstream outfile1(f1.dat),outfile2(f2.dat); if(!outfile1) cerropen f1.dat error!endl; exit(1); if(!outfile2) cerropen f2.dat error!endl; exit(1); coutenter 10 integer numbers:endl; for(int i=0;iai; outfile1ai ; coutenter 10 integer

4、numbers:endl; for(i=0;iai; outfile2ai ; outfile1.close(); outfile2.close(); /從f1,dat讀入10個(gè)數(shù),然后存放到f2.dat文件原有數(shù)據(jù)的后面void fun2()ifstream infile(f1.dat); if(!infile) cerropen f1.dat error!endl; exit(1); ofstream outfile(f2.dat,ios:app); if(!outfile) cerropen f2.dat error!endl; exit(1); int a; for(int i=0;i

5、a; outfilea ; infile.close(); outfile.close(); void fun3()ifstream infile(f2.dat); if(!infile) cerropen f2.dat error!endl; exit(1); int a20; int i,j,t; for(i=0;iai; for(i=0;i19;i+) for(j=0;jaj+1) t=aj;aj=aj+1;aj+1=t; infile.close(); ofstream outfile(f2.dat,ios:out); if(!outfile) cerropen f2.dat erro

6、r!endl; exit(1);coutdata in f2.dat:endl; for( i=0;i20;i+) outfileai ; coutai ; coutendl; outfile.close();int main()fun1(); fun2(); fun3(); return 0;3設(shè)計(jì)一個(gè)管理圖書的簡(jiǎn)單程序,提供的基本功能包括:可連續(xù)將新書存入文件“book.dat”中,新書信息加入到文件的尾部;也可以根據(jù)輸入的書名進(jìn)行查找;把文件“book.dat”中同書名的所有書顯示出來。為簡(jiǎn)單起見,描述一本書的信息包括:書號(hào),書名,出版社和作者。/圖書館書目檢索的源程序#include

7、#include #include class Bookprivate:long int num;/書號(hào)char bookname40;/書名char publicname40;/出版社char name20;/作者public:Book() num=0; bookname0 =0;publicname0 =0; name0 =0;char * Getbookname(void) return bookname ;long Getnum(void ) return num;void Setdata(long , char *,char *,char *);void Show(void );Bo

8、ok(long , char *,char *,char *);void Book:Setdata(long nu , char *bn,char *p,char *n)num = nu; strcpy(bookname,bn);strcpy(publicname,p); strcpy(name,n);void Book:Show(void )cout書號(hào):numt書名:booknamet;cout出版社:publicnamet作者:namen;Book:Book(long nu, char * bp,char *p,char *n)Setdata(nu , bp, p, n); void m

9、ain(void)Book b1,b2;long nu;char bn40;/書名char pn40;/出版社char na20;/作者ifstream file1;ofstream file3;char flag = y;while( flag=y |flag=Y) /由flag控制循環(huán)cout 1: 按書名查找一本書!endl;cout 2: 加入一本新書!endl;cout 3: 退出!endl;coutf;switch(f)case 1:coutbn;file1.open(book.dat,ios:in | ios:binary);/按讀方式打開文件while(!file1.eof()

10、int n;file1.read(char *)&b1,sizeof(Book);n=file1.gcount();if(n=sizeof(Book)if(strcmp(b1.Getbookname(),bn)=0)/顯示書的信息b1.Show();file1.close();break;case 2:coutnu;coutbn;coutpn;coutna;b1.Setdata(nu,bn,pn,na);file3.open(book.dat,ios:app|ios:binary);/增補(bǔ)方式打開文件file3.write(char*)&b1,sizeof(b1);file3.close();

11、break;default: flag = n;4傳說婆羅門廟中有一個(gè)塔臺(tái),臺(tái)上有三根用鉆石鑲嵌的柱子,分別記作A柱、B柱和C柱,在A柱上放著64個(gè)金盤,每個(gè)金盤的大小都不一樣,并且小金盤子放在大金盤的上面,顯然,最大的金盤位于最下面,最小的金盤位于最上面?,F(xiàn)在,試著把A柱上的盤子移到C柱上,移動(dòng)規(guī)則是一次只能移動(dòng)一個(gè)金盤,并且在移動(dòng)過程中大金盤不能位于小金盤的上面。據(jù)說,當(dāng)A柱上的所有金盤都移到C柱上的那一天,就是世界末日。試著用遞歸法編寫一個(gè)程序,輸入A柱上的盤子數(shù)量,輸出正確的移動(dòng)步驟和總的步數(shù)。/漢諾塔問題的源程序#include using namespace std;int main()void hanoi(int n,char one,char two,char three); int m; coutm; coutThe steps of moving m disks:endl; hanoi(m,A,B,C); return 0;void hanoi(int n,char one,char two,char three) /將n個(gè)盤從one座借助two座,移到th

溫馨提示

  • 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)論