实验4运算符重载参考答案_第1页
实验4运算符重载参考答案_第2页
实验4运算符重载参考答案_第3页
实验4运算符重载参考答案_第4页
实验4运算符重载参考答案_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、运算符重载1、实验目的和要求:掌握运算符重载的语法要点,学会运算符重载的编程方法。2、实验内容先读程序,写出程序的输出结果,再运行程序验证程序的输出。用友元重载方式重新编写程序。#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用友元方式实现:#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定义一个有理数类Rational,重载算术运算符。写一个完整的程序,测试各种运算符的使用,要求输出化简后的计算结果,注意分母不能为零!测试代码样例: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;/指向链表的头结点public:IntSet()first=0;IntSet(intcount);IntSet(IntSet&old);IntSet();booladd(intnum);增加元素booldel(intnum);删除元素boolsearch(intnum);/查找元素voidshow();IntSetoperator+(IntSet&other);/并IntSetoperator-(IntSet&other);/差IntSetoperator*(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链表比较长while(p2)add(p2-val);p2=p2-pNext;if(pl)当前链表比较长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();运彳丁结果:c:FD:CprojectEbcDeljugljc_exebetAshouallnodeinthelink:p51thenumbeptos

16、houallnodebeindeltedisnotintliesetTSetfi-thelink:p51betBshouallnodeinthelink:p4fi+BThenumisintheset!shouallnodeinthelink:p431betfiBDelete5!shouallnodeinthelink:p1fi*BDelete3fDeleteIfshouallnodeinthelinkSetCshouallnodeinthelinkp51inthelinkDelete5fshouallnodetocontinue(4)设计日期类(Date),重载下列运算符实现相关操作:重载一(

17、日期相差天数)(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)设计字符串类(String),定义各种

22、构造函数并实现以下运算符重载:(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. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论