vs2013中 c语言中复数怎么表示,[VC++原创]一个基于MFC(Visual C++)的复数计算器

#include "stdafx.h"

//  所有的文件都需要在"Complex.h"之前

#include "Complex.h"

#include

//  为了在debug中检查内存是否泄露

#ifdef _DEBUG

#undef THIS_FILE

static char THIS_FILE[]=__FILE__;

#define new DEBUG_NEW

#endif

#define PI 3.141592653

//  下面定义相应的函数

CComplex::CComplex()

{

m_dblX = 0.0;

m_dblY = 0.0;

}

CComplex::CComplex(double dblX , double dblY)

{

m_dblX = dblX;

m_dblY = dblY;

}

//  拷贝构造函数

CComplex::CComplex(const CComplex& other)

{

m_dblX = other.m_dblX;

m_dblY = other.m_dblY;

}

//  输入和显示

void CComplex::SetReal(double a)

{

m_dblX = a;

}

void CComplex::SetImag(double b)

{

m_dblY = b;

}

double CComplex::GetReal()

{

return m_dblX;

}

double CComplex::GetImag()

{

return m_dblY;

}

//  字符串的转换函数

CString CComplex::ToString() const

{

CString s;

if(fabs(m_dblX)>1e-10)

{

if(m_dblY>0.0)

{

s.Format(_T("%.3f + %.3fj"),m_dblX,m_dblY);

} else if (m_dblY<0.0)

{

s.Format(_T("%.3f - %.3fj"),m_dblX,fabs(m_dblY));

} else

{

s.Format(_T("%.3f"),m_dblX);

}

} else

//  判断小于0的情况

{

if(m_dblY>0.0)

{

s.Format(_T("%.3fj"),m_dblY);

} else if (m_dblY<0.0)

{

s.Format(_T("-%.3f"),fabs(m_dblY));

} else

{

s.Format(_T("%.3fj"),m_dblX);

}

}

return s;

}

void CComplex::FromString(CString s , const CString& sDelim)

{

int nPos = s.Find(sDelim);

if (nPos == -1)

{

s.TrimLeft();

s.TrimRight();

m_dblX = _wtof(s);

m_dblY = 0;

} else

{

int len = s.GetLength();

CString sLeft = s.Left(len);

CString sRight = s.Right(nPos - len - 1);

sLeft.TrimLeft();

sRight.TrimRight();

m_dblX = _wtof(sLeft);

m_dblY = _wtof(sRight);

}

}

//  相应的重载运算符

BOOL CComplex::operator==(const CComplex& cpxX) const

{

return(m_dblX == cpxX.m_dblX && m_dblY == cpxX.m_dblY);

}

BOOL CComplex::operator!=(const CComplex& cpxX) const

{

return(m_dblX != cpxX.m_dblX || m_dblY != cpxX.m_dblY);

}

CComplex& CComplex::operator=(const CComplex& cpxX)

{

m_dblX = cpxX.m_dblX;

m_dblY = cpxX.m_dblY;

return *this;

}

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);

}

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);

}

//  乘法

CComplex CComplex::operator*(const CComplex& cpxX) const

{

double a = m_dblX;

double b = m_dblY;

double c = cpxX.m_dblX;

double d = cpxX.m_dblY;

return(CComplex(a*c - b*d,a*d+b*c));

}

//  除法

CComplex CComplex::operator/(const CComplex& cpxX) const

{

double a = m_dblX;

double b = m_dblY;

double c = cpxX.m_dblX;

double d = cpxX.m_dblY;

double x,y;

x = (a*c + b*d)/(pow(c,2)+pow(d,2));

y = (b*c - a*d)/(pow(c,2)+pow(d,2));

return CComplex(x,y);

}

//  复数的模

double CComplex::Abs() const

{

double a,b,c;

a = pow(m_dblX,2);

b = pow(m_dblY,2);

c = sqrt(a+b);

return c;

}

//  复数的根

void CComplex::Root(CComplex cpxR[]) const

{

double q = atan2(m_dblX , m_dblY);

double r = sqrt(pow(m_dblX,2) + pow(m_dblY,2));

r = pow(r,0.5);

double t = 0.0;

for(int k = 0;k<2;k++)

{

t = (2.0*k*PI + q)/2;

cpxR[k].m_dblX = r*cos(t);

cpxR[k].m_dblY = r*sin(t);

}

}

