📄 calculation.cpp
字号:
else return ModiResult(store);
}
void CCalculation::Macro(CString *store)
{
int pos;
for(int i=0;i<CONSTNUM;i++)
{
pos=store->Find(m_strConName[i]);
while(pos!=-1)
{
store->Delete(pos,m_strConName[i].GetLength());
store->Insert(pos,m_strConValue[i]);
if(pos>=1)
{
char ch=store->GetAt(pos-1);
if(ch>=48 && ch<=57 || ch==41)
{*store="缺少二元运算符";return;}
}
pos=store->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 *store)
{
int pos=store->Find("ERROR"); //找到错误的地方
if(pos!=-1) //找到错误做以下处理
{
pos=store->ReverseFind('_');
store->Delete(pos,store->GetLength()-pos);
pos=store->ReverseFind('_');
store->Delete(0,pos+1);
return 0;
}
else
{
pos=store->Find("--");
while(pos!=-1)
{
store->Delete(pos,2);
pos=store->Find("--");
}
return 1;
}
}
void CCalculation::MinusMinus(CString *store)
{
int pos=store->Find("--");
if(pos!=-1)
{
store->Delete(pos,2);
store->Insert(pos,"+");
if(store->GetAt(0)=='+') store->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 *store)
{
int len,i,index,pos=store->Find("xo");
CString strTmp,strDF;
char ch;
double dx;
while(pos!=-1)
{
dx=0;strTmp="";strDF="";
store->Delete(pos,2);
for(i=pos-1;i>=0;i--)
{
ch=store->GetAt(i);
if(ch==56 || ch==57 || ch>=97 && ch<=102)
{
*store="ERROR_八进制数越界_";
return;
}
if(ch>=48 && ch<=55 ||ch==46)
{
strTmp.Insert(0,store->Mid(i,1));
store->Delete(i);
}
else break;
}
if(i==pos-1) {*store="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);
store->Insert(index+1,strTmp);
pos=store->Find("xo");
}
}
void CCalculation::Hex2Dec(CString *store)
{
int len,i,index,pos=store->Find("xh");
CString strTmp,strDF;
char ch;
double dx;
while(pos!=-1)
{
dx=0;strTmp="";strDF="";
store->Delete(pos,2);
for(i=pos-1;i>=0;i--)
{
ch=store->GetAt(i);
if(ch>=48 && ch<=57 || ch>=97 && ch<=102 ||ch==46)
{
strTmp.Insert(0,store->Mid(i,1));
store->Delete(i);
}
else break;
}
if(i==pos-1) {*store="ERROR_缺少二元运算符_";return;}
index=i;
pos=0;
for(i=0;i<strTmp.GetLength();i++)
{
if(strTmp.GetAt(i)=='.') pos++;
if(pos>1) {*store="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);
store->Insert(index+1,strTmp);
pos=store->Find("xh");
}
}
void CCalculation::Bin2Dec(CString *store)
{
int len,i,index,pos=store->Find("xb");
CString strTmp,strDF;
char ch;
double dx;
while(pos!=-1)
{
dx=0;strTmp="";strDF="";
store->Delete(pos,2);
for(i=pos-1;i>=0;i--)
{
ch=store->GetAt(i);
if(ch>=50 && ch<=57 || ch>=97 && ch<=102)
{
*store="ERROR_二进制数越界_";
return;
}
if(ch=='0' || ch=='1' ||ch==46)
{
strTmp.Insert(0,store->Mid(i,1));
store->Delete(i);
}
else break;
}
if(i==pos-1) {*store="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);
store->Insert(index+1,strTmp);
pos=store->Find("xb");
}
}
void CCalculation::Dec2Hex(CString *store/*store须为数字*/)
{
bool bMinus=0;
if(store->GetAt(0)=='-')
{
bMinus=1;
store->Delete(0);
}
int pos=store->Find('.');
CString str,strDec;
int nDecInt;
double dDec;
if(pos!=-1)
{
strDec=store->Left(pos);
nDecInt=atoi(strDec.GetBuffer(0));
strDec=store->Right(store->GetLength()-pos);
}
else
{
nDecInt=atoi(store->GetBuffer(0));
}
store->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;
store->Insert(0,str);
}
*store+=".";
if(pos!=-1)
{
dDec=StoN(strDec);
int nCount=0;
while(dDec!=0)
{
dDec*=16;
int nTmp=(int)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);
*store+=str.Left(pos);
dDec-=nTmp;
if(++nCount==17) break;
}
}
if(bMinus) store->Insert(0,"-");
if(store->Find("-1")!=-1 && bMinus!=1) *store="太大无法表示";
}
void CCalculation::Dec2Oct(CString *store)
{
bool bMinus=0;
if(store->GetAt(0)=='-')
{
bMinus=1;
store->Delete(0);
}
int pos=store->Find('.');
CString str,strDec;
int nDecInt;
double dDec;
if(pos!=-1)
{
strDec=store->Left(pos);
nDecInt=atoi(strDec.GetBuffer(0));
strDec=store->Right(store->GetLength()-pos);
}
else
{
nDecInt=atoi(store->GetBuffer(0));
}
store->Empty();
while(nDecInt!=0)
{
int nTmp=nDecInt%8;
str.Format("%d",nTmp);
nDecInt/=8;
store->Insert(0,str);
}
*store+=".";
if(pos!=-1)
{
dDec=StoN(strDec);
int nCount=0;
while(dDec!=0)
{
dDec*=8;
int nTmp=(int)dDec;
str.Format("%d",nTmp);
*store+=str.Left(pos);
dDec-=nTmp;
if(++nCount==17) break;
}
}
if(bMinus) store->Insert(0,"-");
}
void CCalculation::Dec2Bin(CString *store)
{
bool bMinus=0;
if(store->GetAt(0)=='-')
{
bMinus=1;
store->Delete(0);
}
int pos=store->Find('.');
CString str,strDec;
_int64 nDecInt;
double dDec;
if(pos!=-1)
{
strDec=store->Left(pos);
if(strDec.Compare("4294967295")>0 && strDec.GetLength()>10 || strDec.GetLength()>10)
{
*store="太大无法转换";
return;
}
nDecInt=atoi(strDec.GetBuffer(0));
strDec=store->Right(store->GetLength()-pos);
}
else
{
if(store->Compare("4294967295")>0 && store->GetLength()>10 || store->GetLength()>10)
{
*store="太大无法转换";
return;
}
nDecInt=atoi(store->GetBuffer(0));
}
store->Empty();
while(nDecInt!=0)
{
_int64 nTmp=nDecInt%2;
str.Format("%d",nTmp);
nDecInt/=2;
store->Insert(0,str);
}
*store+=".";
if(pos!=-1)
{
dDec=StoN(strDec);
int nCount=0;
while(dDec!=0)
{
dDec*=2;
int nTmp=(int)dDec;
str.Format("%d",nTmp);
*store+=str.Left(pos);
dDec-=nTmp;
if(++nCount==17) break;
}
}
if(bMinus) store->Insert(0,"-");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -