📄 calculation.cpp
字号:
int pos;
for(int i=0;i<CONSTNUM;i++)
{
pos=strExp->find(m_strConName[i]);
while(pos!=-1)
{
strExp->erase(pos,m_strConName[i].length());
strExp->insert(pos,m_strConValue[i]);
if(pos>=1)
{
char ch=strExp->at(pos-1);
if(ch>=48 && ch<=57 || ch==41)
{*strExp="缺少二元运算符";return;}
}
pos=strExp->find(m_strConName[i]);
}
}
}
string CCalculation::ModiResult(string strRes)
{
if(strRes.find("#IN")!=-1) return "结果有溢出或值域越界";
/*****************去掉保护括号**********************/
if(strRes.at(0)=='(') strRes.erase(0,1);
if(strRes.at(strRes.length()-1)==')') strRes.erase(strRes.length()-1,1);
/***************************************************/
int pos=strRes.find(".");string str="";
if(pos!=-1)
{
if(pos==0) strRes="0"+strRes;
else if(strRes.at(0)=='-' && strRes.at(1)=='.') strRes.insert(1,"0");
}
if(pos>16)
{
strRes.erase(pos,1);
strRes.insert(1,".");
//str.Format("%d",pos-1);
char tmp[10];
sprintf(tmp, "%d", pos-1);
str = tmp;
str=" E"+str;
}
pos=strRes.find(".");
if(pos==0 || pos==1 && strRes.at(0)=='0')
{
for(int i=pos+1;i<strRes.length();i++)
{
if(strRes.at(i)!='0') break;
}
if(i>4)
{
char tmp[10];
sprintf(tmp, " E-%d", i-2);
str = tmp;
//str.Format(" E-%d",i-2);
strRes.erase(pos,i-1);
strRes.insert(1,".");
}
}
strRes=Left(strRes, pos+16)+str;//截取小数点后16位
return strRes;
}
bool CCalculation::SynRes(string *strExp)
{
int pos=strExp->find("ERROR");
if(pos!=-1)
{
pos=strExp->rfind('_', strExp->length());
strExp->erase(pos,strExp->length()-pos);
pos=strExp->rfind('_', strExp->length());
strExp->erase(0,pos+1);
return 0;
}
return 1;
}
void CCalculation::MinusMinus(string *strExp)
{
int pos=strExp->find("--");
if(pos!=-1)
{
strExp->erase(pos,2);
strExp->insert(pos,"+");
if(strExp->at(0)=='+') strExp->erase(0,1);
}
}
int CCalculation::BraCheck(string str)
{
int lb=0,rb=0,len=str.length();
for(int i=0;i<len;i++)
{
if(str.at(i)=='(') lb++;
else if(str.at(i)==')') rb++;
}
return lb-rb;
}
void CCalculation::Oct2Dec(string *strExp)
{
int len,i,index,pos=strExp->find("xo");
string strTmp,strDF;
char ch;
double dx;
while(pos!=-1)
{
dx=0;strTmp="";strDF="";
strExp->erase(pos,2);
for(i=pos-1;i>=0;i--)
{
ch=strExp->at(i);
if(ch==56 || ch==57 || ch>=97 && ch<=102)
{
*strExp="ERROR_八进制数越界_";
return;
}
if(ch>=48 && ch<=55 ||ch==46)
{
strTmp.insert(0,strExp->substr(i,1));
strExp->erase(i,1);
}
else break;
}
if(i==pos-1) {*strExp="ERROR_缺少二元运算符_";return;}
index=i;
pos=strTmp.find(".");
if(pos!=-1)
{
strDF=Right(strTmp, strTmp.length()-pos-1);
strTmp.erase(pos,strTmp.length()-pos);
}
MakeReverse(strTmp);
len=strTmp.length();
for(i=0;i<len;i++)
{
ch=strTmp.at(i);
dx+=(ch-48)*pow(8,i);
}
len=strDF.length();
for(i=0;i<len;i++)
{
ch=strDF.at(i);
dx+=(ch-48)*pow(8,-i-1);
}
strTmp=NtoS(dx);
strExp->insert(index+1,strTmp);
pos=strExp->find("xo");
}
}
void CCalculation::Hex2Dec(string *strExp)
{
int len,i,index,pos=strExp->find("xh");
string strTmp,strDF;
char ch;
double dx;
while(pos!=-1)
{
dx=0;strTmp="";strDF="";
strExp->erase(pos,2);
for(i=pos-1;i>=0;i--)
{
ch=strExp->at(i);
if(ch>=48 && ch<=57 || ch>=97 && ch<=102 ||ch==46)
{
strTmp.insert(0,strExp->substr(i,1));
strExp->erase(i,1);
}
else break;
}
if(i==pos-1) {*strExp="ERROR_缺少二元运算符_";return;}
index=i;
pos=0;
for(i=0;i<strTmp.length();i++)
{
if(strTmp.at(i)=='.') pos++;
if(pos>1) {*strExp="ERROR_缺少二元运算符_";return;}
}
pos=strTmp.find(".");
if(pos!=-1)
{
strDF=Right(strTmp, strTmp.length()-pos-1);
strTmp.erase(pos,strTmp.length()-pos);
}
MakeReverse(strTmp);
len=strTmp.length();
for(i=0;i<len;i++)
{
ch=strTmp.at(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.length();
for(i=0;i<len;i++)
{
ch=strDF.at(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(string *strExp)
{
int len,i,index,pos=strExp->find("xb");
string strTmp,strDF;
char ch;
double dx;
while(pos!=-1)
{
dx=0;strTmp="";strDF="";
strExp->erase(pos,2);
for(i=pos-1;i>=0;i--)
{
ch=strExp->at(i);
if(ch>=50 && ch<=57 || ch>=97 && ch<=102)
{
*strExp="ERROR_二进制数越界_";
return;
}
if(ch=='0' || ch=='1' ||ch==46)
{
strTmp.insert(0,strExp->substr(i,1));
strExp->erase(i,1);
}
else break;
}
if(i==pos-1) {*strExp="ERROR_缺少二元运算符_";return;}
index=i;
pos=strTmp.find(".");
if(pos!=-1)
{
strDF=Right(strTmp, strTmp.length()-pos-1);
strTmp.erase(pos,strTmp.length()-pos);
}
MakeReverse(strTmp);
len=strTmp.length();
for(i=0;i<len;i++)
{
ch=strTmp.at(i);
dx+=(ch-48)*pow(2,i);
}
len=strDF.length();
for(i=0;i<len;i++)
{
ch=strDF.at(i);
dx+=(ch-48)*pow(2,-i-1);
}
strTmp=NtoS(dx);
strExp->insert(index+1,strTmp);
pos=strExp->find("xb");
}
}
void CCalculation::Dec2Hex(string *strExp/*strExp须为数字*/)
{
bool bMinus=0;
if(strExp->at(0)=='-')
{
bMinus=1;
strExp->erase(0,1);
}
int pos=strExp->find('.');
string str,strDec;
int nDecInt;
double dDec;
if(pos!=-1)
{
strDec=Left(strExp, pos);
nDecInt=atoi(strDec.data());
strDec=Right(strExp, strExp->length()-pos);
}
else
{
nDecInt=atoi(strExp->data());
}
strExp->erase(0,strExp->length());
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);
char tmp[10];
sprintf(tmp, "%d", nTmp);
str = tmp;
}
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);
char tmp[10];
sprintf(tmp, "%d", nTmp);
str = tmp;
}
*strExp+=Left(str, pos);
dDec-=nTmp;
if(++nCount==17) break;
}
}
if(bMinus) strExp->insert(0,"-");
if(strExp->find("-1")!=-1 && bMinus!=1) *strExp="太大无法表示";
}
void CCalculation::Dec2Oct(string *strExp)
{
bool bMinus=0;
if(strExp->at(0)=='-')
{
bMinus=1;
strExp->erase(0,1);
}
int pos=strExp->find('.');
string str,strDec;
int nDecInt;
double dDec;
if(pos!=-1)
{
strDec=Left(strExp, pos);
nDecInt=atoi(strDec.data());
strDec=Right(strExp, strExp->length()-pos);
}
else
{
nDecInt=atoi(strExp->data());
}
strExp->erase(0,strExp->length());
while(nDecInt!=0)
{
int nTmp=nDecInt%8;
//str.Format("%d",nTmp);
char tmp[10];
sprintf(tmp, "%d", nTmp);
str = tmp;
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);
char tmp[10];
sprintf(tmp, "%d",nTmp);
str = tmp;
*strExp+=Left(str, pos);
dDec-=nTmp;
if(++nCount==17) break;
}
}
if(bMinus) strExp->insert(0,"-");
}
void CCalculation::Dec2Bin(string *strExp)
{
bool bMinus=0;
if(strExp->at(0)=='-')
{
bMinus=1;
strExp->erase(0,1);
}
int pos=strExp->find('.');
string str,strDec;
_int64 nDecInt;
double dDec;
if(pos!=-1)
{
strDec=Left(strExp, pos);
if(strDec.compare("4294967295")>0 && strDec.length()>10 || strDec.length()>10)
{
*strExp="太大无法转换";
return;
}
nDecInt=atoi(strDec.data());
strDec=Right(strExp, strExp->length()-pos);
}
else
{
if(strExp->compare("4294967295")>0 && strExp->length()>10 || strExp->length()>10)
{
*strExp="太大无法转换";
return;
}
nDecInt=atoi(strExp->data());
}
strExp->erase(0,strExp->length());
while(nDecInt!=0)
{
_int64 nTmp=nDecInt%2;
//str.Format("%d",nTmp);
char tmp[10];
sprintf(tmp, "%d",nTmp);
str = tmp;
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);
char tmp[10];
sprintf(tmp, "%d",nTmp);
str = tmp;
*strExp+=Left(str, pos);
dDec-=nTmp;
if(++nCount==17) break;
}
}
if(bMinus) strExp->insert(0,"-");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -