实现一个复数类

作者:青岛澄润国际贸易有限公司 来源:www.usabcc.com 发布时间:2017-09-11 13:56:23
实现一个复数类



#include<iostream>
using namespace std;
class Complex
{
public:
Complex(double real = 0.0, double image = 0.0)

this->_real = real;
_image = image;

Complex(const Complex &c)

_real = c._real;
_image = c._image;

Complex& operator=(const Complex &c)

_real = c._real;
_image = c._image;
return *this;

bool operator>(const Complex &c)

double tmp1,tmp2;
tmp1 = sqrt(pow(_real, 2) + pow(_image, 2));
tmp2 = sqrt(pow(c._real, 2) + pow(c._image, 2));
return tmp1 > tmp2;

bool operator==(const Complex &c)

return sqrt(pow(_real, 2) + pow(_image, 2)) == sqrt(pow(c._real, 2) + pow(c._image, 2));

bool operator>=(const Complex &c)
(*this == c));

bool operator<(const Complex &c)

return sqrt(pow(_real, 2) + pow(_image, 2)) < sqrt(pow(c._real, 2) + pow(c._image, 2));

bool operator<=(const Complex &c)

return (*this < c)
Complex operator+(const Complex &c)

Complex add;
add._real = _real + c._real;
add._image = _image + c._image;
return add;

Complex operator-(const Complex &c)

Complex sub;
sub._real = _real - c._real;
sub._image = _image - c._image;
return sub;

Complex operator*(const Complex &c)const//******

Complex mul;
mul._real = _real*c._real - _image*c._image;
mul._image = _real*c._image + _image*c._real;
return mul;

Complex operator/(const Complex &c)******

Complex div;
Complex c1;
c1._real = c._real;
c1._image = (-1)*c._image;
const Complex c2 = c*c1;//********************易错
div._real = ((*this*c1)._real) / (c2._real);
div._image = ((*this*c1)._image) / (c2._real);
return div;

Complex operator+=(const Complex &c)

_real = _real + c._real;
_image = _image + c._image;
return *this;

Complex operator-=(const Complex &c)

_real = _real - c._real;
_image = _image - c._image;
return *this;

Complex operator++(int)//后置

Complex tmp(*this);
_real++;
_image++;
return tmp;

Complex operator++()//前置

_real++;
_image++;
return *this;

Complex operator--(int)//后置

Complex tmp(*this);
_real--;
_image--;
return tmp;

Complex operator--()//前置

_real--;
_image--;
return *this;

void Display()
{
if (_real != 0)

if (_image>0)
cout << "Complex=" << _real << "+i" << _image << endl;
else if (_image < 0)
cout << "Complex=" << _real << "-i" << (_image)*(-1) << endl;
else
cout << "Complex=" << _real << endl;

else

if (_image>0)
cout << "Complex=" << "i" << _image << endl;
else if (_image < 0)
cout << "Complex=" << "-i" << (_image)*(-1) << endl;
else
cout << "Complex=" << _real << endl;

}
~Complex()


private:
double _real;
double _image;
};
void Test()

Complex c1(2,1);
c1.Display();
//Complex c2(c1);
Complex c2=c1;
c2.Display();
Complex c3,c4;
c4=c3 = c2;
c3.Display();
c4.Display();
Complex c5(1,2);
c5.Display();
bool ret1 = c1 > c5;
cout << "> "<<ret1 << endl;
bool ret2 = c1 == c5;
cout << "== " << ret2 << endl;
bool ret3 = c1 >= c5;
cout << ">= " << ret3 << endl;
bool ret4 = c1 < c5;
cout << "< " << ret4 << endl;
bool ret5 = c1 <= c5;
cout << "<= " << ret5 << endl;
Complex add = c1 + c5;
add.Display();
Complex sub = c1 - c5;
sub.Display();
Complex mul = c1 * c5;
mul.Display();
Complex div = c1 / c5;
div.Display();
(c1 += c5).Display();
(c1 -= c5).Display();
(c1++).Display();
(++c5).Display();
(c1--).Display();
(--c5).Display();

int main()

Test();
system("pause");
return 0;

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:神农架网站建设 http://shennongjia.45qun.com

  • 上一篇:Eclipse for PHP安装和配置过程
  • 下一篇:最后一页
  •