實(shí)驗(yàn)四流運(yùn)算符的重載及文件的使用_第1頁(yè)
實(shí)驗(yàn)四流運(yùn)算符的重載及文件的使用_第2頁(yè)
實(shí)驗(yàn)四流運(yùn)算符的重載及文件的使用_第3頁(yè)
實(shí)驗(yàn)四流運(yùn)算符的重載及文件的使用_第4頁(yè)
實(shí)驗(yàn)四流運(yùn)算符的重載及文件的使用_第5頁(yè)
已閱讀5頁(yè),還剩12頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、實(shí)驗(yàn)報(bào)告(2014 / 2015 學(xué)年第二學(xué)期)課程名稱面向?qū)ο蟪绦蛟O(shè)計(jì)及C+實(shí)驗(yàn)名稱實(shí)驗(yàn)四:流運(yùn)算符的重載及文件的使用實(shí)驗(yàn)時(shí)間 2016年 5 月鈍 日指導(dǎo)單位 計(jì)算機(jī)研究中心指導(dǎo)教師 陳景強(qiáng)學(xué)生姓名班級(jí)學(xué)號(hào)學(xué)院(系)專 業(yè)實(shí)驗(yàn)報(bào)告實(shí)驗(yàn)名稱流運(yùn)算符的重載及文件的使用指導(dǎo)教師陳景強(qiáng)實(shí)驗(yàn)類型驗(yàn)證實(shí)驗(yàn)學(xué)時(shí)2實(shí)驗(yàn)時(shí)間4一、 實(shí)驗(yàn)?zāi)康暮鸵?1)掌握在自定義的類中重載輸入流運(yùn)算符 >>和輸出流運(yùn)算符 <<,并/&入/輸出本 類對(duì)象。(2)掌握文件操作的步驟和方法,能利用程序建立數(shù)據(jù)文件、打開數(shù)據(jù)文件并進(jìn)行 相關(guān)操作。二、實(shí)驗(yàn)環(huán)境(實(shí)驗(yàn)設(shè)備)硬件:微型計(jì)算機(jī)軟件:Micr

2、osoft Visual C+6.0三、實(shí)驗(yàn)原理及內(nèi)容實(shí)驗(yàn)題目(1):在多態(tài)性實(shí)驗(yàn)第2題的基礎(chǔ)上,在復(fù)數(shù)類中增加輸入流運(yùn)算符 >> 和輸出流運(yùn)算符 <<的重載,主函數(shù)通過“ cin>>對(duì)象名”輸入對(duì)象的值,通過“ cout<< 對(duì)象名”輸出對(duì)象的值,輸出復(fù)數(shù)值時(shí)將原來主函數(shù)中“對(duì)象名.print() ”改成"cout<< 對(duì)象名”形式。在上一步完成的基礎(chǔ)上,將復(fù)數(shù)類改成一個(gè)類模板,只設(shè)一個(gè)模板參數(shù),即實(shí)部和 虛部用同一種類型,修改相應(yīng)的代碼,完成輸入、輸出功能。實(shí)驗(yàn)解答:(1)源代碼參考多態(tài)性實(shí)驗(yàn)第2題。(2)記錄重載ope

3、rator<<函數(shù)的代碼:ostream& operator<<(ostream &s,const Complex &c)cout<<c.real;if(c.imag!=0)if(c.imag>0)cout<<"+"cout<<c.imag<<"i"cout<<endl;return s;(3)將類改寫成類模板:(多態(tài)性實(shí)驗(yàn)代碼中的重載符號(hào)僅保留加法 +,其他可省略)#include<iostream>using namespac

4、e std;template <class T>class Complexprivate:T real;T imag;public:Complex(T r=0,T i=0)real=r;imag=i;friend Complex operator+(const Complex &a,const Complex &b);friend Complex operator-(const Complex &a,const Complex &b);friend ostream &operator<<(ostream &out,cons

5、t Complex &a);friend istream &operator>>(istream &in,const Complex &a);Complex operator*(const Complex &a);Complex operator/(const Complex &a);;template <class T>ostream &operator<<(ostream &out,const Complex &a)out<<a.real;if(a.imag!=0)if(

6、a.imag>0)out<<"+"out<<a.imag<<"i"out<<endl;return out;template <class T>istream &operator>>(istream &in,const Complex &a)in>>a.real;if(a.imag!=0)if(a.imag>0)in>>"+"in>>a.imag>>"i"ret

