1011. 复数类

http://acm.sjtu.edu.cn/OnlineJudge/problem/1011

 

学习了C++重载的写法。

 

  1 # include <iomanip>
  2 # include <iostream>
  3 
  4 using namespace std;
  5 
  6 class MyComplex{
  7     double real;
  8     double imag;
  9 
 10     friend istream &operator>>(istream & input, MyComplex & z) {
 11         input >> z.real >> z.imag ;
 12         return input;
 13     }
 14     friend ostream &operator<<(ostream & output, const MyComplex & z) {
 15         output.precision(2);
 16         output << fixed << z.real << ' ' << fixed << z.imag ;
 17         return output;
 18     }
 19 
 20 public:
 21 //    MyComplex(){
 22 //        real = 0;
 23 //        imag = 0;
 24 //    }
 25     MyComplex(double real = 0, double imag = 0) {
 26         this->real = real;
 27         this->imag = imag;
 28     }
 29     const MyComplex operator+(MyComplex &);
 30     const MyComplex operator-(MyComplex &);
 31     const MyComplex operator*(MyComplex &);
 32     const MyComplex operator/(MyComplex &);
 33     const MyComplex operator+=(MyComplex &);
 34     const MyComplex operator-=(MyComplex &);
 35     const MyComplex operator*=(MyComplex &);
 36     const MyComplex operator/=(MyComplex &);
 37 };
 38 
 39 const MyComplex MyComplex::operator+(MyComplex &rz)
 40 {
 41     MyComplex ret;
 42     ret.real = real + rz.real;
 43     ret.imag = imag + rz.imag;
 44     return ret;
 45 }
 46 
 47 const MyComplex MyComplex::operator-(MyComplex & rz)
 48 {
 49     MyComplex ret;
 50     ret.real = real - rz.real;
 51     ret.imag = imag - rz.imag;
 52     return ret;
 53 }
 54 
 55 const MyComplex MyComplex::operator*(MyComplex & rz)
 56 {
 57     MyComplex ret;
 58     ret.real = real*rz.real - imag*rz.imag;
 59     ret.imag = real*rz.imag + rz.real*imag;
 60     return ret;
 61 }
 62 
 63 const MyComplex MyComplex::operator/(MyComplex & rz)
 64 {
 65     MyComplex ret;
 66     double m2 = rz.real*rz.real + rz.imag*rz.imag;
 67     ret.real = real*rz.real + imag*rz.imag;
 68     ret.imag = rz.real*imag - real*rz.imag;
 69     ret.real /= m2;
 70     ret.imag /= m2;
 71     return ret;
 72 }
 73 
 74 const MyComplex MyComplex::operator+=(MyComplex & rz)
 75 {
 76 
 77     return (*this) = (*this) + rz;
 78 }
 79 
 80 const MyComplex MyComplex::operator-=(MyComplex & rz)
 81 {
 82     return (*this) = (*this) - rz;
 83 }
 84 
 85 const MyComplex MyComplex::operator*=(MyComplex & rz)
 86 {
 87     return (*this) = (*this) * rz;
 88 }
 89 
 90 const MyComplex MyComplex::operator/=(MyComplex & rz)
 91 {
 92     return (*this) = (*this) / rz;
 93 }
 94 
 95 int main()
 96 {
 97     MyComplex z1;
 98     MyComplex z2;
 99 
100     cin >> z1 >> z2;
101 
102     cout << z1 + z2 << endl;
103     cout << z1 - z2 << endl;
104     cout << z1 * z2 << endl;
105     cout << z1 / z2 << endl;
106     cout << (z1 += z2) << endl;
107     cout << (z1 -= z2) << endl;
108     cout << (z1 *= z2) << endl;
109     cout << (z1 /= z2) << endl;
110 
111     return 0;
112 }

 

转载于:https://www.cnblogs.com/txd0u/p/3358319.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值