c++实现复数运算(运算符重载)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c++实现复数运算(运算符重载)相关的知识,希望对你有一定的参考价值。

#include <iostream>

using namespace std;

class Complex

{

public:

//构造函数

Complex(int real=2,int image=4)

:_real(real)

,_image(image)

{

cout<<"构造函数"<<endl;

}

//拷贝构造函数

Complex(Complex& c)

{

cout<<"拷贝构造函数"<<endl;

_real = c._real;

_image = c._image;

}

//析构函数

~Complex()

{

cout<<"析构函数"<<endl;

}


Complex& operator=(Complex& c)//赋值运算符的重载

{

cout<<"赋值运算符的重载"<<endl;

if(this != &c)

{

_real = c._real;

   _image = c._image;

}

return *this;

}

void Print()

{

if(_image > 0 )

{

cout<<_real<<"+"<<_image<<"i"<<endl;

}

else

{

cout<<_real<<_image<<"i"<<endl;

}

}

Complex operator+(Complex& c)//加

{

Complex tmp(*this);

tmp._real += c._real;

tmp._image += c._image;

return tmp;

}

Complex& operator+=(Complex& c)//a+=b  a=a+b

{

this->_real += c._real;

this->_image += c._image;

return *this;

}

Complex operator-(Complex& c)//减

{

Complex tmp(*this);

tmp._real -= c._real;

tmp._image -= c._image;

return tmp;

}

Complex& operator-=(Complex& c)//a-=b  a=a-b

{

this->_real -= c._real;

this->_image -= c._image;

return *this;

}

Complex operator*(Complex &c)//乘

{

Complex tmp(*this);

tmp._real = this->_real * c._real - this->_image * c._image;

tmp._image = this->_real * c._image + this->_image * c._real;

return tmp;

}

    Complex& operator*=(Complex &c)    //a*=b  a=a*b

{

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

this->_image = this->_real * c._image + this->_image * c._real;

return *this;

}

Complex operator/(Complex &c)//除

{

Complex tmp(*this);

tmp._real = (this->_real * c._real + this->_image * c._image)/(c._real*c._real+c._image*c._image);

tmp._image = (this->_real * c._image * (-1) + this->_image * c._real)/(c._real*c._real+c._image*c._image);

return tmp;

}

Complex& operator/=(Complex &c)

{

this->_real = (this->_real * c._real + this->_image * c._image)/(c._real*c._real+c._image*c._image);

this->_image = (this->_real * c._image * (-1) + this->_image * c._real)/(c._real*c._real+c._image*c._image);

return *this;

}

Complex& operator++()//前置++

{

this->_real++;

this->_image++;

return *this;

}

    Complex operator++(int)//后置++

{

Complex tmp(*this);

this->_real++;

this->_image++;

return tmp;

}

   Complex& operator--()//前置--

   {

  this->_real--;

  this->_image--;

  return *this;

   }

   Complex operator--(int)//后置--

   {

Complex tmp(*this);

this->_real--;

this->_image--;

return tmp;

   }

bool operator==(Complex &c)//相等

{

return (this->_real == c._real)&&(this->_image == c._image);

}

    bool operator!=(Complex &c)//不等

{

return !(this->_real == c._real)&&(this->_image == c._image);

}

    bool operator<(Complex &c)//小于

{

return ((this->_real<c._real)&&(this->_image == 0)&&(c._image ==0));

}

    bool operator<=(Complex &c)//小于等于

{

return ((this->_real<=c._real)&&(this->_image == 0)&&(c._image ==0));

}

    bool operator>(Complex &c)//大于

{

return ((this->_real>c._real)&&(this->_image == 0)&&(c._image ==0));

}

    bool operator>=(Complex &c)//大于等于

{

return ((this->_real>=c._real)&&(this->_image == 0)&&(c._image ==0));

}

ostream &operator <<(ostream &os)//输出

{

os<<this->_real<<"+"<<this->_image<<"i"<<endl;

return os;

}

istream &operator >>(istream &is)//输入

{

is>>this->_real>>this->_image;

return is;

}


private:

int _real;

int _image;

/*double _real;

double _image;*/

};


int main()

{

Complex c;

Complex c1(5,8);

Complex c3 = c.operator /=(c1);//Complex c3 = (c/c1);

c3.Print();

//c.operator=(c1);

//Complex c3 = c.operator+(c1);

/*Complex c3;

c3 = c.operator +(c1);*/

/*Complex c4 = c.operator-(c1);

c4.Print();*/

/*Complex c5 = c.operator -=(c1);

c5.Print();*/

/*Complex c6 = c.operator*=(c1);

c6.Print();*/

//c1.operator++();

//c1.Print();

/*int d = c.operator !=(c1);

cout<<d<<endl;*/

    //int e = c.operator >(c1);//int d = (c > c1);

//cout<<e<<endl;

//c<<cout;

//int f = c.operator >=(c1);//int f = (c >= c1);

//cout<<f<<endl;

/* c>>cin;

c.Print();*/

 

return 0;

}


以上是关于c++实现复数运算(运算符重载)的主要内容,如果未能解决你的问题,请参考以下文章

YTU 2439: C++习题 复数类--重载运算符+

YTU 2441: C++习题 复数类--重载运算符2+

YTU 2443: C++习题 复数类--重载运算符3+

C++运算符的重载

C++运算符的重载

c++运算符重载