Implementation of C + + complex complex class

Source: Internet
Author: User

#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 (n);
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&AMP;&AMP;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;
}

Implementation of C + + complex complex class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.