欢迎来到天天文库
浏览记录
ID:60775983
大小:15.50 KB
页数:5页
时间:2020-12-17
《定义复数Complex类使用友元完成复数的加法减法乘法和除法运算以及对复数的输出.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、1.定义复数Complex类,使用友元,完成复数的加法、减法、乘法和除法运算,以及对复数的输出。#include#includeclassComplex{public:Complex(doubler,doublei):real(r),imag(i){}voidprint();friendComplexoperator+(Complex,Complex);friendComplexoperator-(Complex,Complex);friendComplexoperator
2、*(Complex,Complex);friendComplexoperator/(Complex,Complex);private:doublereal;doubleimag;};voidComplex::print(){cout<<"("<3、plexoperator-(Complexc1,Complexc2){Complexc3(0,0);c3.real=c1.real-c2.real;c3.imag=c1.imag-c2.imag;returnc3;}Complexoperator*(Complexc1,Complexc2){Complexc3(0,0);c3.real=c1.real*c2.real-c1.imag*c2.imag;c3.imag=c1.real*c2.imag+c1.imag*c2.real;returnc3;}Complexop4、erator/(Complexc1,Complexc2){Complexc3(0,0);c3.real=(c1.real*c2.real+c1.imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);c3.imag=(c2.real*c1.imag–c2.imag*c1.real)/(c2.real*c2.real+c2.imag*c2.imag);returnc3;}voidmain(){Complexc1(2,8),c2(4,5),c3(0,0),c4(0,0),c5(05、,0),c6(0,0);c1.print();cout<<"+";c2.print();c3=c1+c2;cout<<"=";c3.print();cout<6、();c6=c1/c2;cout<<"=";c6.print();cout<
3、plexoperator-(Complexc1,Complexc2){Complexc3(0,0);c3.real=c1.real-c2.real;c3.imag=c1.imag-c2.imag;returnc3;}Complexoperator*(Complexc1,Complexc2){Complexc3(0,0);c3.real=c1.real*c2.real-c1.imag*c2.imag;c3.imag=c1.real*c2.imag+c1.imag*c2.real;returnc3;}Complexop
4、erator/(Complexc1,Complexc2){Complexc3(0,0);c3.real=(c1.real*c2.real+c1.imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);c3.imag=(c2.real*c1.imag–c2.imag*c1.real)/(c2.real*c2.real+c2.imag*c2.imag);returnc3;}voidmain(){Complexc1(2,8),c2(4,5),c3(0,0),c4(0,0),c5(0
5、,0),c6(0,0);c1.print();cout<<"+";c2.print();c3=c1+c2;cout<<"=";c3.print();cout<6、();c6=c1/c2;cout<<"=";c6.print();cout<
6、();c6=c1/c2;cout<<"=";c6.print();cout<
此文档下载收益归作者所有