问题标题:
设计一个可进行复数运算的演示程序要求实现六种基本运算:由输入的实部和虚部生成一个复数,两个复数求和,求差,求积,从已知复数中分离出实部,从已知复数分离虚部.运算结果以相应的复
问题描述:
设计一个可进行复数运算的演示程序
要求实现六种基本运算:由输入的实部和虚部生成一个复数,两个复数求和,求差,求积,从已知复数中分离出实部,从已知复数分离虚部.运算结果以相应的复数或实数的表示形式显示.提示定义复数习由两个相互之间存在次序关系的实数构成的抽象数据类型,则可以利用实数的操作来实现复数的操作.
姜大龙回答:
#includeusingnamespacestd;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);voiddisplay();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.imag;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
查看更多