运算符重载作业

#include <iostream>
using namespace std;
class complex{
 private:
  double real,image;
 public:
  complex(double r=0,double i=0){//两个参数的构造函数 
   real=r;image=i;
  }
  complex(const complex &c){//复制构造函数 
      real=c.real;
   image=c.image;
  }
  ~complex(){//析构函数 
   cout<<"*******xigou*******"<<endl; 
  }
 void print(){
 	if(image>=0) cout<<real<<"+"<<image<<"i"<<endl; 
    else cout<<real<<image<<"i"<<endl; 
 }
 friend complex operator-(complex &,const complex &);//重载+ 
 friend complex operator-=(complex &,const complex &);//重载-= 
 friend complex operator*=(complex &,const complex &);//重载*=
 friend complex operator/=(complex &,const complex &);//重载/=  
 friend complex operator++(complex &);//重载前置++ 
 friend complex operator++(complex &,int);//重载后置++ 
};
 complex operator-( complex &c1,const complex &c2){//重载+ 
  complex t(c1.real-c2.real,c1.image-c2.image);
    c1.real=t.real;c1.image=t.image;
  return c1;
 }
 complex operator-=(complex &c1,const complex &c2){//重载-=
   c1.real=c1.real-c2.real,c1.image=c1.image-c2.image;
   return c1;
 }
 complex operator*=(complex &c1,const complex &c2){//重载*=
  complex t(c1.real*c2.real-c1.image*c2.image,c1.real*c2.image+c2.real*c1.image);
  c1.real=t.real;c1.image=t.image;
  return c1;
 }
  complex operator/=(complex &c1,const complex &c2){//重载/=  
  complex t((c1.real*c2.real+c1.image*c2.image)/(c2.real*c2.real+c2.image*c2.image),
           (c1.real*c2.image+c2.real*c1.image)/(c2.real*c2.real+c2.image*c2.image));
 c1.real=t.real;c1.image=t.image;
  return c1;
}
complex operator++(complex &s){//重载前置++ 
 return complex(++s.real,++s.image);
} 
complex operator++(complex &c1,int){//重载后置++ 
 return complex(c1.real++,c1.image++);
}
 int main(){
  complex c1(1.0,1.0),c2(2.0,2.0),c3(3.0,3.0),c;
  cout<<"c1:";c1.print();
  cout<<"c2:";c2.print();
  cout<<"c3:";c3.print();
  cout<<"c:";c.print();
  c=c1-c2;c.print();
  c-=c1;c.print();
  c1*=c2;c1.print();
  c1/=c2;c1.print();
  c3++;c3.print();
  ++c3;c3.print();
  return 0;
 }
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Camellia.332

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值