?? complex.cpp
字號:
// Complex.cpp: implementation of the CComplex class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Complex.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
const CComplex & CComplex::operator=(const CComplex & Rhs)
{
if(this != &Rhs)
{
Re = Rhs.Re;
Im = Rhs.Im;
}
return *this;
}
const CComplex & CComplex::operator=(const double & Rhs)
{
Re = Rhs;
Im = 0;
return *this;
}
const CComplex & CComplex::operator+=(const CComplex & Rhs)
{
Re += Rhs.Re;
Im += Rhs.Im;
return *this;
}
const CComplex & CComplex::operator+=(const double & Rhs)
{
Re += Rhs;
return *this;
}
const CComplex & CComplex::operator-=(const CComplex & Rhs)
{
Re -= Rhs.Re;
Im -= Rhs.Im;
return *this;
}
const CComplex & CComplex::operator-=(const double & Rhs)
{
Re -= Rhs;
return *this;
}
const CComplex & CComplex::operator*=(const CComplex & Rhs)
{
double tempRe = Re;
double tempIm = Im;
Re = tempRe * Rhs.Re - tempIm * Rhs.Im;
Im = tempRe * Rhs.Im + tempIm * Rhs.Re;
return *this;
}
const CComplex & CComplex::operator*=(const double & Rhs)
{
Re *= Rhs;
Im *= Rhs;
return *this;
}
const CComplex & CComplex::operator/=(const CComplex & Rhs)
{
if(Rhs == 0)
{
//cerr << "Divisor cannot be zero!" << endl;
}
else
{
double tempRe = Re;
double tempIm = Im;
double RhsAbs = pow(Rhs.Re, 2.0) + pow(Rhs.Im, 2.0);
Re = (tempRe * Rhs.Re + tempIm * Rhs.Im) / RhsAbs;
Im = (tempIm * Rhs.Re - tempRe * Rhs.Im) / RhsAbs;
}
return *this;
}
const CComplex & CComplex::operator/=(const double & Rhs)
{
if(Rhs == 0)
{
//cerr << "Divisor cannot be zero!" << endl;
}
else
{
Re /= Rhs;
Im /= Rhs;
}
return *this;
}
CComplex CComplex::operator+(const CComplex & Rhs) const
{
CComplex Answer(*this);
Answer += Rhs;
return Answer;
}
CComplex CComplex::operator+(const double & Rhs) const
{
CComplex Answer(*this);
Answer += Rhs;
return Answer;
}
CComplex CComplex::operator-(const CComplex & Rhs) const
{
CComplex Answer(*this);
Answer -= Rhs;
return Answer;
}
CComplex CComplex::operator-(const double & Rhs) const
{
CComplex Answer(*this);
Answer -= Rhs;
return Answer;
}
CComplex CComplex::operator*(const CComplex & Rhs) const
{
CComplex Answer(*this);
Answer *= Rhs;
return Answer;
}
CComplex CComplex::operator*(const double & Rhs) const
{
CComplex Answer(*this);
Answer *= Rhs;
return Answer;
}
CComplex CComplex::operator/(const CComplex & Rhs) const
{
CComplex Answer(*this);
Answer /= Rhs;
return Answer;
}
CComplex CComplex::operator/(const double & Rhs) const
{
CComplex Answer(*this);
Answer /= Rhs;
return Answer;
}
bool CComplex::operator!=(const CComplex & Rhs) const
{
return (Re != Rhs.Re || Im != Rhs.Im);
}
bool CComplex::operator!=(const double & Rhs) const
{
return (Re != Rhs || Im != 0);
}
bool CComplex::operator==(const CComplex & Rhs) const
{
return (Re == Rhs.Re && Im == Rhs.Im);
}
bool CComplex::operator==(const double & Rhs) const
{
return (Re == Rhs && Im == 0);
}
CComplex CComplex::operator-() const
{
return CComplex(-Re, -Im);
}
const CComplex & CComplex::Rotate(double angle)
{
CComplex multi(cos(angle), sin(angle));
*this *= multi;
return *this;
}
const CComplex & CComplex::Normalize()
{
Re /= GetAbs();
Im /= GetAbs();
return *this;
}
double CComplex::GetAbs()
{
return sqrt(pow(Re, 2.0) + pow(Im, 2.0));
}
double CComplex::GetComplexAngle()
{
double angle = asin(fabs(sin(Re / GetAbs())));
if(Re >= 0 && Im >= 0)
return angle;
else if(Re <= 0 && Im >= 0)
return (PI - angle);
else if(Re <= 0 && Im <= 0)
return (PI + angle);
else if(Re >= 0 && Im <= 0)
return (2 * PI - angle);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -