求助——定义复数类complex,重载运算符“+”的C++程序题定义一个复数类complex,重载运算符“+”,使之能用于复数的加法运算.将运算符函数重载为非成员、非友元的普通函数.编写程序,求两个复

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/27 23:31:33

求助——定义复数类complex,重载运算符“+”的C++程序题定义一个复数类complex,重载运算符“+”,使之能用于复数的加法运算.将运算符函数重载为非成员、非友元的普通函数.编写程序,求两个复
求助——定义复数类complex,重载运算符“+”的C++程序题
定义一个复数类complex,重载运算符“+”,使之能用于复数的加法运算.将运算符函数重载为非成员、非友元的普通函数.编写程序,求两个复数之和.
是来这里求简洁正确答案的,
真的很感谢这里的朋友都给出了自己认为合理而且完整的答案,

求助——定义复数类complex,重载运算符“+”的C++程序题定义一个复数类complex,重载运算符“+”,使之能用于复数的加法运算.将运算符函数重载为非成员、非友元的普通函数.编写程序,求两个复
#include
#include
class Complex
{
public:
Complex() :_real(0),_imag(0) {}
explicit Complex( double r) :_real(r),_imag(0) {}
Complex(double r,double i) :_real(r),_imag(i) {}
Complex& operator+=(const double& d)
{
_real += d;
return *this;
}
Complex& operator+=(const Complex& c)
{
_real += c._real;
_imag += c._imag;
return *this;
}
Complex& operator-=(const double &d)
{
_real -= d;
return *this;
}
Complex& operator-=(const Complex& c)
{
_real -= c._real;
_imag -= c._imag;
return *this;
}
Complex& operator*=(const double& d)
{
_real *= d;
_imag *= d;
return *this;
}
Complex& operator*=(const Complex& c)
{
double re = _real;
double im = _imag;
_real = re * c._real - im * c._imag;
_imag = re * c._imag + im * c._real;
return *this;
}
Complex& operator/=(const double& d)
{
_real /= d;
_imag /= d;
return *this;
}
Complex& operator/=(const Complex& c)
{
double re = _real;
double im = _imag;
double d = c._real * c._real + c._imag * c._imag;
_real = (re * c._real + im * c._imag) / d;
_imag = (im * c._real - re * c._imag) / d;
return *this;
}
Complex Conj() const
{
return Complex(_real,-_imag);
}
double Real() const { return _real; }
double Imag() const { return _imag; }
void Real(const double& re) { _real = re ; }
void Imag(const double& im) { _imag = im ; }
void Set(const double& re,const double& im){ _real = re; _imag = im ; }
double Modsq() const { return _real*_real + _imag * _imag ; }
double Mod() const { return sqrt(_real*_real + _imag * _imag); }
private:
double _real;
double _imag;
};
inline Complex operator+(const Complex& c)
{
return Complex(c.Real(),c.Imag());
}
inline Complex operator-(const Complex& c)
{
return Complex(-c.Real(),-c.Imag());
}
inline Complex operator+(const Complex& c,const double& d)
{
return Complex(c.Real() + d,c.Imag());
}
inline Complex operator+(const double& d,const Complex& c)
{
return Complex(d + c.Real(),c.Imag());
}
inline Complex operator+(const Complex& c1,const Complex& c2)
{
return Complex(c1.Real() + c2.Real(),c1.Imag() + c2.Imag());
}
inline Complex operator-(const Complex& c,const double& d)
{
return Complex(c.Real() - d,c.Imag());
}
inline Complex operator-(const double& d,const Complex& c)
{
return Complex(d - c.Real(),-c.Imag());
}
inline Complex operator-(const Complex& c1,const Complex& c2)
{
return Complex(c1.Real() - c2.Real(),c1.Imag() - c2.Imag());
}
inline Complex operator*(const Complex& c,const double& d)
{
return Complex(c.Real() * d,c.Imag() * d);
}
inline Complex operator*(const double& d,const Complex& c)
{
return Complex(c.Real() * d,c.Imag() * d);
}
inline Complex operator*(const Complex& c1,const Complex& c2)
{
double real = c1.Real() * c2.Real() - c1.Imag() * c2.Imag();
double imag = c1.Real() * c2.Imag() + c1.Imag() * c2.Real();
return Complex(real,imag);
}
inline Complex operator/(const Complex& c,const double& d)
{
return Complex(c.Real() / d,c.Imag() / d);
}
inline Complex operator/(const double& d,const Complex& c)
{
double dd = c.Real() * c.Real() + c.Imag() * c.Imag();
return Complex((d * c.Real())/dd,(-d * c.Imag())/dd);
}
inline Complex operator/(const Complex& c1,const Complex& c2)
{
double d = c2.Real() * c2.Real() + c2.Imag() * c2.Imag();
double real = (c1.Real() * c2.Real() + c1.Imag() * c2.Imag()) / d;
double imag = (c1.Imag() * c2.Real() - c1.Real() * c2.Imag()) / d;
return Complex(real,imag);
}
inline double real(const Complex &c)
{
return c.Real();
}
inline double imag(const Complex &c)
{
return c.Imag();
}
inline double abs(const Complex &c)
{
return sqrt(c.Real() * c.Real() + c.Imag() * c.Imag());
}
inline double norm(const Complex &c)
{
return c.Real() * c.Real() + c.Imag() * c.Imag();
}
inline Complex conj(const Complex &c)
{
return Complex(c.Real(),-c.Imag());
}
ostream &operator