?? capp.cpp
字號:
#include "stdafx.h"
#include "cheader.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// 基本構造函數
//////////////////////////////////////////////////////////////////////
CComplex::CComplex()
{
m_dblX = 0.0;
m_dblY = 0.0;
}
//////////////////////////////////////////////////////////////////////
// 指定值構造函數
//
// 參數:
// 1. double dblX - 指定的實部
// 2. double dblY - 指定的虛部
//////////////////////////////////////////////////////////////////////
CComplex::CComplex(double dblX, double dblY)
{
m_dblX = dblX;
m_dblY = dblY;
}
//////////////////////////////////////////////////////////////////////
// 拷貝構造函數
//
// 參數:
// 1. const CComplex& other - 源復數
//////////////////////////////////////////////////////////////////////
CComplex::CComplex(const CComplex& other)
{
m_dblX = other.m_dblX;
m_dblY = other.m_dblY;
}
//////////////////////////////////////////////////////////////////////
// 指定復數的實部
//
// 參數:
// 1. double dblX - 復數的實部
//////////////////////////////////////////////////////////////////////
void CComplex::SetReal(double dblX)
{
m_dblX = dblX;
}
//////////////////////////////////////////////////////////////////////
// 指定復數的虛部
//
// 參數:
// 1. double dblX - 復數的虛部
//////////////////////////////////////////////////////////////////////
void CComplex::SetImag(double dblY)
{
m_dblY = dblY;
}
//////////////////////////////////////////////////////////////////////
// 取復數的實部
//
// 參數: 無
//
// 返回值:double 型,復數的實部
//////////////////////////////////////////////////////////////////////
double CComplex::GetReal()
{
return m_dblX;
}
//////////////////////////////////////////////////////////////////////
// 取復數的虛部
//
// 參數: 無
//
// 返回值:double 型,復數的虛部
//////////////////////////////////////////////////////////////////////
double CComplex::GetImag()
{
return m_dblY;
}
//////////////////////////////////////////////////////////////////////
// 將復數轉化為"a+bj"形式的字符串
//
// 參數: 無
//
// 返回值:CString 對象,"a+bj"形式的字符串
//////////////////////////////////////////////////////////////////////
CString CComplex::ToString() const
{
CString s;
if (m_dblX != 0.0)
{
if (m_dblY > 0)
s.Format("%f + %fi", m_dblX, m_dblY);
else if (m_dblY < 0)
s.Format("%f - %fi", m_dblX, fabs(m_dblY));
else
s.Format("%f", m_dblX);
}
else
{
if (m_dblY > 0)
s.Format("%fi", m_dblY);
else if (m_dblY < 0)
s.Format("-%fi", fabs(m_dblY));
else
s.Format("%f", m_dblX);
}
return s;
}
//////////////////////////////////////////////////////////////////////
// 將"a,b"形式的字符串轉化為復數,以a為復數的實部,b為復數的虛部
//
// 參數:
// 1. CString s - "a,b"形式的字符串,a為復數的實部,b為復數的虛部
// 2. const CString& sDelim - a, b之間的分隔符,默認為空格
//
// 返回值:無
//////////////////////////////////////////////////////////////////////
void CComplex::FromString(CString s, const CString& sDelim /*= " "*/)
{
int nPos = s.Find(sDelim);
if (nPos == -1)
{
s.TrimLeft();
s.TrimRight();
m_dblX = atof(s);
m_dblY = 0;
}
else
{
int nLen = s.GetLength();
CString sLeft = s.Left(nPos);
CString sRight = s.Right(nLen - nPos - 1);
sLeft.TrimLeft();
sRight.TrimRight();
m_dblX = atof(sLeft);
m_dblY = atof(sRight);
}
}
//////////////////////////////////////////////////////////////////////
// 重載運算符==,比較兩個復數是否相等
//
// 參數:
// 1. const CComplex& cpxX - 用于比較的復數
//
// 返回值:BOOL型,相等則為TRUE,否則為FALSE
//////////////////////////////////////////////////////////////////////
BOOL CComplex::operator==(const CComplex& cpxX) const
{
return (m_dblX == cpxX.m_dblX && m_dblY == cpxX.m_dblY);
}
//////////////////////////////////////////////////////////////////////
// 重載運算符!=,比較兩個復數是否不等
//
// 參數:
// 1. const CComplex& cpxX - 用于比較的復數
//
// 返回值:BOOL型,不相等則為TRUE,相等為FALSE
//////////////////////////////////////////////////////////////////////
BOOL CComplex::operator!=(const CComplex& cpxX) const
{
return (m_dblX != cpxX.m_dblX || m_dblY != cpxX.m_dblY);
}
//////////////////////////////////////////////////////////////////////
// 重載運算符=,給復數賦值
//
// 參數:
// 1. const CComplex& cpxX - 用于給復數賦值的源復數
//
// 返回值:CComplex型的引用,所引用的復數與cpxX相等
//////////////////////////////////////////////////////////////////////
CComplex& CComplex::operator=(const CComplex& cpxX)
{
m_dblX = cpxX.m_dblX;
m_dblY = cpxX.m_dblY;
return *this;
}
//////////////////////////////////////////////////////////////////////
// 重載運算符+,實現復數的加法
//
// 參數:
// 1. const CComplex& cpxX - 與指定復數相加的復數
//
// 返回值:CComplex型,指定復數與cpxX相加之和
//////////////////////////////////////////////////////////////////////
CComplex CComplex::operator+(const CComplex& cpxX) const
{
double x = m_dblX + cpxX.m_dblX;
double y = m_dblY + cpxX.m_dblY;
return CComplex(x, y);
}
//////////////////////////////////////////////////////////////////////
// 重載運算符-,實現復數的減法
//
// 參數:
// 1. const CComplex& cpxX - 與指定復數相減的復數
//
// 返回值:CComplex型,指定復數減去cpxX之差
//////////////////////////////////////////////////////////////////////
CComplex CComplex::operator-(const CComplex& cpxX) const
{
double x = m_dblX - cpxX.m_dblX;
double y = m_dblY - cpxX.m_dblY;
return CComplex(x, y);
}
//////////////////////////////////////////////////////////////////////
// 重載運算符*,實現復數的乘法
//
// 參數:
// 1. const CComplex& cpxX - 與指定復數相乘的復數
//
// 返回值:CComplex型,指定復數與cpxX相乘之積
//////////////////////////////////////////////////////////////////////
CComplex CComplex::operator*(const CComplex& cpxX) const
{
double x = m_dblX * cpxX.m_dblX - m_dblY * cpxX.m_dblY;
double y = m_dblX * cpxX.m_dblY + m_dblY * cpxX.m_dblX;
return CComplex(x, y);
}
CComplex CComplex::operator/(const CComplex& cpxX) const
{
double e, f, x, y;
if (fabs(cpxX.m_dblX) >= fabs(cpxX.m_dblY))
{
e = cpxX.m_dblY / cpxX.m_dblX;
f = cpxX.m_dblX + e * cpxX.m_dblY;
x = (m_dblX + m_dblY * e) / f;
y = (m_dblY - m_dblX * e) / f;
}
else
{
e = cpxX.m_dblX / cpxX.m_dblY;
f = cpxX.m_dblY + e * cpxX.m_dblX;
x = (m_dblX * e + m_dblY) / f;
y = (m_dblY * e - m_dblX) / f;
}
return CComplex(x, y);
}
//////////////////////////////////////////////////////////////////////
// 計算復數的模
//
// 參數:無
//
// 返回值:double型,指定復數的模
//////////////////////////////////////////////////////////////////////
double CComplex::Abs() const
{
// 求取實部和虛部的絕對值
double x = fabs(m_dblX);
double y = fabs(m_dblY);
if (m_dblX == 0)
return y;
if (m_dblY == 0)
return x;
// 計算模
if (x > y)
return (x * sqrt(1 + (y / x) * (y / x)));
return (y * sqrt(1 + (x / y) * (x / y)));
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -