zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    dedecms 5.7 站点文件从本地子目录上传到远程根目录后找不到模板的解决方案
    Dedecms实现"文章标题2级栏目1级栏目网站名"
    织梦安装在子目录会出现问题的解决技巧集合
    ASP.NET三层架构中数据层数据访问类部分代码
    织梦DEDECMS缩短URL路径长度的方法
    CSS各种属性全集
    css和js引用图片路径
    ASP.Net中FileUpLoad控件内容清空
    WampServer的配置
    asp.net 中的 callback
  • 原文地址:https://www.cnblogs.com/txd0u/p/3358319.html
Copyright © 2011-2022 走看看