實(shí)驗(yàn)4運(yùn)算符重載參考答案_第1頁(yè)
實(shí)驗(yàn)4運(yùn)算符重載參考答案_第2頁(yè)
實(shí)驗(yàn)4運(yùn)算符重載參考答案_第3頁(yè)
實(shí)驗(yàn)4運(yùn)算符重載參考答案_第4頁(yè)
實(shí)驗(yàn)4運(yùn)算符重載參考答案_第5頁(yè)
已閱讀5頁(yè),還剩14頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、運(yùn)算符重載1、實(shí)驗(yàn)?zāi)康暮鸵螅赫莆者\(yùn)算符重載的語(yǔ)法要點(diǎn),學(xué)會(huì)運(yùn)算符重載的編程方法。2、實(shí)驗(yàn)內(nèi)容先讀程序,寫出程序的輸出結(jié)果,再運(yùn)行程序驗(yàn)證程序的輸出。用友元重載方式重新編寫程序。#includeusingnamespacestd;classVectorpublic:Vector(inti=0,intj=0)x=i;y=j;Vectoroperator+=(Vectorv)Vectortemp;temp.x=x+v.x;temp.y=y+v.y;returntemp;Vectoroperator-=(Vectorv)Vectortemp(x-v.x,y-v.y);returntemp;voidd

2、isplay()cout(x,y)endl;private:intx,y;voidmain()Vectorv1(1,2),v2(3,4),v3,v4;v3=v1+=v2;v4=v1-=v2;coutv1=;v1.display();/1,2coutv2=;v2.display();/3,4coutv3=;v3.display();/4,6coutv4=;v4.display();/-2,-2用友元方式實(shí)現(xiàn):#includeusingnamespacestd;classVectorpublic:Vector(inti=0,intj=0)x=i;y=j;friendVectoroperator+=

3、(Vector&v1,Vector&v2)v1.x+=v2.x;v1.y+=v2.y;returnv1;friendVectoroperator-=(Vector&v1,Vector&v2)v1.x=v1.x-v2.x;v1.y=v1.y-v2.y;returnVector(v1.x,v1.y);voiddisplay()cout(x,y)endl;private:intx,y;voidmain()Vectorv1(1,2),v2(3,4),v3,v4;v3=v1+=v2;v4=v1-=v2;coutv1=;v1.display();/1,2coutv2=;v2.display();/3,4c

4、outv3=;v3.display();/4,6coutv4=;v4.display();/1,2定義一個(gè)有理數(shù)類Rational,重載算術(shù)運(yùn)算符。寫一個(gè)完整的程序,測(cè)試各種運(yùn)算符的使用,要求輸出化簡(jiǎn)后的計(jì)算結(jié)果,注意分母不能為零!測(cè)試代碼樣例:RationalA(2,6),B(1,-2),C;C=-A;C.print();/輸出1/3C=A+B;C.print();/輸出-1/6C=C*A/B;C.print();C=+A;A.print;C.print();C=B-;B.print;C.print();源程序:#includeclassRationalpublic:Rational(int

5、nn=1,intmm=1);voidprint();voidsimple();Rationaloperator+(Rational&a);/加法friendRationaloperator-(Rational&a,Rational&b);減法friendRationaloperator*(Rational&a,Rational&b);乘法friendRationaloperator/(Rational&a,Rational&b);除法Rationaloperator-。;/取反Rational&operator+();/r=+r1Rationaloperator+(int);/r=r1+Rat

