複數類

複數的各項運算符的重載( + - * /)以及邏輯運算(== !=)和複數賦值

#include<iostream>
using namespace std;
class Complex
{
public:
	Complex(double real, double image)
		: _real(real)
		, _image(image)
	{}
	// 成員函數 
	Complex operator+(const Complex& c)
	{
		Complex temp(_real + c._real, _image + c._image);
		return temp;
	}
	Complex operator-(const Complex& c)
	{
		Complex temp(_real - c._real, _image - c._image);
		return temp;
	}
	Complex operator*(const Complex& c)
	{ 
		Complex temp(_real*c._real-_image*c._image, _image*c._real+_real*c._image);
		return temp;
	}
	Complex operator/(const Complex& c)
	{
		double modulus = c._real*c._real+c._image*c._image;
		Complex temp((_real*c._real - _image*-c._image) / modulus,(_image*c._real + _real*-c._image) / modulus);
		return temp;
	}
	Complex& operator+=(const Complex& c)
	{
		_real += c._real;
		_image+=c._image;
		return *this;
	}
	Complex& operator-=(const Complex& c)
	{
		_real -= c._real;
		_image-=c._image;
		return *this;
	}
	Complex& operator*=(const Complex& c)
	{
		Complex temp(0,0);
		temp._real = _real*c._real - _image*c._image;
		temp._image = _image*c._real + _real*c._image;
		_real = temp._real;
		_image = temp._image;
		return *this;
	}
	Complex& operator/=(const Complex& c)
	{

		Complex temp(0, 0);
		double modulus = c._real*c._real + c._image*c._image;
		temp._real  = (_real*c._real - _image*-c._image) / modulus;
		temp._image = (_image*c._real + _real*-c._image) / modulus;
		_real = temp._real;
		_image = temp._image;
		return *this;
	}
	// 賦值運算符 
	Complex& operator=(const Complex& c)
	{
		_real = c._real;
		_image = c._image;
		return *this;
	}
	bool operator==(const Complex& c)
	{
		if (this == &c){
			cout << "一個數等於它本身" << endl;
			return true;
		}
		if (_real == c._real&&_image == c._image)
			return true;
		else
			return false;
	}
	bool operator!=(const Complex& c)
	{
		if (this == &c){
			cout << "一個數等於它本身" << endl;
			return true;
		}
		if (_real != c._real&&_image != c._image)
			return true;
		else
			return false;
	}
	//重載輸出格式運算符<<
	friend ostream& operator<<(ostream& _cout, const Complex& c);
private:
	double _real;
	double _image;
};
ostream& operator<<(ostream& _cout, const Complex& c)
{
	_cout <<c._real <<"+"<<c._image<<"i" ;
	return _cout;
}
void test()
{

	Complex d1(1.0,2.0);
	Complex d2(2.0,3.0);
	cout <<d1+d2<< endl;
	cout << d1 - d2 << endl;
	cout <<d1*d2<< endl;
	cout <<d1/d2<< endl;
	cout << d1 << endl;
	cout << d2 << endl;
	cout << "(d1 += d2)-->" << (d1 += d2) << endl;
	cout << "d1-->" << d1 << endl;
	cout << "d2-->" << d2 << endl;
	cout << "(d1 -= d2)-->" << (d1 -= d2) << endl;
	cout << "d1-->" << d1 << endl;
	cout << "d2-->" << d2 << endl;
	cout << "(d1 *= d2)-->" << (d1 *= d2) << endl;
	cout << "d1-->" <<d1<< endl;
	cout << "d2-->"<< d2 << endl;
	cout <<"(d1/=d2)-->"<<(d1/=d2)<< endl;
	cout << "d1-->" << d1 << endl;
	cout << "d2-->" << d2 << endl;;
	cout <<(d1==d2)<< endl;
	cout <<(d1!=d2)<< endl;
	cout << "(d1 = d2)-->" << (d1 = d2) << endl;
	cout << "d1-->" << d1 << endl;
	cout << "d2-->" << d2 << endl;;
}
int main()
{
	test();
	system("pause");
	return 0;
}

測試結果如下:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章