求助——定义复数类complex,重载运算符“+”的C++程序题
来源:学生作业帮 编辑:灵鹊做题网作业帮 分类:综合作业 时间:2024/05/01 14:37:52
求助——定义复数类complex,重载运算符“+”的C++程序题
定义一个复数类complex,重载运算符“+”,使之能用于复数的加法运算.将运算符函数重载为非成员、非友元的普通函数.编写程序,求两个复数之和.
是来这里求简洁正确答案的,
真的很感谢这里的朋友都给出了自己认为合理而且完整的答案,
定义一个复数类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 &operato
#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 &operato
求助——定义复数类complex,重载运算符“+”的C++程序题
(C++)定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算.
定义一个复数类COMPLEX,重载运算符“+”使之用于复数的加减运算
(C++高手帮忙)定义一个复数类Complex,重载运算符“-”,使之能用于复数的减法运算.
定义一个复数类Complex,重载运算符“+”,“-”,“*”,“/”,使之能用于复数的加、减、乘、除.
用C++语言 定义复数类complex,使用成员函数重载运算符+-,使用友元函数重载运算符.
定义一个复数类complex ,重载运算符“+”使之用于复数的加法运算,两个复数之和,复数与整数之和
编写一个Complex类,需要完成的运算符重载有:+ :重载+,用来完成两个复数的加法
定义一个复数类Complex,重载运算符“+”、“-”、“*”和“/”,使之能用于复数的加、减、乘和除.
请编写一个类Complex,定义复数的加法、减法、乘法和除法运算,要求在编写该类时重载这些运算操作符,并重载I/O操作符
用C++编写程序,定义一个复数类,编写程序重载四则运算符和++、--运算符,实现复数的相关运算;
设计一个复数类,重载运算符+、—.在Main中定义两个复数对象并用+、—运算获取结果.