前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >operator重载之复数计算—C++

operator重载之复数计算—C++

作者头像
WindCoder
发布2018-09-19 18:25:22
5930
发布2018-09-19 18:25:22
举报
文章被收录于专栏:WindCoder
代码语言:javascript
复制
/*
功能:复数计算类
作者:wind
日期:2013-11-29
*/
#include <iostream>
#include <string>
using namespace std;
/************************************************************/
class Complex
{
public:
	Complex()
	{
		m_iX = 0;
		m_iY = 0;
	}
	Complex(double aX,double aY);
	void setX(double aX);
	double getX()const;
	void setY(double aY);
	double getY()const;
	Complex operator + (Complex c1);
	Complex operator - (Complex c1);
    void display(Complex c1);
private:
	double m_iX;
	double m_iY;
};
/************************************************************/

void Complex::setX(double aX)
{
	m_iX = aX;
}
double Complex::getX()const
{
	return m_iX;
}
void Complex::setY(double aY)
{
	m_iY = aY;
}
double Complex::getY()const
{
	return m_iY;
}

Complex::Complex(double aX,double aY)
{
	m_iX =  aX;
	m_iY =  aY;
}

Complex Complex::operator + (Complex c1)
{
	Complex c;
	c.m_iX = m_iX + c1.m_iX;
	c.m_iY = m_iY + c1.m_iY;
	return c;
}
Complex Complex::operator - (Complex c1)
{
	Complex c;
	c.m_iX = m_iX - c1.m_iX;
	c.m_iY = m_iY - c1.m_iY;
	return c;
}
void Complex::display(Complex c1)
{
     string n;
	 Complex c;
	 cout<<"Please input the sign(-/+):";
	 cin>>n;
	 if  ("-" == n)
	 {
		 c = operator- (c1);
		 cout<<"The result is"<<"("<<c.m_iX<<","<<c.m_iY<<")"<<endl;
	 }
	 else if ("+" == n)
	 {
		 c = operator+ (c1);
		 cout<<"The result is"<<"("<<c.m_iX<<","<<c.m_iY<<")"<<endl;
	 }
	 else
	 {
         cout<<"The operator is error";
	 }

}
/************************************************************/

int main(void)
{

	Complex c1,c2;
	double aX,aY;

	cout<<"请输入第一个复数:(实部,虚部):";
	cin>>aX>>aY;
	c1.setX(aX);
	c1.setY(aY);

	cout<<"请输入第二个复数:(实部,虚部):";
	cin>>aX>>aY;
	c2.setX(aX);
	c2.setY(aY);

	c1.display(c2);

	system("pause");
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014-11-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档