//  实幂指数

CComplex CComplex::Pow(double dblW) const

{

// 局部变量

double r, t;

// 特殊值处理

if ((m_dblX == 0) && (m_dblY == 0))

return CComplex(0, 0);

// 幂运算公式中的三角函数运算

if (m_dblX == 0)

{

if (m_dblY > 0)

t = 1.5707963268;

else

t = -1.5707963268;

}

else

{

if (m_dblX > 0)

t = atan2(m_dblY, m_dblX);

else

{

if (m_dblY >= 0)

t = atan2(m_dblY, m_dblX) + PI;

else

t = atan2(m_dblY, m_dblX) - PI;

}

}

// 模的幂

r = exp(dblW * log(sqrt(m_dblX * m_dblX + m_dblY * m_dblY)));

// 复数的实幂指数

return CComplex(r * cos(dblW * t), r * sin(dblW * t));

}

//  复幂指数

CComplex CComplex::Pow(CComplex cpxW,int n = 0) const

{

// 局部变量

double r, s, u, v;

// 特殊值处理

if (m_dblX == 0)

{

if (m_dblY == 0)

return CComplex(0, 0);

s = 1.5707963268 * (fabs(m_dblY) / m_dblY + 4 * n);

}

else

{

s = 2 * PI * n + atan2(m_dblY, m_dblX);

if (m_dblX < 0)

{

if (m_dblY > 0)

s = s + PI;

else

s = s - PI;

}

}

// 求幂运算公式

r = 0.5 * log(m_dblX * m_dblX + m_dblY * m_dblY);

v = cpxW.m_dblX * r + cpxW.m_dblY * s;

u = exp(cpxW.m_dblX * r - cpxW.m_dblY * s);

return CComplex(u * cos(v), u * sin(v));

}

//  求对数

CComplex CComplex::Log() const

{

double p = log(sqrt(pow(m_dblX,2.0) +pow(m_dblY,2.0)));

return CComplex(p,atan2(m_dblY,m_dblX));

}

//  求解正弦

CComplex CComplex::Sin() const

{

int i;

double x, y, y1, br, b1, b2, c[6];

// 切比雪夫公式的常数系数

c[0] = 1.13031820798497;

c[1] = 0.04433684984866;

c[2] = 0.00054292631191;

c[3] = 0.00000319843646;

c[4] = 0.00000001103607;

c[5] = 0.00000000002498;

y1 = exp(m_dblY);

x = 0.5 * (y1 + 1 / y1);

if (fabs(m_dblY) >= 1)

y = 0.5 * (y1 - 1 / y1);

else

{

b1 = 0;

b2 = 0;

y1 = 2 * (2 * m_dblY * m_dblY - 1);

for (i = 5; i >=0; --i)

{

br = y1 * b1 - b2 - c[i];

if (i != 0)

{

b2 = b1;

b1 = br;

}

}

y = m_dblY * (br - b1);

}

// 组合计算结果

x = x * sin(m_dblX);

y = y * cos(m_dblX);

return CComplex(x, y);

}

//  计算余弦

CComplex CComplex::Cos() const

{

int i;

double x, y, y1, br, b1, b2, c[6];

// 切比雪夫公式的常数系数

c[0] = 1.13031820798497;

c[1] = 0.04433684984866;

c[2] = 0.00054292631191;

c[3] = 0.00000319843646;

c[4] = 0.00000001103607;

c[5] = 0.00000000002498;

y1 = exp(m_dblY);

x = 0.5 * (y1 + 1 / y1);

if (fabs(m_dblY) >= 1)

y = 0.5 * (y1 - 1 / y1);

else

{

b1 = 0;

b2 = 0;

y1 = 2 * (2 * m_dblY * m_dblY - 1);

for (i=5 ; i>=0; --i)

{

br = y1 * b1 - b2 - c[i];

if (i != 0)

{

b2 = b1;

b1 = br;

}

}

y = m_dblY * (br - b1);

}

// 组合计算结果

x = x * cos(m_dblX);

y = -y * sin(m_dblX);

return CComplex(x, y);

}

//  计算正切算法

CComplex CComplex::Tan() const

{

//  类内调用

return Sin()/Cos();

}

你可能感兴趣的:(vs2013中,c语言中复数怎么表示)