📄 calculation.cpp
字号:
// Calculation.cpp: implementation of the CCalculation class.
// Made by JudeChu on Nov. 26, 2006
//////////////////////////////////////////////////////////////////////
#include "Calculation.h"
#include "math.h"
#include <string.h>
//////////////////////////////////////////////////////////////////////////
//Utility Functions
//////////////////////////////////////////////////////////////////////////
string Right(string str,int nCount){
int len = str.length();
string res = str.substr(len - nCount, nCount);
return res;
}
string Right(string * str, int nCount){
int len = str->length();
string res = str->substr(len - nCount, nCount);
return res;
}
string Left(string str, int nCount){
int len = str.length();
string res = str.substr(0, nCount);
return res;
}
string Left(string * str, int nCount){
int len = str->length();
string res = str->substr(0, nCount);
return res;
}
void MakeReverse(string &str){
char * p = (char *)str.c_str();
char tmp;
int len = (int)strlen(p);
for (int i = 0; i < len / 2 - 1; i ++){
tmp = p[i];
p[i] = p[len - 1 - i];
p[len - 1 - i] = tmp;
}
str = p;
}
#define FUNCNUM 15 //函数个数
#define CONSTNUM 11 //常数个数
#define E 2.7182818284590452353
#define PI 3.1415926535897932384
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CCalculation::CCalculation()
{
//初始化函数和常数
m_bDegree=0;
m_nOutputFlag=0;
opt[0]="e^";
opt[1]="ch";
opt[2]="sqr";
opt[3]="sh";
opt[4]="log";
opt[5]="cos";
opt[6]="sin";
opt[7]="tan";
opt[8]="ln";
opt[9]="abs";
opt[10]="at";
opt[11]="as";
opt[12]="ac";
opt[13]="th";
opt[14]="exp";
opt2[0]='^';
opt2[1]='/';
opt2[2]='*';
opt2[3]='%';
opt2[4]='+';
opt2[5]='-';
m_strConValue[0]="3.1415926535897932384"; m_strConName[0]="PI";
m_strConValue[1]="9.80665"; m_strConName[1]="GG";
m_strConValue[2]="(8.988*10^9)"; m_strConName[2]="EF";
m_strConValue[3]="0.02241383"; m_strConName[3]="VM";
m_strConValue[4]="(1.380662*10^(-23))"; m_strConName[4]="BC";
m_strConValue[5]="8.3144"; m_strConName[5]="MG";
m_strConValue[6]="(6.6720*10^(-11))"; m_strConName[6]="GR";
m_strConValue[7]="(6.022045*10^23)"; m_strConName[7]="NA";
m_strConValue[8]="(2.99792458*10^8)"; m_strConName[8]="LS";
m_strConValue[9]="(8.854187818*10^(-12))"; m_strConName[9]="DC";
m_strConValue[10]="2.7182818284590452353"; m_strConName[10]="E";
}
CCalculation::~CCalculation()
{
}
bool CCalculation::IsDigital(string str)
{
while(str.at(0)=='-') str.erase(0, 1);
for(int i=0;i<FUNCNUM;i++) if(str.find(opt[i])!=-1) return 0;
for(i=0;i<6;i++) if(str.find(opt2[i])!=-1) return 0;
return 1;
}
string CCalculation::NtoS(double d)
{
int decimal, sign;
char *buffer;
buffer = _ecvt( d, 16, &decimal, &sign );
string str=buffer;
if(decimal>=0 && decimal<=16) str.insert(decimal,".");
else if(decimal>16)
{
for(int i=str.length();i<decimal;i++) str+="0";
str+=".0";
}
else
{
for(int i=0;i<-decimal;i++) str.insert(0,"0");
str.insert(0,".");
}
if(sign==1) str.insert(0,"-");
return str;
}
double CCalculation::StoN(string str)
{
char *stopstring;
double x;
x = strtod( str.data(), &stopstring );
m_strTmp=stopstring;
return x;
}
string CCalculation::TwoE(string strExp)
{
double x2,y,x1=StoN(strExp);
strExp=m_strTmp;
char op=strExp.at(0);
strExp.erase(0,1);
x2=StoN(strExp);
if(m_strTmp!="") return "ERROR_"+m_strTmp+"未知符号_";
if(op=='+') {y=x1+x2;}
else if(op=='*') {y=x1*x2;}
else if(op=='-') {y=x1-x2;}
else if(op=='^') {y=pow(x1,x2);}
else if(op=='/') {y=x1/x2;}
else if(op=='%') {y=fmod(x1,x2);}
else return "ERROR_"+m_strTmp+"未知运算符_";
return NtoS(y);
}
string CCalculation::SingleE(string op,double dx)
{
if(op=="ln")
{
return NtoS(log(dx));
}
else if(op=="log")
{
return NtoS(log10(dx));
}
else if(op=="sqr")
{
return NtoS(sqrt(dx));
}
else if(op=="e^")
{
return NtoS(exp(dx));
}
else if(op=="abs")
{
return NtoS(fabs(dx));
}
else if(op=="ac")
{
return NtoS(acos(dx));
}
else if(op=="as")
{
return NtoS(asin(dx));
}
else if(op=="at")
{
return NtoS(atan(dx));
}
if(m_bDegree) dx=dx*PI/180;
if(op=="tan")
{
return NtoS(tan(dx));
}
else if(op=="sin")
{
return NtoS(sin(dx));
}
else if(op=="cos")
{
return NtoS(cos(dx));
}
else if(op=="cot")
{
return NtoS(1/tan(dx));
}
else if(op=="sh")
{
return NtoS(sinh(dx));
}
else if(op=="ch")
{
return NtoS(cosh(dx));
}
else if(op=="th")
{
return NtoS(sinh(dx)/cosh(dx));
}
else if(op=="exp")
{
return NtoS(pow(10,dx));
}
return "ERROR"+op+"_未知函数_";
}
int CCalculation::LocateLBra(string strExp)
{
int len=strExp.length();
int pos=-1;
for(int i=0;i<len;i++)
{
if(pos>=0 && strExp.at(i)==')') break;
if(strExp.at(i)=='(')
{
pos=i;
}
}
return pos;
}
void CCalculation::DelBracket(string *strExp)
{
int pos=LocateLBra(*strExp);
int i,len=strExp->length();
if(pos==-1) {*strExp="ERROR_无效表达式_";return;}
for(i=pos;i<len;i++) if(strExp->at(i)==')') break;
string str;
str=strExp->substr(pos+1,i-pos-1);
MultiE(&str);
strExp->erase(pos,i-pos+1);
strExp->insert(pos,str);
}
void CCalculation::MultiE(string *strExp)
{
if(strExp->empty()) *strExp="ERROR_函数表达式为空_";
if(IsDigital(*strExp)) {return;}
while(1)/*处理所有的一元运算*/
{
for(int i=0;i<FUNCNUM;i++)
{
int pos=strExp->find(opt[i]);
if(pos!=-1)
{
string str=Right(strExp, strExp->length()-pos-opt[i].length());
double dx=StoN(str);
if(m_strTmp==str) {*strExp="ERROR"+str+"_无法识别的函数_";return;}
strExp->erase(pos,strExp->length()-pos);
*strExp+=SingleE(opt[i],dx)+m_strTmp;
MinusMinus(strExp);
if(pos>=1)
{
char ch=strExp->at(pos-1);
if(ch>=48 && ch<=57)
{*strExp="ERROR_缺少二元运算符_";return;}
}
break;
}
}
if(i==FUNCNUM) break;
}
//按运算符优先级处理二元运算
int pos=-1;
while(1)
{
pos=strExp->find('^');
if(pos==-1) break;
else Calcu(strExp,pos);
}
while(1)
{
pos=strExp->find('/');
if(pos==-1) break;
else Calcu(strExp,pos);
}
while(1)
{
pos=strExp->find('*');
if(pos==-1) break;
else Calcu(strExp,pos);
}
while(1)
{
pos=strExp->find('%');
if(pos==-1) break;
else Calcu(strExp,pos);
}
pos=0;
if(strExp->at(0)=='-' ||strExp->at(0)=='+') strExp->insert(0,"0");
while(1)
{
int tmpos=Right(strExp, strExp->length()-pos).find('-');
if(tmpos==-1) break;
else pos+=tmpos;
if(pos==0 && strExp->at(pos+1)=='-' || pos>0)
{
if(strExp->at(pos+1)=='+')
{
strExp->erase(pos+1,1);
pos=0;
continue;
}
/*********处理连减(如:----1)的情况***********/
int nCount=0;
while(1)
{
if(++pos>0)
{
if(strExp->at(pos)!='-') break;
else nCount++;
}
else break;
}
if(nCount>0)
{
strExp->erase(pos-nCount-1,nCount+1);
if(nCount%2==0) strExp->insert(pos-nCount-1,"-");
else if(pos-nCount-1>0) strExp->insert(pos-nCount-1,"+");
pos=0;
continue;
}
else pos--;
/***********************************************/
if(pos>0 && strExp->at(pos-1)=='+')
{
pos++;
continue;
}
Calcu(strExp,pos);
}
else pos++;
}
pos=0;
while(1)
{
pos=strExp->find('+');
// int tmpos=strExp->Right(strExp->length()-pos).find('+');
if(pos==-1) break;
// else pos+=tmpos;
if(pos==0 && strExp->at(pos+1)=='+' || pos>0)
{
/*********处理连加(如:++++1)的情况***********/
int nCount=0;
while(1)
{
if(++pos>0)
{
if(strExp->at(pos)!='+') break;
else nCount++;
}
else break;
}
if(nCount>0)
{
strExp->erase(pos-nCount-1,nCount+1);
strExp->insert(pos-nCount-1,"+");
pos=0;
continue;
}
else pos--;
/***********************************************/
Calcu(strExp,pos);
}
else pos++;
}
}
void CCalculation::Calcu(string *strExp,int pos)
{
char ch;
for(int j=pos-1;j>=0;j--)
{
ch=strExp->at(j);
if(ch=='+' ||ch=='-' ||ch=='*'||ch=='/' ||ch=='%' ||ch=='^' )
{
if(j==0 && ch!='-') {*strExp="ERROR_缺少参数_";return;}
if(j==0 && ch=='-') j=-1;//防止把负号误判为减号
else if(j>0 && ch=='-' && !IsDigital(strExp->substr(j-1,1))) j--;
break;
}
}
for(int k=pos+1;k<strExp->length();k++)
{
ch=strExp->at(k);
if(ch=='+' ||ch=='-' ||ch=='*'||ch=='/' ||ch=='%' ||ch=='^' )
{
if(ch=='-' && k>pos+1) break;
if(ch=='-' && k>0 && IsDigital(strExp->substr(k+1,1)) ) k++;
else break;
}
}
string strExp2=strExp->substr(j+1,k-j-1);
*strExp=Left(strExp, j+1)+TwoE(strExp2)+Right(strExp, strExp->length()-k);
if(strExp->find("#IN")!=-1) {*strExp="ERROR_结果有溢出或值域越界_";return;}
if(!SynRes(strExp)) {*strExp="ERROR_缺少运算符_";return;}
MinusMinus(strExp);
}
string CCalculation::MainPro(string ex_strExp)
{
if(ex_strExp.empty()) return "表达式不能为空";
Macro(&ex_strExp);
char * tmp = (char *)ex_strExp.c_str();
strlwr(tmp);
string strExp(tmp); //表达式全部小写
/**********给表达式加上保护括号************/
strExp.insert(0,"(");
strExp+=")";
/******************************************/
int pos=strExp.find(" ");
int n=BraCheck(strExp);
//string str;
//str.Format("%d",abs(n));
char temp[10];
sprintf(temp, "%d", abs(n));
string str(temp);
if(n==1) strExp+=")";
else if(n==-1) strExp.insert(0,"(");
else if(n>0) return "缺少"+str+"个右括号";
else if(n<0) return "缺少"+str+"个左括号";
while(pos!=-1) //去掉表达式中的空格符
{
strExp.erase(pos,1);
pos=strExp.find(" ");
}
Oct2Dec(&strExp); //将表达式中的八进制数转换成十进制
Hex2Dec(&strExp); //将表达式中的十六进制数转换成十进制
Bin2Dec(&strExp); //将表达式中的二进制数转换成十进制
while(!IsDigital(strExp))
{
DelBracket(&strExp);
if(!SynRes(&strExp)) return strExp;
}
if(!SynRes(&strExp)) return strExp;
else return ModiResult(strExp);
}
void CCalculation::Macro(string *strExp)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -