欢迎来到天天文库
浏览记录
ID:40648918
大小:28.00 KB
页数:5页
时间:2019-08-05
《重载运算符求两个复数之和、差、积和商》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#includeusingnamespacestd;classComplex{public:Complex(){real=0;image=0;}Complex(doubler,doublei){real=r;image=i;}Complexoperator+(Complex&c2);Complexoperator-(Complex&c2);Complexoperator*(Complex&c2);Complexoperator/(Complex&c2);voiddisplay();private:doublereal;doubleimage;};intmain(){C
2、omplexc1(3,5),c2(5,-10),c3;c3=c1+c2;cout<<"c1+c2=";c3.display();c3=c1-c2;cout<<"c1-c2=";c3.display();c3=c1*c2;cout<<"c1*c2=";c3.display();c3=c1/c2;cout<<"c1/c2=";c3.display();return0;}ComplexComplex::operator+(Complex&c2){Complexc;c.real=real+c2.real;c.image=image+c2.image;returnc;}ComplexComplex
3、::operator-(Complex&c2){Complexc;c.real=real-c2.real;c.image=image-c2.image;returnc;}ComplexComplex::operator*(Complex&c2){Complexc;c.real=real*c2.real-image*c2.image;c.image=image*c2.real+real*c2.image;returnc;}ComplexComplex::operator/(Complex&c2){Complexc;c.real=(real*c2.real+image*c2.image)/(
4、c2.real*c2.real+c2.image*c2.image);c.image=(image*c2.real-real*c2.image)/(c2.real*c2.real+c2.image*c2.image);returnc;}voidComplex::display(){cout<0)cout<<"+";cout<
此文档下载收益归作者所有