复数相加减(练手)

作者在 2006-09-04 00:07:00 发布以下内容

第一次数据结构留的作业,练练手而已。

 

#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();
}

c++开拓 | 阅读 1030 次
文章评论,共0条
游客请输入验证码