/*
功能:复数计算类
作者: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;
}