⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 complex.cpp

📁 用于声音图像的FFC变换源码
💻 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);
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -