简易复数类

【问题描述】


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


【代码实现】

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

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

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

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

ostream& operator<<(ostream& os ,const Comoplex& c)  //输出运算符重载	  
{
	os << c._real << "+" << c._image << "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


本文出自 “Pzd流川枫” 博客,请务必保留此出处http://xujiafan.blog.51cto.com/10778767/1745839

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值