第一次数据结构留的作业,练练手而已。
#include <iostream.h>
class ComplexNumber
{
int real;
int vain;
public:
ComplexNumber(){real=0;vain=0;}
ComplexNumber(int a,int b){real=a;vain=b;}
void setReal(int Get);
void setVain(int Get);
ComplexNumber operator+(ComplexNumber &second);
ComplexNumber operator-(ComplexNumber &second);
void disp();
};
void ComplexNumber::setReal(int Get)
{
real=Get;
}
void ComplexNumber::setVain(int Get)
{
vain=Get;
}
void ComplexNumber::disp()
{
if(real!=0)
{
if(vain>0)
{
cout<<real<<'+'<<vain<<"i\n";
}
else if(vain<0)
{
cout<<real<<vain<<"i\n";
}
else
{
cout<<real<<"\n";
}
}
if(real==0)
{
if(vain!=0)
{
cout<<vain<<"i\n";
}
else
{
cout<<real<<"\n";
}
}
}
ComplexNumber ComplexNumber::operator+(ComplexNumber &second)
{
return ComplexNumber(real+second.real,vain+second.vain);
}
ComplexNumber ComplexNumber::operator-(ComplexNumber &second)
{
return ComplexNumber(real-second.real,vain-second.vain);
}
void main()
{
ComplexNumber a(2,1);
ComplexNumber b(3,3);
ComplexNumber c;
c=a+b;
c.disp();
c=a-b;
c.disp();
}