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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值