码迷,mamicode.com
首页 > 编程语言 > 详细

c++实现复数类的输入输出流以及+-*重载

时间:2020-05-26 00:57:46      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:ble   com   color   ios   real   har   输出   实现   get   

c++实现复数类的输入输出流以及+ - *的重载

  1 #include <iostream>
  2 using namespace std;
  3 class Complex
  4 {
  5 public:
  6     Complex():real(0),image(0){}
  7     Complex(double a, double b):real(a),image(b){}
  8     operator double(){
  9         if(real==0&&image==0)
 10         return 0;
 11         else
 12         return 1;
 13     }
 14     friend Complex operator *(Complex&, Complex&);
 15     friend Complex operator +(Complex&, Complex&);
 16     friend Complex operator -(Complex&, Complex&);
 17     friend ostream& operator <<(ostream&, Complex&);
 18     friend istream& operator >>(istream&, Complex&);
 19 private:
 20     double real;
 21     double image;
 22 };
 23 Complex operator +(Complex& a, Complex& b)
 24 {
 25     Complex c(a.real + b.real,a.image + b.image);
 26     return c;
 27 }
 28 Complex operator -(Complex& a, Complex& b)
 29 {
 30     Complex c(a.real - b.real,a.image - b.image);
 31     return c;
 32 }
 33 Complex operator *(Complex& a, Complex& b)
 34 {
 35     Complex c(a.real * b.real - a.image * b.image, a.real * b.image + a.image * b.real);
 36     return c;
 37 }
 38 
 39 istream& operator >>(istream& is, Complex& cp)
 40 {
 41     is.clear();
 42     char t,t2;
 43     double a,b;
 44     t2=is.peek();
 45     if(t2== ||t2==\n)
 46     {
 47         getchar();
 48         t2=is.peek();
 49     }
 50     is>>a;
 51     t=getchar();
 52     if(t==i)
 53     {
 54         if(a)
 55         cp=Complex(0,a);
 56         else
 57         {
 58             if(t2==-)
 59             cp=Complex(0,-1);
 60             else
 61             cp=Complex(0,1);
 62         }
 63         return is;
 64     }
 65     else if(t==+||t==-)
 66     {
 67         is>>b;
 68         t2=getchar();
 69         if(b&&t==-)
 70         {
 71            b*=-1;
 72         }
 73         if(b)
 74         {
 75              cp=Complex(a,b);
 76             return is;
 77         }
 78         else
 79         {
 80             if(t==-)
 81              cp=Complex(a,-1);
 82              else
 83              cp=Complex(a,1);
 84             return is;
 85         }
 86     }
 87     else
 88     {
 89         cp=Complex(a,0);
 90         return is;
 91     }
 92 }
 93 ostream& operator <<(ostream& os, Complex& a)
 94 {
 95     if (a.image > 0)
 96     {
 97         if(a.real!=0)
 98         {
 99             if(a.image!=1)
100             os << a.real << "+" << a.image << "i";
101             else
102             os << a.real << "+" << "i";
103         }
104         else if(a.image==1)
105         os << "i";
106         else
107         os << a.image << "i";
108     }
109     else if(a.image<0)
110     {
111         if (a.real != 0)
112         {
113             if(a.image!=-1)
114             os << a.real  << a.image << "i";
115             else
116             os << a.real  << "-i";
117         }
118         else
119             if(a.image!=-1)
120             os << a.image << "i";
121             else
122             os << "-i";
123     }
124     else
125     {
126         os << a.real;
127     }
128     return os;
129 }

 

c++实现复数类的输入输出流以及+-*重载

标签:ble   com   color   ios   real   har   输出   实现   get   

原文地址:https://www.cnblogs.com/sanmuljl/p/12961962.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!