【问题描述】


    定义一个复数类,并重载运算符,以实现复数的加减乘除,相等与否,并显示其结果。


【代码实现】

// code.c
#include <iostream>
using namespace std;

class Comoplex
{
	friend ostream& operator<<(ostream& os ,const Comoplex& c); //友元
public:
	Comoplex(double real = 0.0, double p_w_picpath = 0.0) //构造函数
		:_real(real)
		,_p_w_picpath(p_w_picpath)
	{}
	Comoplex(const Comoplex& c)	 //拷贝构造函数
	{
		_real = c._real;
		_p_w_picpath = c._p_w_picpath;
	}
	
	~Comoplex()//析构函数
	{

	}
	Comoplex& operator=(const Comoplex& c)   //赋值运算符的重载
	{
		if(this != &c)
		{
			this->_real = c._real;
			this->_p_w_picpath = c._p_w_picpath;
		}
		return *this;
	}
	bool operator==(const Comoplex &c)
	{
		return (this->_real == c._real) && (this->_p_w_picpath == c._p_w_picpath);	
	}
	bool operator!=(const Comoplex &c)
	{
		return !(*this == c);
	}

	Comoplex operator+(const Comoplex &c)  //加法
	{
		Comoplex tmp(*this);
		tmp._real +=c._real;
		tmp._p_w_picpath += c._p_w_picpath;
		return tmp;//临时变量
	}
	Comoplex operator-(const Comoplex &c)	
	{
		Comoplex tmp(*this);
		tmp._real -= c._real;
		tmp._p_w_picpath -= c._p_w_picpath;
		return tmp;//临时变量
	}
	Comoplex operator*(const Comoplex &c)
	{
		Comoplex tmp(*this);
		tmp._real = this->_real * c._real - this->_p_w_picpath * c._p_w_picpath;
		tmp._p_w_picpath = this->_real * c._p_w_picpath + this->_p_w_picpath * c._real;
		return tmp;//临时变量 
	}
	Comoplex operator/(const Comoplex &c)
	{
		Comoplex tmp(*this);
		tmp._real = (this->_real *c._real +this ->_p_w_picpath * c._p_w_picpath)/(c._real *c._real + c._p_w_picpath *c._p_w_picpath);
		tmp._p_w_picpath = (this->_p_w_picpath*c._real - this->_real * c._p_w_picpath)/(c._real *c._real + c._p_w_picpath *c._p_w_picpath);
		return tmp;//临时变量
	}
	Comoplex& operator+=(const Comoplex &c)	 
	{
		this->_real += c._real;
		this->_p_w_picpath += c._p_w_picpath;
		return *this;
	}
	Comoplex& operator-=(const Comoplex &c)
	{
		this->_real -= c._real;
		this->_p_w_picpath -= c._p_w_picpath;
		return *this;
	}
	Comoplex& operator*=(const Comoplex &c)
	{
		this->_real = this->_real * c._real - this->_p_w_picpath * c._p_w_picpath;
		this->_p_w_picpath = this->_real * c._p_w_picpath + this->_p_w_picpath * c._real;
		return *this;
	}
	Comoplex& operator/=(const Comoplex &c)
	{
		this->_real = (this->_real *c._real +this ->_p_w_picpath * c._p_w_picpath)/(c._real *c._real + c._p_w_picpath *c._p_w_picpath);
		this->_p_w_picpath = (this->_p_w_picpath*c._real - this->_real * c._p_w_picpath)/(c._real *c._real + c._p_w_picpath *c._p_w_picpath);
		return *this;
	}
	Comoplex& operator++()//前置++
	{
		this->_real++;
		this->_p_w_picpath++;
		return *this;
	}
	Comoplex operator++(int)//后置++
	{
		Comoplex tmp(*this);
		this->_real++;
		this->_p_w_picpath++;
		return tmp;//临时变量	
	}
	Comoplex& operator--()//前置--
	{
		this->_real--; 
		this->_p_w_picpath--;
		return *this;
	}
	Comoplex operator--(int)//后置--
	{
		Comoplex tmp(*this);
		this->_real--;
		this->_p_w_picpath--;
		return tmp;//临时变量
	}
private:
	double _real;
	double _p_w_picpath;
};

ostream& operator<<(ostream& os ,const Comoplex& c)  //输出运算符重载	  
{
	os << c._real << "+" << c._p_w_picpath << "i" <<endl;
	return os;
}


【测试代码】

//测试
int main()
{
	Comoplex c1(2,3);
	Comoplex c2(3,4);
	Comoplex c3 = c1 + c2;
	//Comoplex c4 = c1 - c2;
	//Comoplex c5 = c1 * c2;
	//Comoplex c6 = c1 / c2;
	//Comoplex c7 = c1 += c2;
	//Comoplex c8 = c1 -= c2;
	//Comoplex c9 = c1 *= c2;
	//Comoplex c10 = c1 /= c2;
	bool ret = (c1 == c2);
	if (ret)
	{
		cout<< "c1 == c2"<< endl;
	}
	else
	{
		cout << "c1 != c2" << endl;
	}
	cout <<"c1="<<c1;
	cout <<"c2="<<c2;
	cout <<"c3="<<c3;
	//cout <<"c4="<<c4;
	//cout <<"c5="<<c5;
	//cout <<"c6="<<c6;
	//cout <<"c7="<<c7;
	//cout <<"c8="<<c8;
	//cout <<"c9="<<c9;
	//cout <<"c10="<<c10;
	system("pause");
	return 0;
}


【测试结果】


wKiom1bS99bgyVEMAAAvB3yBqMI439.png


wKioL1bS-EuS6CvwAAATN9oxEpI526.png


wKioL1bS-EvyhsPEAAASflnQOw4180.png


wKiom1bS99eScQadAAAPKbc6qYY164.png


wKiom1bS99eS2N7RAAAQKVwxOzg783.png


wKioL1bS-EyySuYMAAAUg8OMCqU854.png


wKioL1bS-EzBFL9FAAAQS7ekacs782.png


wKiom1bS99iiBKhcAAAPNdaRSAw106.png