Class Complex

///////////The delarcation///////////

#ifndef COMPLEX_H
#define COMPLEX_H

#include <iostream>
#include <fstream>

using namespace std;


class Complex
{
public:
 Complex(){real = 1; img = 1;}
 Complex(int nReal, int  nImg);
 ~Complex(){}
 Complex const operator ! ();
 Complex&  operator ++ ();
 Complex const operator ++ (int);
 Complex const operator -- ();
 Complex const operator -- (int);
 friend Complex const operator += ( Complex &x,  Complex &y)
 {
  x = x + y;
  return x;
 }
 Complex const operator + (Complex &x);
 Complex const operator - (Complex &x);
 friend Complex const operator -= ( Complex &x,  Complex &y)
 {
  x = x - y;
  return x;
 }
 double const operator * (Complex &x);
 bool const operator > (Complex &x);
 bool const operator < (Complex &x);
 bool const operator == (const Complex &x);
 friend Complex const operator / (const Complex &x,const Complex &y)
 { 
  if (y.real == 0 && y.img == 0)
  {
   cerr<<"Divled by zero!/n";
   exit(1);
  }
  double temReal=0,temImg=0;
  temReal = (y.real * x.real - y.img * x.img) / ( y.real * y.real - y.img * y.img);
  temImg = y.real * x.img - x.real * y.img ;
  return Complex ( temReal, temImg);
 }
 friend const ostream & operator << (ostream &out, const Complex &x)
 {
  out<<"Number: "<<x.real<<"+";
  out<<((x.img>=0)?"":"(")<<x.img<<(x.img>=0?"":")")<<"i"<<endl;
  return out;
 }
 friend const istream & operator >> (istream &in, Complex &x)
 {
  in>>x.real>>x.img;
  return in;
 }
 void ShowInfo();
private:
 int real;
 int img;
};

 

/*ostream & operator << (ostream &out, const Complex &x)
{
 out<<"Number: "<<x.real<<"+"<<x.img<<"i"<<endl;
 return out;
}

istream & operator >> (istream &in, Complex &x)
{
 in>>x.real>>x.img;
 return in;
}*/

#endif

////the end of file//////////

 

 

////////the definition///////////

#include "Complex.h"
#include <math.h>


Complex::Complex(int  nReal, int nImg)
{
 real = nReal;
 img = nImg;
}

void Complex::ShowInfo()

 if (img > 0 )
 {
  cout<<"Number :"<<real<<"+"<<img<<"i"<<endl;
 }
 else
  cout<<"Number :"<<real<<"+"<<"("<<img<<")"<<"i"<<endl;
}

Complex const Complex::operator +(Complex &x)
{
 Complex tem;
 tem.real = this->real + x.real;
 tem.img = this->img + x.img;
 return tem;
}

Complex const Complex::operator - (Complex &x)
{
 Complex tem;
 tem.real = this->real - x.real;
 tem.img = this->img - x.img;
 return tem;
}

Complex const Complex::operator ! ()
{
 return Complex(-this->real,-this->img);
}

double const Complex::operator * (Complex &x)

 double result=0;
 if( (this->img>0)&&(x.img>0) || (this->img<0)&&(x.img<0) ) //same value
 {
  result = this->real*x.real - this->img*x.img;
  return result;
 }
 else
  result =  this->real*x.real + this->img*x.img;
 return result;
}

bool const Complex::operator == (const Complex &x)
{
 if( (this->real == x.real) && (this->img == x.img))
  return true ;
 else
  return false;
}

Complex const  Complex::operator ++ (int)

 Complex temp(*this);
 real++;
 img++;
 return temp;
}

Complex &   Complex::operator ++()
{
 real++;
 img++;
 return *this;
}

Complex const Complex::operator --()
{
 real--;
 img--;
 return *this;
}

Complex const Complex::operator --(int)
{
 Complex temp(*this);
 real--;
 img--;
 return temp;
}

bool const Complex::operator < (Complex &x)
{
 if( ( pow(this->real,2) - pow(this->img,2) )
  < ( pow(x.real,2) - pow(x.img,2) ) )
  return true;
 else
  return false;
}

bool const Complex::operator > (Complex &x)
{
 if( ( pow(this->real,2) - pow(this->img,2) )
  > ( pow(x.real,2) - pow(x.img,2) ) )
  return true;
 else
  return false;
}

 

 

 

///Test Function////////

#include "Complex.h"

void main()
{
 Complex my(1,2),you(2,-5);
 Complex result;
 Complex test1(1,0),test2(2,0);
 result = my + you;
 my.ShowInfo();
 you.ShowInfo();
 result.ShowInfo();
 cout<<my;
 cout<<you;
 cout<<!result;
 cout<<((my == you)?"Right!/n":"Wrong!/n");
 result = test1/my;
 cout<<result;
 cout<<my;
 cout<<you;
 my+=you;
 cout<<my;
 cin>>my;
 my-=you;
 cout<<my;
 Complex *ptCplex = new Complex[100];
 for ( int i = 0; i < 100; i++)
 { 
  //ptCplex[i].real = i * 10;
  //ptCplex[i].img = i * i ;
  ptCplex[i].ShowInfo();
 }
 delete []ptCplex;
 
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章