6、ional&operator-();/r=-r1Rationaloperator-(int);/r=r1-operatordouble();friendostream&operator(ostream&output,Rational&a);booloperatorr2boolRational:operator(double)a.n/a.m?true:false;/r1=r2booloperator=(Rational&a,Rational&b)returndouble(a.n)/a.m=(double)b.n/b.m?true:false;/coutr;ostream&operator(ost

7、ream&output,Rational&a)/output(double)a.n/a.mendl;if(a.m0)outputThevalueis-a.n/-a.mendl;elseoutputThevalueisa.n/a.mendl;returnoutput;voidRational:print()simple();if(m0)coutThevalueis-n/-mendl;elsecoutThevalueisn/mendl;voidRational:simple()inta=m,b=n,r=0;if(m#includevstdlib.hstructNodeintval;Node*pNe

8、xt;classIntSetNode*first;/指向鏈表的頭結(jié)點(diǎn)public:IntSet()first=0;IntSet(intcount);IntSet(IntSet&old);IntSet();booladd(intnum);增加元素booldel(intnum);刪除元素boolsearch(intnum);/查找元素voidshow();IntSetoperator+(IntSet&other);/并IntSetoperator-(IntSet&other);/差I(lǐng)ntSetoperator*(IntSet&other);/交IntSet&operator=(IntSet&oth

9、er);/賦值,作為左值需要返回引用類型!IntSetoperator-=(IntSet&other);IntSet:IntSet(IntSet&old)Node*p=old.first,*tail;if(!p)first=0;elseNode*now=newNode;now-val=p-val;first=now;tail=now;p=p-pNext;while(p)Node*now=newNode;now-val=p-val;tail-pNext=now;tail=now;p=p-pNext;tail-pNext=0;IntSet:IntSet(intcount)Node*head=0,*

10、tail=0;coutSetnewLinkwithcountnote!endl;for(inti=0;icount;i+)coutnow-val;if(!head)head=now;elsetail-pNext=now;tail=now;if(count!=0)tail-pNext=0;first=head;IntSet:IntSet()Node*p=first,*pre;while(p)pre=p;p=p-pNext;deletepre;boolIntSet:add(intnum)Node*tail=first;if(first)while(tail)if(tail-val=num)cout

11、Thenumisintheset!pNext;Node*now=newNode;now-val=num;if(!now)coutErrornewnodewhenaddone!pNext)tail=tail-pNext;tail-pNext=now;tail=now;tail-pNext=0;return1;boolIntSet:search(intnum)if(!first)coutEmptyset,deletenonode!val)&p-pNext)p=p-pNext;if(num=(p-val)return1;elsereturn0;boolIntSet:del(intnum)if(!fi

12、rst)coutEmptyset,deletenonode!val)&p-pNext)pre=p;p=p-pNext;if(num=p-val)if(p=first)first=p-pNext;elsepre-pNext=p-pNext;coutDeletenum!endl;return1;elsecoutthenumbertobedeltedisnotintheset!;return0;voidIntSet:show()coutshowallnodeinthelink:endl;inti=0;Node*tail=first;while(tail)i+;couti)valpNext;IntSe

13、tIntSet:operator+(IntSet&other)IntSettemp(other);Node*p=first;while(p)temp.add(p-val);p=p-pNext;returntemp;IntSetIntSet:operator-(IntSet&other)IntSettemp(*this);Node*q=other.first;while(q)if(search(q-val)temp.del(q-val);q=q-pNext;if(!first)coutEmpty!val)del(q-val);q=q-pNext;if(!first)coutEmpty!val)t

14、emp.del(p-val);p=p-pNext;if(!temp.first)coutEmpty!val=p2-val;pre=p1;p1=p1-pNext;p2=p2-pNext;if(p2)/old鏈表比較長(zhǎng)while(p2)add(p2-val);p2=p2-pNext;if(pl)當(dāng)前鏈表比較長(zhǎng)pre-pNext=0;/刪除多余的鏈表Node*prel;while(pl)prel=pl;pl=pl-pNext;deleteprel;return*this;voidmain()IntSetA;add(3);A.add(5);add(l);coutSetAendl;A.show();A.

15、del(0);coutSetA-endl;A.show();IntSetB;add(5);B.add(4);coutSetBendl;B.show();coutvvSetA+Bvvendl;并(A+B).show();coutvvSetA-Bvvendl;差(A-B).show();coutvvSetA*Bvvendl;交(A*B).show();IntSetC=A;coutvvSetCvvendl;show();A-=B;-=A.show();運(yùn)彳丁結(jié)果:c:FD:CprojectEbcDeljugljc_exebetAshouallnodeinthelink:p51thenumbeptos

16、houallnodebeindeltedisnotintliesetTSetfi-thelink:p51betBshouallnodeinthelink:p4fi+BThenumisintheset!shouallnodeinthelink:p431betfiBDelete5!shouallnodeinthelink:p1fi*BDelete3fDeleteIfshouallnodeinthelinkSetCshouallnodeinthelinkp51inthelinkDelete5fshouallnodetocontinue(4)設(shè)計(jì)日期類(Date),重載下列運(yùn)算符實(shí)現(xiàn)相關(guān)操作:重載一(

17、日期相差天數(shù))(2)重載+(Date+int)(3)重載(intDate)(4)重載+(Dated;d+;+d;+d;)程序源代碼#include/usingnamespacestd;classDatepublic:Date(inty,intm,intd);intoperator-(Date&date2);friendDateoperator+(Date&date1,intnum);friendDateoperator+(intnum,Date&date1);Date&operator+();Dateoperator+(int);intdaycount();voidadjust();voids

18、how();private:intyear,month,day;boolisLeapYear(inti)return(i%4=0&i%100!=0)|(i%400=0);voidDate:adjust()intMonth13=0,31,28,31,30,31,30,31,31,30,31,30,31;if(isLeapYear(year)Month2+;if(dayMonthmonth)day-=Monthmonth;month+;if(month=13)month=1;year+;Dateoperator+(Date&date1,intnum)Datetemp(date1);temp.day

19、=date1.day+num;temp.adjust();returntemp;Dateoperator+(intnum,Date&date1)Datetemp(date1);temp.day=date1.day+num;temp.adjust();returntemp;intDate:daycount()intdayOfMonth13=0,31,28,31,30,31,30,31,31,30,31,30,31,i,sum=0;for(i=1;imonth;i+)sum+=dayOfMonthi;sum+=day;if(isLeapYear(year)sum+=1;for(i=1;iyear;

20、i+)if(isLeapYear(i)sum+=366;elsesum+=365;returnsum;intDate:operator-(Date&date2)return(*this).daycount()-date2.daycount();Date:Date(inty,intm,intd)year=y;month=m;day=d;voidDate:show()coutyear/month/dayendl;DateDate:operator+(int)Dateold=*this;day+;adjust。;returnold;Date&Date:operator+()day+;adjust。;

21、return*this;voidmain()Datedl(2011,12,31);Dated2(2010,4,1);coutvv(d1-d2)vvendl;Dated4=d1+5;d4.show();Dated5=10+d2;d5.show();Dated6=d1+;d6.show();Dated7=+d1;d7.show();Dated8=+d1;d8.show();:CprojectsbsvDebug.tsv.exe-6392012/1/5201/4/112011/12/312912/1/22912/1/4PressanyFertccnt.nuc(5)設(shè)計(jì)字符串類(String),定義各種

22、構(gòu)造函數(shù)并實(shí)現(xiàn)以下運(yùn)算符重載:(1)重載+(連接)(2)重載=(相等)和v(小于)(3)重載Stringstr1;str1.set(Ilove);Stringstr2(you);Stringstr3=str1+str2;str3.print();Stringstr4=str1+love+;str4.print();if(str3str2)cout”str3str2”endl;elseif(str3=str2)cout”str3=str2”endl;elsecoutstr2”endl;str11=str21;str1.print();str2.print();程序源代碼#include#incl

23、ude#includeclassStringpublic:String();String(char*s);String();voidshow();voidset(char*newstr);Stringoperator+(String&str2);friendStringoperator+(String&str1,char*str2);booloperator(String&str2);booloperator=(String&str2);char&operator(inti);private:char*str;intlen;String:String()if(!str)deletestr;St

24、ring:String()len=0;String:String(char*s)len=strlen(s);str=newcharlen+1;if(str!=0)strcpy(str,s);strlen=0;char&String:operator(inti)if(str=0)couterror;exit(0);elseif(i=len)coutoutofrange!;exit(0);elsereturnstri;boolString:operator=(String&str2)if(strcmp(str,str2.str)=0)returntrue;elsereturnfalse;boolString:operator(String&str2)if(strcmp(str,str2.str)0)returntrue;elsereturnfalse;Stringoperator+(String&str1,char*str2)Stringret;ret.len=str1.len+strlen(str2);ret.str=

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝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)論