#include<math.h>
#include"string.h"
#include "stdafx.h"
class CComplex
{
public:
CComplex();
CComplex(double dblx,double dbly);
CComplex(const CComplex&other);
virtual ~CComplex(){};
void Setreal(double dblx);
void Setimag(double dbly);
double Getreal();
double Getimag();
CString ToString() const;
void FromString(CString s,const CString& sDelim=" ");
bool operator==(const CComplex &cpxx) const;
bool operator!=(const CComplex &cpxx) const;
CComplex& operator=(const CComplex &cpxx);
CComplex operator+(const CComplex &cpxx) const;
CComplex operator-(const CComplex &cpxx) const;
CComplex operator*(const CComplex &cpxx) const;
CComplex operator/(const CComplex &cpxx) const;
double Abs() const;
void Root(int n,CComplex cpxr[]) const;
CComplex Pow(double dblw) const;
CComplex Pow(CComplex cpxw,int n=0) const;
CComplex Log() const;
CComplex Sin() const;
CComplex Cos() const;
CComplex Tan() const;
protected:
double m_dblx;
double m_dbly;
};
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 dblx)
{
m_dblx=dblx;
}
void CComplex::Setimag(double dbly)
{
m_dbly=dbly;
}
double CComplex::Getreal()
{
return m_dblx;
}
double CComplex::Getimag()
{
return m_dbly;
}
CString CComplex::ToString() const
{
CString s;
if(m_dblx!=0.0)
{
if(m_dbly>0)
s.Format("%f+%fj",m_dblx,m_dbly);
else if(m_dbly<0)
s.Format("%f-%fj",m_dblx,fabs(m_dbly));
else
s.Format("%f",m_dblx);
}
else
{
if(m_dbly>0)
s.Format("%f",m_dbly);
else if(m_dbly<0)
s.Format("-%fj",fabs(m_dbly));
else
s.Format("%f",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=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);
}
}
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 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);
}
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+(y/x)*(y/x)));
}
void CComplexCaculatorDlg::OnPlus()
{
// TODO: Add your control notification handler code here
UpdateData();
CComplex cpxA(double m_dblA,double m_dblB);
CComplex cpxB(double m_dblC,double m_dblD);
CComplex cpxC= cpxA*cpxB;
m_strResult=cpxC.ToString();
UpdateData(FALSE);
}
--------------------Configuration: ComplexCaculator - Win32 Debug--------------------
Compiling...
ComplexCaculatorDlg.cpp
D:\c++\CComplex\ComplexCaculatorDlg.cpp(199) : error C2296: '*' : illegal, left operand has type 'class CComplex (__cdecl *)(double,double)'
D:\c++\CComplex\ComplexCaculatorDlg.cpp(199) : error C2297: '*' : illegal, right operand has type 'class CComplex (__cdecl *)(double,double)'
D:\c++\CComplex\ComplexCaculatorDlg.cpp(242) : error C2228: left of '.Abs' must have class/struct/union type
Error executing cl.exe.
Creating browse info file...
ComplexCaculator.exe - 3 error(s), 0 warning(s)