C++ Complex复数类

#define _CRT_SECURE_NO_WARNINGS 1
#include 
using namespace std;

class Complex
{
public:
	Complex(double real = 0, double image = 0)
		:_real(real)
		,_image(image)
	{}

	Complex(const Complex& d)
		:_real(d._real)
		,_image(d._image)
	{}

	void Display()
	{
		cout<<"real:"<<_real<<"image:"<<_image<
	//operator >=
	//operator <
	//operator <=
	bool operator ==(const Complex& d)//复数的==
	{
		return (_real == d._real)
			&& (_image == d._image);
	}

	bool operator !=(const Complex& d)//复数的!=
	{
		return (_real != d._real)
			|| (_image != d._image);
	}

	Complex operator *(const Complex& d)//复数相乘
	{
		Complex tmp;
		tmp._real = (_real*d._real) - (_image*d._image);
		tmp._image = (_real*d._image) + (_image*d._real);
		return tmp;
	}

	Complex operator /(const Complex& d)//复数相除
	{
		Complex tmp;
		tmp._real= ((_real*d._real) - (_image*d._image))
			      /((d._real)*(d._real) + (d._image)*(d._image));
		tmp._image = (_real*d._image) + (_image*d._real)
				  /((d._real)*(d._real) + (d._image)*(d._image));
		return tmp;
	}

private:
	double _real;
	double _image;
};

void test1()//测试加减相关函数
{
	Complex d1(1, 1);
	Complex d2(2, 2);
	Complex d3;
	d3 = d1 + d2;
	d3.Display();

	d3 = d1++;
	d3.Display();

	d3 = d1+=(d2);
	d3.Display();

	d3 = ++d1;
	d3.Display();
}

void test2()//测试乘除/==/!=
{
	int a = -1;
	Complex d1(1, 1);
	Complex d2(2, 2);
	Complex d3;

	d3 = d1*(d2);
	d3.Display();

	d3 = d1/(d2);
	d3.Display();

	a = d1==(d2);
	cout<<"operator ==():"<


你可能感兴趣的:(C/C++)