#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
//实现复数类的基本成员函数
//实现复数之间比较大小
//实现复数的四则运算
/* 复数加法:
复数z = a + bi(a, b为实数)
当b = 0时, z为实数, 可以比较大小;
当b不为零时, z为虚数, (a = 0时为纯虚数), 不能比较大小.*/
/* 复数减法:
设z1=a+bi,z2=c+di是任意两个复数,
则它们的差是 (a+bi)-(c+di)=(a-c)+(b-d)i.
两个复数的差依然是复数,它的实部是原来两个复数实部的差,它的虚部是原来两个虚部的差
*/
//复数乘法:设z1 = a + bi,z2 = c + di(a、b、c、d∈R)是任意两个复数,
//那么它们的积(a + bi)(c + di) = (ac - bd) + (bc + ad)i.
//复数除法:
//(a + bi) / (c + di) = (ac + bd) / (c ^ 2 + d ^ 2) + (bc - ad) / (c ^ 2 + d ^ 2)i
class Complex
{
public:
Complex(double real=0.0 , double image=0.0 )
:_real(real)
, _image(image)
{}
Complex(Complex & z)
{
_real = z._real;
_image = z._image;
}
void Display()
{
cout << "z="<<_real << "+" << _image << "i" << endl;
}
~Complex()
{}
//赋值运算符重载
Complex & operator=(Complex& z)
{
if (this != &z)
{
this->_real = z._real;
this->_image = z._image;
}
return *this;
}
//复数比较大小
bool operator>(const Complex &z)
{
if (this->_image != 0 || z._image != 0)
{
cout << "虚数不能比较大小" << endl;
return false;
}
else
{
if (_real > z._real)
{
return true;
}
else
return false;
}
}
bool operator==(const Complex &z)
{
if (this->_image != 0 || z._image != 0)
{
cout << "虚数不能比较大小" << endl;
return false;
}
return (this->_image == z._image) && (this->_real == z._real);
}
bool operator>=(const Complex &z)
{
if (this->_image != 0 || z._image != 0)
{
cout << "虚数不能比较大小" << endl;
return false;
}
return (*this == z ||* this > z);
}
bool operator<(const Complex &z)
{
if (this->_image != 0 || z._image != 0)
{
cout << "虚数不能比较大小" << endl;
return false;
}
return !(*this >= z);
}
bool operator<=(const Complex &z)
{
if (this->_image != 0 || z._image != 0)
{
cout << "虚数不能比较大小" << endl;
return false;
}
return !(*this>z);
}
//复数的四则运算
Complex& operator+=(const Complex &z)
{
_real = this->_real + z._real;
_image = this->_image + z._image;
return *this;
}
Complex operator+(const Complex& z)
{
Complex sum;
sum._real =_real + z._real;
sum._image =_image + z._image;
return sum;
}
Complex& operator-=(const Complex &z)
{
_real = this->_real - z._real;
_image = this->_image - z._image;
return *this;
}
Complex operator-(const Complex& z)
{
Complex sub;
sub._real = _real - z._real;
sub._image = _image - z._image;
return sub;
}
Complex operator*(const Complex& z)
{
Complex mul;
mul._real = (this->_real*z._real) - (this->_image*z._image);
mul._image = (this->_real*z._real) + (this->_image*z._image);
return mul;
}
Complex operator/(const Complex& z)
{
Complex div;
div._real = ((this->_real*z._real) + (this->_image*z._image))/(z._real*z._real+z._image*z._image);
div._image = ((this->_image*z._real) - (this->_real*z._image)) / (z._real*z._real + z._image*z._image);
return div;
}
Complex operator++()
{
++_real;
++_image;
return *this;
}
Complex operator++(int)
{
Complex tmp = *this;
_real++;
_image++;
return tmp;
}
private:
double _real;
double _image;
};
void Test()
{
Complex z1(2, 1);
z1.Display();
//Complex z2(3,1);
//z2.Display();
//bool ret = (z1 <z2);
//cout << ret << endl;
//Complex z3 = z1 - z2;
//z3.Display();
//Complex z3 = z1 + z2;
//z3.Display();
//z1-=z2;
//z1.Display();
//Complex z3 = z1 / z2;
//z3.Display();
z1++;
z1.Display();
++z1;
z1.Display();
}
int main()
{
Test();
system("pause");
return 0;
}
本文出自 “言安阳” 博客,请务必保留此出处http://iynu17.blog.51cto.com/10734157/1735825