📄 calculation.cpp
字号:
{
strExp->Delete(pos,m_strConName[i].GetLength());
strExp->Insert(pos,m_strConValue[i]);
if(pos>=1)
{
char ch=strExp->GetAt(pos-1);
if(ch>=48 && ch<=57 || ch==41)
{*strExp="缺少二元运算符";return;}
}
pos=strExp->Find(m_strConName[i]);
}
}
}
CString CCalculation::ModiResult(CString strRes)
{
if(strRes.Find("#IN")!=-1) return "结果有溢出或值域越界";
/*****************去掉保护括号**********************/
if(strRes.GetAt(0)=='(') strRes.Delete(0);
if(strRes.GetAt(strRes.GetLength()-1)==')') strRes.Delete(strRes.GetLength()-1);
/***************************************************/
int pos=strRes.Find(".");CString str="";
if(pos!=-1)
{
if(pos==0) strRes="0"+strRes;
else if(strRes.GetAt(0)=='-' && strRes.GetAt(1)=='.') strRes.Insert(1,"0");
}
if(pos>16)
{
strRes.Delete(pos);
strRes.Insert(1,".");
str.Format("%d",pos-1);
str=" E"+str;
}
pos=strRes.Find(".");
if(pos==0 || pos==1 && strRes.GetAt(0)=='0')
{
for(int i=pos+1;i<strRes.GetLength();i++)
{
if(strRes.GetAt(i)!='0') break;
}
if(i>4)
{
str.Format(" E-%d",i-2);
strRes.Delete(pos,i-1);
strRes.Insert(1,".");
}
}
strRes=strRes.Left(pos+16)+str;//截取小数点后16位
return strRes;
}
bool CCalculation::SynRes(CString *strExp)
{
int pos=strExp->Find("ERROR");
if(pos!=-1)
{
pos=strExp->ReverseFind('_');
strExp->Delete(pos,strExp->GetLength()-pos);
pos=strExp->ReverseFind('_');
strExp->Delete(0,pos+1);
return 0;
}
return 1;
}
void CCalculation::MinusMinus(CString *strExp)
{
int pos=strExp->Find("--");
if(pos!=-1)
{
strExp->Delete(pos,2);
strExp->Insert(pos,"+");
if(strExp->GetAt(0)=='+') strExp->Delete(0);
}
}
int CCalculation::BraCheck(CString str)
{
int lb=0,rb=0,len=str.GetLength();
for(int i=0;i<len;i++)
{
if(str.GetAt(i)=='(') lb++;
else if(str.GetAt(i)==')') rb++;
}
return lb-rb;
}
void CCalculation::Oct2Dec(CString *strExp)
{
int len,i,index,pos=strExp->Find("xo");
CString strTmp,strDF;
char ch;
double dx;
while(pos!=-1)
{
dx=0;strTmp="";strDF="";
strExp->Delete(pos,2);
for(i=pos-1;i>=0;i--)
{
ch=strExp->GetAt(i);
if(ch==56 || ch==57 || ch>=97 && ch<=102)
{
*strExp="ERROR_八进制数越界_";
return;
}
if(ch>=48 && ch<=55 ||ch==46)
{
strTmp.Insert(0,strExp->Mid(i,1));
strExp->Delete(i);
}
else break;
}
if(i==pos-1) {*strExp="ERROR_缺少二元运算符_";return;}
index=i;
pos=strTmp.Find(".");
if(pos!=-1)
{
strDF=strTmp.Right(strTmp.GetLength()-pos-1);
strTmp.Delete(pos,strTmp.GetLength()-pos);
}
strTmp.MakeReverse();
len=strTmp.GetLength();
for(i=0;i<len;i++)
{
ch=strTmp.GetAt(i);
dx+=(ch-48)*pow(8,i);
}
len=strDF.GetLength();
for(i=0;i<len;i++)
{
ch=strDF.GetAt(i);
dx+=(ch-48)*pow(8,-i-1);
}
strTmp=NtoS(dx);
strExp->Insert(index+1,strTmp);
pos=strExp->Find("xo");
}
}
void CCalculation::Hex2Dec(CString *strExp)
{
int len,i,index,pos=strExp->Find("xh");
CString strTmp,strDF;
char ch;
double dx;
while(pos!=-1)
{
dx=0;strTmp="";strDF="";
strExp->Delete(pos,2);
for(i=pos-1;i>=0;i--)
{
ch=strExp->GetAt(i);
if(ch>=48 && ch<=57 || ch>=97 && ch<=102 ||ch==46)
{
strTmp.Insert(0,strExp->Mid(i,1));
strExp->Delete(i);
}
else break;
}
if(i==pos-1) {*strExp="ERROR_缺少二元运算符_";return;}
index=i;
pos=0;
for(i=0;i<strTmp.GetLength();i++)
{
if(strTmp.GetAt(i)=='.') pos++;
if(pos>1) {*strExp="ERROR_缺少二元运算符_";return;}
}
pos=strTmp.Find(".");
if(pos!=-1)
{
strDF=strTmp.Right(strTmp.GetLength()-pos-1);
strTmp.Delete(pos,strTmp.GetLength()-pos);
}
strTmp.MakeReverse();
len=strTmp.GetLength();
for(i=0;i<len;i++)
{
ch=strTmp.GetAt(i);
if(ch>=48 && ch<=57)//该数在0~9之间
{
dx+=(ch-48)*pow(16,i);
}
else if(ch>=97 && ch<=102)//该数在a~f之间
{
dx+=(ch-87)*pow(16,i);
}
}
len=strDF.GetLength();
for(i=0;i<len;i++)
{
ch=strDF.GetAt(i);
if(ch>=48 && ch<=57)//该数在0~9之间
{
dx+=(ch-48)*pow(16,-i-1);
}
else if(ch>=97 && ch<=102)//该数在a~f之间
{
dx+=(ch-87)*pow(16,-i-1);
}
}
strTmp=NtoS(dx);
strExp->Insert(index+1,strTmp);
pos=strExp->Find("xh");
}
}
void CCalculation::Bin2Dec(CString *strExp)
{
int len,i,index,pos=strExp->Find("xb");
CString strTmp,strDF;
char ch;
double dx;
while(pos!=-1)
{
dx=0;strTmp="";strDF="";
strExp->Delete(pos,2);
for(i=pos-1;i>=0;i--)
{
ch=strExp->GetAt(i);
if(ch>=50 && ch<=57 || ch>=97 && ch<=102)
{
*strExp="ERROR_二进制数越界_";
return;
}
if(ch=='0' || ch=='1' ||ch==46)
{
strTmp.Insert(0,strExp->Mid(i,1));
strExp->Delete(i);
}
else break;
}
if(i==pos-1) {*strExp="ERROR_缺少二元运算符_";return;}
index=i;
pos=strTmp.Find(".");
if(pos!=-1)
{
strDF=strTmp.Right(strTmp.GetLength()-pos-1);
strTmp.Delete(pos,strTmp.GetLength()-pos);
}
strTmp.MakeReverse();
len=strTmp.GetLength();
for(i=0;i<len;i++)
{
ch=strTmp.GetAt(i);
dx+=(ch-48)*pow(2,i);
}
len=strDF.GetLength();
for(i=0;i<len;i++)
{
ch=strDF.GetAt(i);
dx+=(ch-48)*pow(2,-i-1);
}
strTmp=NtoS(dx);
strExp->Insert(index+1,strTmp);
pos=strExp->Find("xb");
}
}
void CCalculation::Dec2Hex(CString *strExp/*strExp须为数字*/)
{
bool bMinus=0;
if(strExp->GetAt(0)=='-')
{
bMinus=1;
strExp->Delete(0);
}
int pos=strExp->Find('.');
CString str,strDec;
int nDecInt;
double dDec;
if(pos!=-1)
{
strDec=strExp->Left(pos);
nDecInt=atoi(strDec.GetBuffer(0));
strDec=strExp->Right(strExp->GetLength()-pos);
}
else
{
nDecInt=atoi(strExp->GetBuffer(0));
}
strExp->Empty();
while(nDecInt!=0)
{
int nTmp=nDecInt%16;
if(nTmp==10) str="a";
else if(nTmp==11) str="b";
else if(nTmp==12) str="c";
else if(nTmp==13) str="d";
else if(nTmp==14) str="e";
else if(nTmp==15) str="f";
else str.Format("%d",nTmp);
nDecInt/=16;
strExp->Insert(0,str);
}
*strExp+=".";
if(pos!=-1)
{
dDec=StoN(strDec);
int nCount=0;
while(dDec!=0)
{
dDec*=16;
int nTmp=dDec;
if(nTmp==10) str="a";
else if(nTmp==11) str="b";
else if(nTmp==12) str="c";
else if(nTmp==13) str="d";
else if(nTmp==14) str="e";
else if(nTmp==15) str="f";
else str.Format("%d",nTmp);
*strExp+=str.Left(pos);
dDec-=nTmp;
if(++nCount==17) break;
}
}
if(bMinus) strExp->Insert(0,"-");
if(strExp->Find("-1")!=-1 && bMinus!=1) *strExp="太大无法表示";
}
void CCalculation::Dec2Oct(CString *strExp)
{
bool bMinus=0;
if(strExp->GetAt(0)=='-')
{
bMinus=1;
strExp->Delete(0);
}
int pos=strExp->Find('.');
CString str,strDec;
int nDecInt;
double dDec;
if(pos!=-1)
{
strDec=strExp->Left(pos);
nDecInt=atoi(strDec.GetBuffer(0));
strDec=strExp->Right(strExp->GetLength()-pos);
}
else
{
nDecInt=atoi(strExp->GetBuffer(0));
}
strExp->Empty();
while(nDecInt!=0)
{
int nTmp=nDecInt%8;
str.Format("%d",nTmp);
nDecInt/=8;
strExp->Insert(0,str);
}
*strExp+=".";
if(pos!=-1)
{
dDec=StoN(strDec);
int nCount=0;
while(dDec!=0)
{
dDec*=8;
int nTmp=dDec;
str.Format("%d",nTmp);
*strExp+=str.Left(pos);
dDec-=nTmp;
if(++nCount==17) break;
}
}
if(bMinus) strExp->Insert(0,"-");
}
void CCalculation::Dec2Bin(CString *strExp)
{
bool bMinus=0;
if(strExp->GetAt(0)=='-')
{
bMinus=1;
strExp->Delete(0);
}
int pos=strExp->Find('.');
CString str,strDec;
_int64 nDecInt;
double dDec;
if(pos!=-1)
{
strDec=strExp->Left(pos);
if(strDec.Compare("4294967295")>0 && strDec.GetLength()>10 || strDec.GetLength()>10)
{
*strExp="太大无法转换";
return;
}
nDecInt=atoi(strDec.GetBuffer(0));
strDec=strExp->Right(strExp->GetLength()-pos);
}
else
{
if(strExp->Compare("4294967295")>0 && strExp->GetLength()>10 || strExp->GetLength()>10)
{
*strExp="太大无法转换";
return;
}
nDecInt=atoi(strExp->GetBuffer(0));
}
strExp->Empty();
while(nDecInt!=0)
{
_int64 nTmp=nDecInt%2;
str.Format("%d",nTmp);
nDecInt/=2;
strExp->Insert(0,str);
}
*strExp+=".";
if(pos!=-1)
{
dDec=StoN(strDec);
int nCount=0;
while(dDec!=0)
{
dDec*=2;
int nTmp=dDec;
str.Format("%d",nTmp);
*strExp+=str.Left(pos);
dDec-=nTmp;
if(++nCount==17) break;
}
}
if(bMinus) strExp->Insert(0,"-");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -