《c复数加减乘除的实现.pdf》由会员分享,可在线阅读,更多相关《c复数加减乘除的实现.pdf(3页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、 c 复数加减乘除的实现 集团企业公司编码:(LL3698-KKI1269-TM2483-LUI12689-ITT289-C+复数加减乘除的实现#include usingnamespacestd;classComplex public:Complex()real=0;imag=0;Complex(doubler,doublei)real=r;imag=i;Complexoperator+(Complex&c2);Complexoperator-(Complex&c2);Complexoperator*(Complex&c2);Complexoperator/(Complex&c2);void
2、display();private:doublereal;doubleimag;ComplexComplex:operator+(Complex&c2)Complexc;c.real=real+c2.real;c.imag=imag+c2.imag;returnc;ComplexComplex:operator-(Complex&c2)Complexc;c.real=real-c2.real;c.imag=imag-c2.imag;returnc;ComplexComplex:operator*(Complex&c2)Complexc;c.real=real*c2.real-imag*c2.i
3、mag;c.imag=imag*c2.real+real*c2.imag;returnc;ComplexComplex:operator/(Complex&c2)Complexc;c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);returnc;voidComplex:display()cout(real,imagi)endl;intmain()Complexc1(3,4),c2(5,-10),c3;c3=c1+c2;coutc1+c2=;c3.display();c3=c1-c2;coutc1-c2=;c3.display();c3=c1*c2;coutc1*c2=;c3.display();c3=c1/c2;coutc1/c2=;c3.display();return0;