本文共 3096 字,大约阅读时间需要 10 分钟。
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
class Complex
{
public:
void display( )
{
/*cout << "real:" << this->_real << endl;
cout << "image:" << this->_image << endl;*/
cout << this->_real << "+" << this->_image << "i" << endl;
}
Complex operator+(const Complex& a)
{
Complex tem;
tem._real = this->_real + a._real;
tem._image = this->_image + a._image;
return tem;
}
Complex operator-(const Complex& a)
{
Complex tem;
tem._real = this->_real - a._real;
tem._image = this->_image - a._image;
return tem;
}
Complex operator*(const Complex& a)
{
Complex tem;
tem._real = this->_real * a._real;
tem._image = this->_image * a._image;
return tem;
}
Complex operator/(const Complex& a)
{
Complex tem;
tem._real = this->_real / a._real;
tem._image = this->_image / a._image;
return tem;
}
void operator+=(const Complex& a)
{
this->_real = this->_real + a._real;
this->_image = this->_image + a._image;
}
void operator-=(const Complex& a)
{
this->_real = this->_real - a._real;
this->_image = this->_image - a._image;
}
void operator*=(const Complex& a)
{
this->_real = this->_real * a._real;
this->_image = this->_image * a._image;
}
void operator/=(const Complex& a)
{
this->_real = this->_real / a._real;
this->_image = this->_image / a._image;
}
Complex operator++(int)//huozhi
{
Complex tem(*this);
this->_real += 1;
this->_image += 1;
return tem;
}
Complex& operator++()
{
this->_real += 1;
this->_image += 1;
return *this;
}
Complex operator--(int)//huozhi
{
Complex tem(*this);
this->_real -= 1;
this->_image -= 1;
return tem;
}
Complex& operator--()
{
this->_real -= 1;
this->_image -= 1;
return *this;
}
bool operator>(const Complex& a)
{
double m = pow(this->_real, 2) + pow(this->_image, 2);
double n = pow(a._real, 2) + pow(a._image, 2);
return (m > n);
}
bool operator<(const Complex& a)
{
double m = pow(this->_real, 2) + pow(this->_image, 2);
double n = pow(a._real, 2) + pow(a._image, 2);
return (m < n);
}
bool operator>=(const Complex& a)
{
double m = pow(this->_real, 2) + pow(this->_image, 2);
double n = pow(a._real, 2) + pow(a._image, 2);
return (m > n || m == n);
}
bool operator<=(const Complex& a)
{
double m = pow(this->_real, 2) + pow(this->_image, 2);
double n = pow(a._real, 2) + pow(a._image, 2);
return (m < n || m == n);
}
bool operator==(const Complex& a)
{
double m = pow(this->_real, 2) + pow(this->_image, 2);
double n = pow(a._real, 2) + pow(a._image, 2);
return (m == n);
}
public:
Complex(double real = 0.0, double image = 0.0)
{
this->_real = real;
this->_image = image;
}
Complex(Complex& a)
{
this->_real = a._real;
this->_image = a._image;
}
~Complex()
{
;
}
Complex* operator=(const Complex& a)
{
this->_real = a._real;
this->_image = a._image;
return this;
}
private:
double _real;
double _image;
};
int main()
{
Complex first(1.0, 2.0);
first.display();
Complex second(first);
second.display();
Complex three;
//three = second = first;
//second.display();
//three = first + second;
first.operator++(1);
first.display();
system("pause");
return 0;
}