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

下载本文档

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

文档简介

运算符重载1、 实验目的和要求:掌握运算符重载的语法要点,学会运算符重载的编程方法。2、 实验内容(1) 先读程序,写出程序的输出结果,再运行程序验证程序的输出。用友元重载方式重新编写程序。#include using namespace std;class Vector public:Vector(int i = 0,int j = 0) x=i;y=j;Vector operator+=(Vector v) Vector temp; temp.x = x+v.x; temp.y = y+v.y; return temp;Vector operator-=(Vector v) Vector temp( x-v.x, y-v.y); return temp;void display() cout(x,y)endl;private:int x,y;void main() Vector v1(1,2),v2(3,4),v3,v4; v3=v1+=v2; v4=v1-=v2; coutv1=; v1.display();/1,2 coutv2=; v2.display();/3,4 coutv3=; v3.display();/4,6 coutv4=; v4.display();/-2,-2用友元方式实现:#include using namespace std;class Vectorpublic:Vector(int i = 0,int j = 0)x=i;y=j;friend Vector operator+=(Vector &v1,Vector &v2) v1.x+=v2.x;v1.y+=v2.y;return v1;friend Vector operator-=(Vector &v1,Vector &v2) v1.x=v1.x-v2.x;v1.y=v1.y-v2.y;return Vector( v1.x, v1.y);void display() cout(x,y)endl;private:int x,y;void main()Vector v1(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();/1,2(2) 定义一个有理数类Rational,重载算术运算符。写一个完整的程序,测试各种运算符的使用,要求输出化简后的计算结果,注意分母不能为零!测试代码样例: Rational A(2,6),B(1,-2),C;C = -A; C.print();/输出1/3 C = A + B ; C.print(); /输出-1/6 C = C*A/B; C.print(); C = +A ; A.print; C.print(); C = B- ; B.print; C.print(); 源程序:#includeclass Rationalpublic: Rational(int nn=1,int mm=1); void print();void simple(); Rational operator+(Rational & a);/加法friend Rational operator-(Rational & a,Rational & b);/减法friend Rational operator*(Rational & a,Rational & b);/乘法friend Rational operator/(Rational & a,Rational & b);/除法Rational operator-();/取反 Rational & operator + ();/r=+r1Rational operator + (int);/r = r1+Rational & operator - ();/r=-r1Rational operator - (int);/r = r1- operator double();friend ostream& operator(ostream& output, Rational& a);bool operatorr2bool Rational:operator(double)a.n/a.m ? true : false;/r1=r2bool operator=(Rational& a,Rational&b )return double(a.n)/a.m = (double)b.n/b.m ? true : false;/coutr;ostream& operator(ostream& output, Rational& a)/output(double)a.n/a.mendl;if(a.m0) outputThe value is -a.n /-a.mendl;elseoutputThe value is a.n /a.mendl;return output;void Rational:print()simple();if(m0)coutThe value is -n /-mendl; elsecoutThe value is n /mendl;void Rational:simple()int a = m ,b = n, r = 0; if(mn)a = n;b = m;r = a % b ;while ( r != 0 )a = b ;b = r ; r = a % b ; n /= b; m /= b;void main()Rational A(2,6),B(1,-2),C;C = -A;C.print();/ -1/3 C = A + B;C.print();/ -1/6C = C*A/B; C.print();/ 1/9C = +A ;A.print();/ 4/3C.print();/ 4/3C = B- ; B.print();/ -3/2C.print();/ -1/2运行结果: (3) 设计集合类(Set), 用运算符重载实现并(+),差(),交()等操作,并重载=和 -= ,实现Set S1,S2; s1 = s2; 和 S1-=S2;集合S1中去掉S2中的元素的操作。源程序代码#include#includestruct Nodeint val;Node *pNext;class IntSet Node * first;/指向链表的头结点public:IntSet()first=0;IntSet(int count);IntSet(IntSet &old);IntSet();bool add(int num);/增加元素bool del(int num);/删除元素bool search(int num);/查找元素void show();IntSet operator+(IntSet & other);/并IntSet operator-(IntSet & other);/差IntSet operator*(IntSet & other);/交IntSet & operator=(IntSet & other);/赋值,作为左值需要返回引用类型!IntSet operator-=(IntSet & other);IntSet:IntSet(IntSet &old)Node *p=old.first,*tail;if(!p)first=0;elseNode * now=new Node;now-val=p-val;first=now;tail=now;p=p-pNext;while(p)Node * now=new Node;now-val = p-val;tail-pNext=now;tail=now;p=p-pNext;tail-pNext=0;IntSet:IntSet(int count)Node *head=0,*tail=0;coutSet new Link with count note!endl;for(int i=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;delete pre;bool IntSet:add(int num) Node* tail=first;if(first) while(tail)if(tail-val=num)coutThe num is in the set!pNext;Node *now=new Node;now-val=num;if(!now) coutError new node when add one!pNext)tail=tail-pNext;tail-pNext =now; tail=now;tail-pNext=0;return 1;bool IntSet:search(int num)if(!first)coutEmpty set,delete no node!val) & p-pNext ) p=p-pNext;if(num=(p-val) return 1;else return 0;bool IntSet:del(int num)if(!first)coutEmpty set,delete no node!val) & p-pNext) pre=p; p=p-pNext; if(num=p-val)if(p=first)first=p-pNext ;elsepre-pNext =p-pNext;coutDelete num!endl;return 1;else coutthe number to be delted is not in the set!;return 0;void IntSet:show()coutshow all node in the link:endl;int i=0;Node *tail=first;while(tail)i+;couti) val pNext;IntSet IntSet:operator+(IntSet & other)IntSet temp(other);Node *p=first;while(p)temp.add(p-val);p=p-pNext ;return temp;IntSet IntSet:operator-(IntSet & other)IntSet temp(*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 )temp.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(p1)/当前链表比较长pre-pNext=0;/删除多余的链表Node *pre1;while(p1)pre1=p1;p1=p1-pNext;delete pre1;return *this;void main() IntSet A; A.add(3);A.add(5);A.add(1);coutSet Aendl;A.show(); A.del(0);coutSet A- endl; A.show();IntSet B;B.add(5); B.add(4);coutSet Bendl;B.show(); coutSet A+Bendl;/并 (A+B).show(); coutSet A-Bendl;/差(A-B).show();coutSet A*Bendl;/交(A*B).show();IntSet C=A;coutSet Cendl;C.show();A-=B;/ -=A.show();运行结果:(4)设计日期类(Date),重载下列运算符实现相关操作: (1)重载(日期相差天数) (2)重载(Dateint) (3)重载(intDate) (4) 重载+ ( Date d; d+; +d; +d;)程序源代码 # include /using namespace std;class Datepublic:Date(int y,int m,int d);int operator - (Date & date2);friend Date operator + (Date &date1,int num);friend Date operator + (int num,Date &date1);Date& operator + ();Date operator +(int);int daycount();void adjust();void show();private:int year,month,day;bool isLeapYear(int i)return ( i%4 = 0 & i%100 != 0 )|( i%400 = 0); void Date:adjust()int Month13=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+;Date operator + (Date &date1,int num)Date temp(date1);temp.day=date1.day+num;temp.adjust();return temp;Date operator + (int num,Date &date1)Date temp(date1);temp.day=date1.day+num;temp.adjust();return temp;int Date : daycount()int dayOfMonth13=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;i+)if(isLeapYear(i)sum+=366;elsesum+=365;return sum;int Date:operator -(Date & date2)return (*this).daycount()-date2.daycount();Date:Date(int y,int m,int d)year=y;month=m;day=d;void Date:show()coutyear/month/dayendl;Date Date:operator +(int)Date old=*this;day+;adjust();return old;Date& Date:operator +()day+;adjust();return *this;void main()Date d1(2011,12,31);Date d2(2010,4,1);cout(d1-d2)endl;Date d4=d1+5;d4.show(); Date d5=10+d2;d5.show();Date d6=d1+;d6.show();Date d7=+d1;d7.show();Date d8=+d1;d8.show();(5)设计字符串类(String), 定义各种构造函数并实现以下运算符重载: (1)重载(连接) (2)重载(相等)和(小于)(3)重载 String str1;str1.set(I love ); String str2(you ); String str3 = str1+str2; str3.print(); String str4=str1+love +?; str4.print(); if(str3str2) cout”str3str2”endl;else if(str3=str2) cout”str3=str2”endl;else coutstr2”endl; str11 = str21; str1.print(); str2.print(); 程序源代码# include # include # include class Stringpublic:String();String(char *s);String();void show();void set(char * newstr);String operator + (String &str2);friend String operator + (String &str1,char *str2);bool operator (String &str2);bool operator= (String &str2);char & operator (int i);private:char *str;int len;String:String()if(!str)delete str;String:String()len=0;String:String(char * s)len=strlen(s);str=new charlen+1;if(str!=0)strcpy(str,s);strlen=0;char & String:operator (int i)if(str=0)cout error;exit(0);else if ( i=len) coutout of range!; exit(0);elsereturn stri;bool String:operator = (String & str2)if(strcmp(str,str2.str)=0)return true;elsereturn false;bool String:operator (String &str2)if(strcmp(str,str2.str)0)return true;elsereturn false;St

温馨提示

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

评论

0/150

提交评论