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

C++ Complex复数类的实现

时间:2015-11-30 22:09:10      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

#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;
}

C++ Complex复数类的实现

标签:

原文地址:http://www.cnblogs.com/ustczd/p/5008407.html

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