zoukankan      html  css  js  c++  java
  • C++ Complex复数类的实现

    #include "std_lib_facilities.h"


    using namespace std;

    class Complex
    {
       private:
         double real;
         double imag;
       public:
         Complex(){real=0;imag=1;}
         Complex(double r,double i):real(r),imag(i){}
         Complex(const Complex &tmp):real(tmp.real),imag(tmp.imag){}
         void set_real(double r);
         void set_imag(double i);
         void set_complex(double r,double i);
         double get_real() const;
         double get_imag() const;
         Complex& operator+=(const Complex &tmp);
         Complex& operator+=(const double r);
         Complex& operator-=(const Complex &tmp);
         Complex& operator-=(const double r);
         Complex& operator=(const Complex &tmp);
         Complex& operator=(const double r);
         bool operator==(const Complex &tmp) const;
         bool operator>=(const Complex &tmp) const;
         bool operator<=(const Complex &tmp) const;
         bool operator!=(const Complex &tmp) const;
         bool operator>(const Complex &tmp) const;
         bool operator<(const Complex &tmp) const;
         double operator!();
         friend Complex operator+(const Complex &tmp,const Complex &temp);
         friend Complex operator+(const Complex &tmp,const double r);
         friend Complex operator+(const double r,const Complex &tmp);
         friend Complex operator-(const Complex &tmp,const Complex &temp);
         friend Complex operator-(const Complex &tmp,const double r);
         friend Complex operator-(const double r,const Complex &tmp);
         friend Complex operator*(const Complex &tmp,const Complex &temp);
         friend Complex operator*(const Complex &tmp,const double r);
         friend Complex operator*(const double r,const Complex &tmp);
         friend Complex operator/(const Complex &tmp,const Complex &temp);
         friend Complex operator/(const Complex &tmp,const double r);
         friend Complex operator/(const double r,const Complex &tmp);
         friend istream& operator>>(istream &is ,Complex &tmp);
         friend ostream& operator<<(ostream &os,const Complex &tmp);
    };

    int main()
    {
       Complex p(1,1);
       Complex q(2,2);
       Complex t(3,3);
       double test;

       cout << p+q;
       cout << p-q;
       cout << p*q ;
       cout << p/q ;
       test=(!p);
       cout << test << endl;
       cout << (p==q) << endl;
       cout << (p!=q) << endl;
       cout << (p>=q) << endl;
       cout << (p>q) << endl;
       cout << (p<q) << endl;
       cout << (p<=q) << endl;
       t+=q;
       cout << t;
       t-=p;
       cout << t;
       cout << 10+p;
       cout << p+10;
       cout << 10-p;
       cout << p-10;
       cout << 10*p;
       cout << p*10;
       cout << 10/p;
       cout << p/10;
       cin >> t;
       cout << t;
       return 0;
    }

    void Complex::set_real(double r)
    {
       real=r;
       return;
    }

    void Complex::set_imag(double i)
    {
       imag=i;
       return;
    }

    void Complex::set_complex(double r,double i)
    {
       real=r;
       imag=i;
       return;
    }

    inline double Complex::get_real() const
    {
        return(real);
    }

    inline double Complex::get_imag() const
    {
       return(imag);
    }

    inline Complex& Complex::operator+=(const Complex& tmp)
    {
       real+=tmp.real;
       imag+=tmp.imag;
       return *this;
    }

    inline Complex& Complex::operator+=(double r)
    {
       real+=r;
       return *this;
    }

    inline Complex& Complex::operator-=(const Complex& tmp)
    {
       real-=tmp.real;
       imag-=tmp.imag;
       return *this;
    }

    inline Complex& Complex::operator-=(double r)
    {  
       real-=r;
       return *this;
    }


    inline Complex& Complex::operator=(const Complex& tmp)
    {
       real=tmp.real;
       imag=tmp.imag;
       return *this;
    }

    inline Complex& Complex::operator=(double r)
    {
      real=r;
      imag=0;
      return *this;
    }

    inline Complex operator+(const Complex &tmp,const Complex &temp)
    {    
       Complex result;
       result.real=tmp.real+temp.real;
       result.imag=tmp.imag+temp.imag;
       return result;
    }

    inline Complex operator+(const Complex &tmp,const double r)
    {       
       Complex result;
       result.real=tmp.real+r;
       result.imag=tmp.imag;
       return result;
    }

    inline Complex operator+(const double r,const Complex &tmp)
    {       
       Complex result;
       result.real=r+tmp.real;
       result.imag=tmp.imag;
       return result;
    }

    inline Complex operator-(const Complex &tmp,const Complex &temp)
    {
       Complex result;
       result.real=tmp.real-temp.real;
       result.imag=tmp.imag-temp.imag;
       return result;
    }


    inline Complex operator-(const Complex &tmp,const double r)
    {
        Complex result;
        result.real=tmp.real-r;
        result.imag=tmp.imag;
        return result;
    }

    inline Complex operator-(const double r,const Complex &tmp)
    {
        Complex result;
        result.real=r-tmp.real;
        result.imag=0-tmp.imag;
        return result;
    }

    inline Complex operator*(const Complex &tmp,const Complex& temp)
    {
        Complex result;
        result.real=temp.real*tmp.real-temp.imag*tmp.imag;
        result.imag=temp.real*tmp.imag+temp.imag*tmp.real;
        return result;
    }

    inline Complex operator*(const Complex &tmp,const double r)
    {
        Complex result;
        result.real=tmp.real*r;
        result.imag=tmp.imag*r;
        return result;
    }

    inline Complex operator*(const double r,const Complex &tmp)
    {
        Complex result;
        result.real=r*tmp.real;
        result.imag=r*tmp.imag;
        return result;
    }

    inline Complex operator/(const Complex &tmp,const Complex &temp)
    {
        Complex result;
        if(temp.real==0&&temp.imag==0)
        {
           cout << "error!" << endl;
           return result;
        }
        result.real=(tmp.real*temp.real+tmp.imag*temp.imag)/(temp.real*temp.real+temp.imag*temp.imag);
        result.imag=(tmp.imag*temp.real-tmp.real*temp.imag)/(temp.real*temp.real+temp.imag*temp.imag);
        return result;
    }

    inline Complex operator/(const Complex &tmp,const double r)
    {
       Complex result;
       if(r==0)
       {
          cout << "error!" << endl;
          return result;
       }
       result.real=tmp.real/r;
       result.imag=tmp.imag/r;
       return result;
    }

    inline Complex operator/(const double r,const Complex &tmp)
    {
       Complex result;
       if(tmp.real==0&&tmp.imag==0)
       {
         cout << "error" << endl;
         return result;
       }
       result.real=r*tmp.real/(tmp.real*tmp.real+tmp.imag*tmp.imag);
       result.imag=(0-r*tmp.imag)/(tmp.real*tmp.real+tmp.imag+tmp.imag);
       return result;
    }

    inline double Complex::operator!()
    {
       return sqrt(real*real+imag*imag);
    }

    inline bool Complex::operator==(const Complex &tmp) const
    {
       if(real==tmp.real&&imag==tmp.imag)
          return true;
       else
          return false;
    }

    inline bool Complex::operator!=(const Complex &tmp) const
    {
       if(*this==tmp)
         return false;
       else
         return true;
    }

    inline bool Complex::operator>=(const Complex &tmp) const
    {
       if((real*real+imag*imag)>=(tmp.real*tmp.real+tmp.imag*tmp.imag))
         return true;
       else
        return false;
    }

    inline bool Complex::operator>(const Complex &tmp) const
    {
       if((real*real+imag*imag)>(tmp.real*tmp.real+tmp.imag*tmp.imag))
         return true;
       else
         return false;
    }

    inline bool Complex::operator<=(const Complex &tmp) const
    {
      if((real*real+imag*imag)<=(tmp.real*tmp.real+tmp.imag*tmp.imag))
         return true;
      else
         return false;
    }

    inline bool Complex::operator<(const Complex &tmp) const
    {
      if((real*real+imag*imag)<(tmp.real*tmp.real+tmp.imag*tmp.imag))
         return true;
      else
         return false;
    }

    inline ostream& operator<<(ostream& os,const Complex& tmp)
    {
       os << tmp.real << "+" << tmp.imag << "j" << endl;
       return os;
    }

    inline istream& operator>>(istream &is,Complex& tmp)
    {
       cout << "enter real:";
       is >> tmp.real;
       if(is)
       {
          cout << "enter imag:";
          is >> tmp.imag;
          if(is)
          {
            return is;
          }
          else
          {
             cout << "error!" << endl;
         tmp=Complex(0,1);

          }
       }
       else
       {
          tmp=Complex(0,1);
       }
       return is;
    }
  • 相关阅读:
    你了解JWT吗?
    链接
    C#读取EXCEL发生错误
    TM1637驱动数码管
    Keil中的Code,RO,RW,ZI分别表示什么
    IE 不支持 promise 解决方法
    JS 时间戳转日期格式
    JS input 输入框只能输入 字母和汉字
    小程序 保存图片失败
    小程序充值,方法步骤
  • 原文地址:https://www.cnblogs.com/ustczd/p/5008407.html
Copyright © 2011-2022 走看看