在输出的时候加i就可以了!
实现了加减乘除,输出,其余功能自己加
class Complex
{
private:
double real; //实部
double imag; //虚部
public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
Complex operator +(const Complex &c2 );
Complex operator -(const Complex &c2 );
Complex operator *(const Complex &c2 );
Complex operator /(const Complex &c2 );
void display();
};
Complex Complex::operator+(const Complex &c2)
{
Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
Complex Complex::operator-(const Complex &c2)
{
Complex c;
c.real=real-c2.real;
c.imag=imag-c2.imag;
return c;
}
Complex Complex::operator*(const Complex &c2)
{
Complex c;
c.real=real*c2.real;
c.imag=imag*c2.imag;
return c;
}
Complex Complex::operator/(const Complex &c2)
{
Complex c;
c.real=real/c2.real;
c.imag=imag/c2.imag;
return c;
}
void Complex::display()
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
[此贴子已经被作者于2006-11-9 18:27:39编辑过]