1 回答
TA贡献1796条经验 获得超7个赞
class complex
{
public:
complex(){real=0;imag=0;}
complex(double r){real=r;imag=0;}
complex(double r,double i){real=r;imag=i;}
friend istream & operator >> (istream &,complex &);
friend ostream & operator << (ostream &,complex &);
private:
double real;
double imag;
};
istream & operator >> (istream &input,complex &c)
{
cout<<"please input real part and imaginary part of complex number:";
input>>c.real>>c.imag;
return input;
}
ostream & operator << (ostream &output,complex &c)
{
if(c.imag>0)
if(c.real==0)output<<c.imag<<"i";
else output<<c.real<<"+"<<c.imag<<"i";
else if(c.imag<0)
if(c.real==0)output<<c.imag<<"i";
else output<<c.real<<c.imag<<"i";
else output<<c.real;
return output;
}
添加回答
举报