简单c++实现复数的四则运算

#include
#include
#include

using namespace std;

class complex
{
private:
 double real;
 double unreal;
 

public:
   complex(void):real(0),unreal(0){};
   complex(double v_real,double v_unreal):real(v_real),unreal(v_unreal){};
   complex(const complex & comx){
                 this->real = comx.get_real();
                 this->unreal = comx.get_unreal();
                 };
   bool operator==(const complex& comx){
        if(this->real == comx.real && this->unreal == comx.unreal){
                      return true;
                      }
        return false;
        };
  //加法相关运算 (a+bi)+(c+di)=(a+c)+(b+d)i.
   complex operator+(const complex& comx) {
     complex result((this->get_real()+comx.get_real()),(this->get_unreal()+comx.get_unreal()));
     return result;
   };
   complex operator+(const double a) {
     complex tmp(a,0);
     return (*this)+tmp;
   };
  
  void operator+=(const complex& comx) {
 
     *this = (*this)+comx;
   };
  void operator+=(const double a) {
     complex tmp(a,0);
     *this += tmp;
   };
  
    //减法相关运算   (a+bi)-(c+di)=(a-c)+(b-d)i
   complex operator-(const complex& comx) {
     complex result((this->get_real()-comx.get_real()),(this->get_unreal()-comx.get_unreal()));
     return result;
   };
   complex operator-(const double a) {
     complex tmp(a,0);
     return (*this)-tmp;
   };
  
  void operator-=(const complex& comx) {
     *this = *this-comx;
   };
   void operator-=(const double a) {
    complex tmp(a,0);
     *this -= tmp;
   }; 
  
     //乘法相关运算   (a+bi)(c+di)=(ac-bd)+(bc+ad)i
   complex operator*(const complex& comx) {
     complex result((this->get_real())*(comx.get_real())-(this->get_unreal())*(comx.get_unreal()),
                    (this->get_unreal())*(comx.get_real())+(this->get_real())*(comx.get_unreal()));
     return result;
   };
   complex operator*(const double a) {
     complex tmp(a,0);
     return (*this)*tmp;
   };
  
   void operator*=(const complex& comx) {
     *this = (*this) *(comx);
   };
   void operator*=(const double a) {
     complex tmp(a,0);     
     *this *=tmp;
   }; 
  
    //除法相关运算   :(a+bi)/(c+di)=(a+bi)*(c-di)* (1/(c*c+d*d))
   complex operator/(const complex& comx){
      complex tmp(comx.get_real(),comx.get_unreal()*(-1));
      
      return (*this)*tmp*(1/(pow(comx.get_real(),2)+pow(comx.get_unreal(),2)));
           }        
   complex operator/(const double a){
      complex tmp(a,0);
      return (*this)/tmp;
           }
   void  operator/=(const complex& comx){
      *this =  (*this)/comx;
           }       
          
   void  operator/=(const double a){
      complex tmp(a,0);
      *this/=tmp;
           }
  
  
  
  
    virtual ~complex(void){};
  double get_real() const {return this->real;};
  double get_unreal()const {return this->unreal;};
 
};

 

int main(int argc, char *argv[])
{
    complex a;
    complex c(5,0);
    complex b(3,3);
   
  //(a+bi)(c+di)=(ac-bd)+(bc+ad)i
   a = c*b;
  cout<   a=a/5;
  
   cout<    
   
   /* cout<     cout<     cout<    
  /*  if(a==c) cout<     a=b;
    if(a==b) {cout<     system("PAUSE");
    return EXIT_SUCCESS;
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/20625855/viewspace-1252297/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/20625855/viewspace-1252297/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值