简易复数类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简易复数类相关的知识,希望对你有一定的参考价值。

【问题描述】


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


【代码实现】

// 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;
}


【测试结果】


技术分享


技术分享


技术分享


技术分享


技术分享


技术分享


技术分享


技术分享


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

以上是关于简易复数类的主要内容,如果未能解决你的问题,请参考以下文章

用java定义一个复数类Complex,能够创建复数对象,并且实现复数之间的加、减运算

C++--操作符重载 复数类

java 中实现复数的加减

Lua复数类算术

复数类的运算

简易前端模板引擎