CComplex类

 1.complex.h

#ifndef __COMPLEX_H
#define __COMPLEX_H

class CComplex{
public:
 CComplex();
 CComplex(double x,double y);
 
 CComplex(const CComplex& other);
 
 bool operator==(const CComplex & cpxx)const;
 bool operator!=(const CComplex& cpxx)const;
 CComplex& operator=(const CComplex & cpxx);
 
 CComplex  operator+(const CComplex & cpxx);  //返回对象
 CComplex  operator-(const CComplex & cpxx);
 CComplex  operator*(const CComplex & cpxx);
 CComplex  operator/(const CComplex & cpxx);


// void fromString(CString s,const CString &sDelim)const;  //在头文件里识别不了CString
// CString toString()const;
public:
 double x;
 double y;
};
#endif

 

2.Complex.cpp

#include "CComplex.h"

CComplex::CComplex(){
 this->x=0.0;
 this->y=0.0;
}
CComplex::CComplex(double x,double y){
 this->x=x;
 this->y=y;
}

CComplex::CComplex(const CComplex& other){

 this->x=other.x;
 this->y=other.y;
}

bool CComplex::operator==(const CComplex & cpxx)const{
 return (x==cpxx.x)&&(this->y==cpxx.y);
}
bool CComplex::operator!=(const CComplex& cpxx)const{
 return !((x==cpxx.x)&&(this->y==cpxx.y));

}
CComplex& CComplex::operator=(const CComplex & cpxx){
this->x=cpxx.x;
this->y=cpxx.y;
return *this;
}

CComplex  CComplex::operator+(const CComplex & cpxx){
double  xx=this->x+cpxx.x;
double  yy=this->y+cpxx.y;
 return CComplex(xx,yy);
}
CComplex  CComplex::operator-(const CComplex & cpxx){
double  xx=this->x-cpxx.x;
double  yy=this->y-cpxx.y;
return CComplex(xx,yy);
}
CComplex  CComplex::operator*(const CComplex & cpxx){
double x1=x*cpxx.x-y*cpxx.y;
double y1=x*cpxx.y+y*cpxx.x;
return CComplex(x1,y1);
}

CComplex  CComplex::operator/(const CComplex & cpxx){
double tmp=cpxx.x*cpxx.x+cpxx.y*cpxx.y;
double x1=x*cpxx.x+y*cpxx.y;
double y1=-x*cpxx.y+y*cpxx.x;
x1=x1/tmp;
y1=y1/tmp;
return CComplex(x1,y1);
}


3.建立MFC的对话框

void CNumricAnalysis_zhoucfDlg::OnAdd()
{
UpdateData();
// CString num1;
// GetDlgItemText(IDC_EDIT1,num1);

 

 CComplex c=this->FromString(m_num1," ");
 char a[100];
 memset(a,0,1);
 sprintf(a,"%f+%fj",c.x,c.y);
 ::MessageBox(NULL,a,a,2);
 CComplex d=this->FromString(m_num2," ");
 
 
 CComplex res=c+d;
CString str=this->ToString(res);
 
//char * tmp=itoa(0,c.x,3);
 
 ::MessageBox(NULL,str,str,1);
UpdateData(FALSE);

}

  CString CNumricAnalysis_zhoucfDlg::ToString(CComplex cpxx)
  {
   CString s;
   
   if(cpxx.x!=0.0){
    if(cpxx.y>0.0)
     s.Format("%f+%fj",cpxx.x,cpxx.y);
    else if(cpxx.y<0.0)
     s.Format("%f-%fj",cpxx.x,cpxx.y);
    else
     s.Format("%f",cpxx.x);
   }
   else{
    if(cpxx.y>0.0)
     s.Format("%fj",cpxx.y);
    else if(cpxx.y<0.0)
     s.Format("-%fj",cpxx.y);
    else
     s.Format("%f",cpxx.x);
   }
   return s;
  }
  CComplex CNumricAnalysis_zhoucfDlg::FromString(CString s,const CString &sDelim)const
  {
  
 //::MessageBox(NULL,s.GetBuffer(s.GetLength()),s,1);
   CComplex cpxx;
   int nPos=s.Find(sDelim);
   if(nPos==-1){
    s.TrimLeft();
    s.TrimRight();
    cpxx.x=atof(s);
    cpxx.y=0;
   }else{
    int len=s.GetLength();
    CString sLeft=s.Left(nPos);
    CString sRight=s.Right(len-nPos-1);
    sLeft.TrimLeft();
    sRight.TrimRight();
    cpxx.x=atof(sLeft);
    cpxx.y=atof(sRight);
   }
 
  return cpxx;
  }

 

你可能感兴趣的:(数值分析&FEM)