首页 > 其他 > 详细

实现一个复数类

时间:2015-11-16 17:42:39      阅读:289      评论:0      收藏:0      [点我收藏+]
#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)
	{
		return ((*this > 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) || (*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;
}


实现一个复数类

原文:http://lingdandan.blog.51cto.com/10697032/1713118

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!