7、urn in;template <class T>Complex <T> operator+(const Complex <T> &a,const Complex <T> &b)Complex <T> temp;temp.real=a.real+b.real;temp.imag=a.imag+b.imag;return temp;template <class T>Complex <T> operator-(const Complex <T> &a,const Complex

8、 <T> &b)Complex <T> temp;temp.real=a.real-b.real;temp.imag=a.imag-b.imag;return temp;template <class T>Complex <T> Complex <T>二。perator*(const Complex <T> &a) Complex <T> temp;temp.real=real*a.real;temp.imag=imag*a.imag;return temp;template <class

9、 T>Complex <T> Complex <T>二。perator/(const Complex <T> &a) Complex <T> temp;temp.real=real/a.real;temp.imag=imag/a.imag;return temp;void main()(Complex <double> a1,a2,a3,a4,a5,a6;cout<<"a1="cin>>a1;cout<<" a2="cin>>a2;c

10、out<<"original a1 is:"<<a1<<endl;cout<<"original a2 is:"<<a2<<endl;a3=a1+a2;cout<<"a3=a1+a2="<<a3<<endl;a4=a1-a2;cout<<"a4=a1-a2="<<a4<<endl;a5=a1*a2;cout<<"a5=a1*a2="<&

11、lt;a5<<endl;a6=a1/a2;cout<<"a6=a1/a2="<<a6<<endl;)實(shí)驗(yàn)報(bào)告實(shí)驗(yàn)題目2:事先用Windows的記事本建立一個(gè)文本文件ff.txt 。 編寫一個(gè)函數(shù)void ReadFile(char* s)實(shí)現(xiàn)讀取以s用為文件名的文本文件的內(nèi) 容在屏帚上顯小0 編寫一個(gè)函數(shù) void Change(char *s1 , char *s2)將文本文件中的小寫字母全部 改寫成大寫字母生成一個(gè)新文件ff2.txt 。 主函數(shù)中調(diào)用 ReadFile("ff.txt"); 顯示ff.

12、txt 的內(nèi)容,調(diào)用 Change ("ff.txt" ,"ff2.txt"); 根據(jù)ff.txt文件作修改生成一個(gè)新的文件ff2.txt ,最后再調(diào)用ReadFile("ff2.txt");顯示新文件的內(nèi)容。實(shí)驗(yàn)解答:void ReadFile(char *s) char ch100;ifstream inf(s);if(!inf)cout<<"Cannot open the file!n"return ;inf.get(ch);cout<<ch<<endl;inf.close

13、();/請(qǐng)完成代碼void Change(char *s1,char *s2)ifstream ifile("ff.txt");if(!ifile)cout<<"ff.txt cannot be openned!"<<endl;return ;ofstream ofile("d:tempff2.txt");if(!o刊e)cout<<"ff2.txt cannot be openned!"<<endl;return ;char ch;while(ifile.get(c

14、h)ch=ch-32;ofile.put(ch);ifile.close();ofile.close();/請(qǐng)完成代碼實(shí)驗(yàn)題目3 (選做):定義學(xué)生類,該類包含學(xué)生的一些基本信息:學(xué)號(hào)、姓名、性別、成績(jī)。定義流對(duì)象,實(shí)現(xiàn)用write函數(shù)將學(xué)生信息以二進(jìn)制方式寫到磁盤文件stu.dat中。再用read將磁盤中的學(xué)生信息讀到內(nèi)存顯示在屏幕上。實(shí)驗(yàn)解答:(1) Student:Student(char *nu,char *na,char *se,int s) /構(gòu)造函數(shù)ostream & operator<<(ostream &out,const Student &

15、;s) /重載輸出運(yùn)算符 <<(2) void CreateBiFile(char *filename)ofstream out(filename);tudent stu3=/ 對(duì)象數(shù)組的初始化out.whte();/兩個(gè)實(shí)在參數(shù)自己填寫out.close();(3) void ReadBiFile(char *filename)Student stunum;int i=0;ifstream in(filename);while ( !in.eof( ) /讀出記錄并顯示in.close();實(shí)驗(yàn)報(bào)告四、實(shí)驗(yàn)小結(jié)(包括問題和解決方法、心得體會(huì)、意見與建議等)1 .在題目(1)中,是否可以將模板參數(shù)改為兩個(gè)使得實(shí)部和虛部類型可以不同,有 什么好處?可以,對(duì)于一個(gè)對(duì)復(fù)數(shù)實(shí)部和虛部數(shù)據(jù)類型有不同要求的設(shè)計(jì),可以輕松實(shí)現(xiàn)。2 .在題目(

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(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)論