The Fourth Dimension Space

复数类模板（Complex Class)

#include<iostream>
#include
<cmath>
using namespace std;

//本模板在VS2005下可正常运行
/**//*//////////////////////////////BEGIN_TEMPLATE_CALSS_COMPLEX_BY_ABILITYTAO////////////////////////////*/
class Complex
{
private:

void show();

double real;

double image;
public:

Complex()
{real=0;image=0;}

~Complex(){}
Complex(
double a,double b){real=a;image=b;}

double Getreal(){return real;}

double Getimage(){return image;}

double abs(){return sqrt(real*real+image*image);}
Complex
operator +(Complex &other);
Complex
operator +(const double &other);
Complex
operator +(const int &other);

Complex
operator -(Complex &other);
Complex
operator -(const double &other);
Complex
operator -(const int &other);

Complex
operator *(Complex &other);
Complex
operator *(const double &other);
Complex
operator *(const int &other);

Complex
operator /(Complex &other);
Complex
operator /(const double &other);
Complex
operator /(const int &other);

void operator +=(Complex &other);

void operator +=(const double &other);

void operator +=(const int &other);

void operator -=(Complex &other);

void operator -=(const double &other);

void operator -=(const int &other);

void operator *=(Complex &other);

void operator *=(const double &other);

void operator *=(const int &other);

void operator /=(Complex &other);

void operator /=(const double &other);

void operator /=(const int &other);

Complex
operator =(Complex &other);
Complex
operator =(const double &other);
Complex
operator =(const int &other);

bool operator ==(Complex &other);

bool operator ==(const double &other);

bool operator ==(const int &other);

friend ostream
& operator<<(ostream &os,Complex &other);
friend istream
& operator>>(istream &is,Complex &other);
}
;

void Complex::show()
{

if(real>0&&image<0)
printf(
"%g%gj",real,image);

else if(real>0&&image>0)
printf(
"%g+%gj",real,image);

else if(real<0&&image>0)
printf(
"%g+%gj",real,image);

else if(real<0&&image<0)
printf(
"%g%gj",real,image);

else if(real==0&&image!=0)
printf(
"%gj",image);

else if(real!=0&&image==0)
printf(
"%g",real);

else
printf(
"0");
}

Complex Complex::
operator+(Complex &other)
{
Complex temp;
temp.real
=real+other.real;
temp.image
=image+other.image;

return temp;
}

Complex Complex::
operator +(const double &other)
{

Complex temp;
temp.real
=real+other;
temp.image
=image;

return temp;
}

Complex Complex::
operator +(const int &other)
{
Complex temp;
temp.real
=real+(double)other;
temp.image
=image;

return temp;
}

Complex Complex::
operator-(Complex &other)
{

Complex temp;
temp.real
=real-other.real;
temp.image
=image-other.image;

return temp;
}

Complex Complex::
operator -(const double &other)
{

Complex temp;
temp.real
=real-(double)other;
temp.image
=image;

return temp;
}

Complex Complex::
operator -(const int &other)
{

Complex temp;
temp.real
=real-(double)other;
temp.image
=image;

return temp;
}

Complex Complex::
operator*(Complex &other)
{
Complex temp;
temp.real
=(real*other.real-image*other.image);
temp.image
=(image*other.real+real*other.image);

return temp;

}

Complex Complex::
operator *(const double &other)
{
Complex temp;
temp.real
=real*other;
temp.image
=image*other;

return temp;
}

Complex Complex::
operator *(const int &other)
{
Complex temp;
temp.real
=real*(double)other;
temp.image
=image*(double)other;

return temp;
}

Complex Complex::
operator/(Complex &other)
{

Complex temp;
temp.real
=((real*other.real)+(image*other.image))/(other.real*other.real+other.image*other.image);
temp.image
=((image*other.real)-(real*other.image))/(other.real*other.real+other.image*other.image);

return temp;

}

Complex Complex::
operator /(const double &other)
{
Complex temp;
temp.real
=real/other;
temp.image
=image/other;

return temp;

}

Complex Complex::
operator /(const int &other)
{
Complex temp;
temp.real
=real/(double)other;
temp.image
=image/(double)other;

return temp;
}

void Complex::operator+=(Complex &other)
{

this->real+=other.real;

this->image+=other.image;
}

void Complex::operator +=(const double &other)
{

this->real+=other;
}

void Complex::operator +=(const int&other)
{

this->real+=(double)other;
}

void Complex::operator-=(Complex &other)
{

this->real-=other.real;

this->image-=other.image;
}

void Complex::operator -=(const double &other)
{

this->real-=other;
}

void Complex::operator -=(const int &other)
{

this->real-=(double)other;
}

void Complex::operator*=(Complex &other)
{

this->real=(real*other.real-image*other.image);

this->image=(image*other.real+real*other.image);;
}

void Complex::operator *=(const double &other)
{

this->real=real*other;

this->image=image*other;
}

void Complex::operator *=(const int &other)
{

this->real=real*(double)other;

this->image=image*(double)other;
}

void Complex::operator/=(Complex &other)
{

this->real=((real*other.real)+(image*other.image))/(other.real*other.real+other.image*other.image);

this->image=((image*other.real)-(real*other.image))/(other.real*other.real+other.image*other.image);
}

void Complex::operator /=(const double &other)
{

this->real=real/other;

this->image=image/other;
}

void Complex::operator /=(const int &other)
{

this->real=real/(double)other;

this->image=image/(double)other;
}

Complex Complex::
operator= (Complex &other)
{

this->real=other.real;

this->image=other.image;

return *this;

}

Complex Complex::
operator =(const double &other)
{

this->real=other;

this->image=image;

return *this;

}

Complex Complex::
operator =(const int &other)
{

this->real=(double)other;

this->image=image;

return *this;
}

bool Complex::operator ==(Complex &other)
{

if(this->real=other.real&&this->image==other.image)

return true;

else return false;
}

bool Complex::operator ==(const double &other)
{

if(this->real==other&&this->image==0)

return true;

else

return false;
}

bool Complex::operator ==(const int &other)
{

if(this->real==(double)other&&this->image==0)

return true;

else

return false;
}

ostream
& operator<<(ostream &os,Complex &other)
{
other.show();

return cout;
}

istream
& operator>>(istream &is,Complex &other)
{

is>>other.real;

is>>other.image;

return cin;
}

/**//**//**//////////////////////////////END_TEMPLATE_CLASS_COMPLEX///////////////////////////////

int main()
{
Complex test1(
10,6);
Complex test2(
5,3);
cout
<<test1*test2<<endl;
cout
<<test1+test2<<endl;
cout
<<test1/test2<<endl;
cout
<<test1-test2<<endl;

return 0;
}

posted on 2009-05-29 21:39 abilitytao 阅读(1463) 评论(0)  编辑 收藏 引用