




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/*1.定义一个复数类,通过重载运算符:+、-、*、/ 等,实现两个复数之间的各种运算。编写一个完整的程序。*/#includeclass Complexfloat Real,Image;public:Complex(float x=0,float y=0)Real=x;Image=y;friend Complex operator + (Complex &,Complex &);friend Complex operator - (Complex &,Complex &);friend Complex operator * (Complex &,Complex &);friend Complex operator / (Complex &,Complex &);void show()coutReal=RealtImage=Imageendl;Complex operator + (Complex &a,Complex &b)Complex t;t.Real=a.Real+b.Real;t.Image=a.Image+b.Image;return t;Complex operator - (Complex &a,Complex &b)Complex t;t.Real=a.Real-b.Real;t.Image=a.Image-b.Image;return t;Complex operator * (Complex &a,Complex &b) Complex t;t.Real=a.Real*b.Real-a.Image*b.Image;t.Image=a.Real*b.Image+a.Image*a.Real;return t;Complex operator / (Complex &a,Complex &b) Complex t;t.Real=(a.Real*b.Real+a.Image*b.Image)/(b.Real*b.Real+b.Image*b.Image);t.Image=(a.Image*a.Real-a.Real*b.Image)/(b.Real*b.Real+b.Image*b.Image);return t;void main()Complex c1(10,20),c2,c3(50,40);c2=c1+c3;c2.show();c2=c1-c3;c2.show();c2=c1*c3;c2.show();c2=c1/c3;c2.show();/*2.定义描述一个三维点,利用友元函数重载+和-运算符,并区分这两种运算符的前置和后置运算。*/#includeclass pointint x;int y;int z;public:point(int X=0, int Y=0,int Z=0)x=X;y=Y;z=Z;point operator +(point &a)point t;t.x=x+a.x;t.y=y+a.y;t.z=z+a.z;return t;friend point operator +(point &a);friend point operator +(point &,int);friend point operator -(point &);friend point operator -(point &,int);void show()coutx=xty=ytz=zendl;point operator +(point &a)point t;t.x=+a.x;t.y=+a.y;t.z=+a.z;return t;point operator +(point &a,int)point t;t.x=a.x+;t.y=a.y+;t.z=a.z+;return t;point operator -(point &a) point t;t.x=-a.x;t.y=-a.y;t.z=-a.z;return t;point operator -(point &a,int)point t;t.x=a.x-;t.y=a.y-;t.z=a.z-;return t;void main()point p1(10,20),p2(30,40),p3;p3=p3+p1;p3.show();p3=+p1;p3.show();p1.show();p3=-p1;p3.show();p1.show();p3=p2+;p3.show();p2.show();p3=p2-;p3.show();p2.show();/*3.完善字符窜类,增加以下运算符的重载:+、- 等,实现两个字符窜间的运算。*/#include#includeclass strint length;char *p;public:str()length=0;p=0;str(char *s)if(s) length=strlen(s)+1;p=new char length;strcpy(p,s);else length=0;p=0; str(str &s1)length=s1.length +1;p=new char length;strcpy(p,s1.p);str()if(p) delete p;friend str operator +(str &,str &);friend str operator -(str &,str &);void operator =(str &);void show()coutlength=lengthtpendl;str operator +(str &a,str &b)str s;s.length=a.length+b.length;s.p=new chars.length+1;s.p=strcpy(s.p,a.p);strcat(s.p,b.p);return s;str operator -(str &a,str &b)str s;char *p1=a.p,*p2;int i=0,len=strlen(b.p);if(p2=strstr(a.p,b.p)s.length=a.length-len;s.p=new chars.length+1;while(p1p2)s.pi+=*p1+;p1+=len;while(s.pi+=*p1+);else s.length=a.length;s.p=new char s.length+1;strcpy(s.p,a.p);return s;void str:operator =(str &a)length =a.length +1; p=new char length; strcpy(p,a.p);void main()char *a=I am a ,*b=student!;str A(a),B(b),C;C=A+B;C.show();C=C-B;C.show();/*附加题定义一个字符窜类,用来存放不定长的字符窜,重载运算符= =、,用于两个字符窜的等于、小于和大于的比较运算。*/#include#includeclass strchar *p;public:str()p=0;str(char *s)p=s;friend bool operator (str &,str &);friend bool operator (str &,str &);friend bool operator =(str &,str &);void show()cout(str &a,str &b)if(strcmp(a.p,b.p)0) return 1;else return 0;b
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高中政治3.1说课课件
- 2025年中国自动化仪表行业市场前景及投资研究报告
- 高一急救知识培训班课件
- 智能化施工安全防护空白单位工程劳务分包合同
- 离婚子女抚养权归属与财产分割及子女社会实践协议
- 离婚协议签署及履行监督服务合同
- 离婚协议:财产分割、子女抚养及共同财产清算合同
- 民族特色理发店技师劳务合作合同范本
- 广告内容本地化代理合同
- 职业技能拓展方案设计
- XB/T 903-2002烧结钕铁硼永磁材料表面电镀层
- JJG 146-2011量块
- 小学数学思想方法(课件)
- 食品安全事故案例课件-002
- 领导干部压力管理与心理调适概论课件
- 气管插管导管脱出的应急预案
- 《宠物美容与护理》全套教学课件
- 表面工程学第十二章-表面微细加工技术
- 山东大学工程流体力学(杜广生)课件第5章 粘性流体的一维流动
- 底拖法在管道施工中的应用
- Toeic托业考试真习题及答案
评论
0/150
提交评论