📄 eval.cpp
字号:
// Eval.cpp: implementation of the CEval class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Eval.h"
#include "Stack.h"
#include "ComServer.h"
#include "math.h"
#include "PrvString.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
#define STATE_NONE 0
#define STATE_OPERAND 1
#define STATE_OPERATOR 2
#define STATE_UNARYOP 3
#define STATE_lOGICNOT 4
#define UNARY_NEG "(-)"
#define LOGIC_NOT "(!)"
#define STATE_SIN 5
#define STATE_COS 6
#define TRIANGLE_SIN "($)"
#define TRIANGLE_COS "(@)"
CEval::CEval()
{
}
CEval::~CEval()
{
}
bool CEval::IsSymbolChar(CPrvString sChar)
{
CPrvString m;
char a;
bool returnflg;
int i;
m.SetValue("0123456789_");
returnflg=false;
sChar.MakeUpper();
a=sChar.GetAt(0);
i=m.Find(a);
if ((a >= 'A' && a <= 'Z') || (i!=-1))
returnflg = true;
return returnflg;
}
bool CEval::IsSymbolCharFirst(CPrvString sChar)
{
char a;
bool returnflg;
returnflg=false;
sChar.MakeUpper();
a=sChar.GetAt(0);
if ((a >= 'A' && a <= 'Z') || (a=='_'))
returnflg = true;
return returnflg;
}
int CEval::GetPrecedence(CPrvString ch)
{
int returnflg;
int cm0,cm1,cm2,cm3,cm4,cm5,cm6;
returnflg=0;
cm0=-1;
cm1=-1;
cm2=-1;
cm3=-1;
cm4=-1;
cm5=-1;
cm6=-1;
if (ch.GetLength()>1)
{
cm0=strcmp(ch.GetValue(),UNARY_NEG);
cm1=strcmp(ch.GetValue(),LOGIC_NOT);
cm2=strcmp(ch.GetValue(),TRIANGLE_SIN);
cm3=strcmp(ch.GetValue(),TRIANGLE_COS);
//if ((ch.GetValue() ==UNARY_NEG) || (ch.GetValue() ==LOGIC_NOT))
if ((cm0 ==0) || (cm1 ==0))
returnflg = 10;
//else if ((ch.GetValue()==TRIANGLE_SIN) || (ch.GetValue() ==TRIANGLE_COS))
else if ((cm2==0) || (cm3 ==0))
returnflg = 9;
else
returnflg = 0;
}
else
{
cm0=strcmp(ch.GetValue(),"&");
cm1=strcmp(ch.GetValue(),"|");
cm2=strcmp(ch.GetValue(),"^");
cm3=strcmp(ch.GetValue(),"+");
cm4=strcmp(ch.GetValue(),"-");
cm5=strcmp(ch.GetValue(),"*");
cm6=strcmp(ch.GetValue(),"/");
//if ((ch.GetValue() =='&') || (ch=='|') || (ch=='^')) //按位与、或、异或
if ((cm0 ==0) || (cm1==0) || (cm2==0)) //按位与、或、异或
returnflg = 1;
else if ((cm3==0) || (cm4==0))
returnflg = 2;
//else if ((ch=='*') || (ch=='/'))
else if ((cm5==0) || (cm6==0))
returnflg = 3;
//else if (ch=="^")
// returnflg = 4;
else
returnflg = 0;
}
return returnflg;
}
bool CEval::IsNumric(CPrvString sss)
{
CPrvString m;
bool returnflg;
int i,j,k;
char cc;
m.SetValue("0123456789.-");
returnflg=true;
j=sss.GetLength();
for (k=0;k<j;k++)
{
cc=sss.GetAt(k);
i=m.Find(cc);
if (i==-1)
{
returnflg = false;
break;
}
}
return returnflg;
}
float CEval::DoEvaluate(CPrvString sExpression)
{
int i, j,k,len;
int decimal, sign;
CStack stkTokens;
CPrvString sTemp;
CPrvString result;
float x1,x2,res;
char resbuf[50];
//char resbuf[20];
float returnflg;
short op1,op2,op3;
char *bufftemp;
char bufftemp0[256];
int yy;
CPrvString buffer;
int yy0;
int cm0,cm1,cm2,cm3,cm4,cm5,cm6,cm7,cm8,cm9,cm10;
result.SetValue("00000000000");
i = 1;
returnflg=0;
j=sExpression.Find(' ');
while (j!=-1)
{
//sTemp=sExpression.Mid(i-1,j-i+1);
sTemp=sExpression.Mid(i-1,j-i+1);
if (((SinIsNumric(sTemp))==1) && (sTemp.GetLength()==1))
stkTokens.Push(sTemp);
else if(((IsNumric(sTemp))==1) && (sTemp.GetLength()>1))
stkTokens.Push(sTemp);
else
{
cm0=-1;
cm1=-1;
cm2=-1;
cm3=-1;
cm4=-1;
cm5=-1;
cm6=-1;
cm7=-1;
cm8=-1;
cm9=-1;
cm10=-1;
cm0=strcmp(sTemp.GetValue(),UNARY_NEG);
cm1=strcmp(sTemp.GetValue(),LOGIC_NOT);
cm2=strcmp(sTemp.GetValue(),TRIANGLE_SIN);
cm3=strcmp(sTemp.GetValue(),TRIANGLE_COS);
cm4=strcmp(sTemp.GetValue(),"+");
cm5=strcmp(sTemp.GetValue(),"-");
cm6=strcmp(sTemp.GetValue(),"*");
cm7=strcmp(sTemp.GetValue(),"/");
cm8=strcmp(sTemp.GetValue(),"^");
cm9=strcmp(sTemp.GetValue(),"&");
cm10=strcmp(sTemp.GetValue(),"|");
if (sTemp.GetLength()>1)
{
//if (sTemp==UNARY_NEG)
if (cm0==0)
{
x1=(float)atof(stkTokens.pop().GetValue());
res=-x1;
buffer.SetValue("");
bufftemp = _fcvt( res, 3, &decimal, &sign );
if (sign==1)
//buffer='-';
buffer.SetValue("-");
if (decimal==0)
{ buffer.Connect("0.");
buffer.Connect(bufftemp);
//buffer=buffer+'0'+'.'+bufftemp;
}
else
{
for (yy=0;yy<decimal;yy++)
//buffer=buffer+bufftemp[yy];
bufftemp0[yy]=bufftemp[yy];
bufftemp0[yy]='\0';
buffer.Connect(bufftemp0);
//buffer=buffer+'.';
buffer.Connect(".");
yy0=0;
for (yy;(unsigned)yy<strlen(bufftemp);yy++)
// buffer=buffer+bufftemp[yy];
{
bufftemp0[yy0]=bufftemp[yy];
yy0++;
}
bufftemp0[yy0]='\0';
buffer.Connect(bufftemp0);
}
//result.SetValue(buffer.GetValue());
stkTokens.Push (buffer);
}
//else if(sTemp==LOGIC_NOT)
else if(cm1==0)
{
op1=atoi(stkTokens.pop().GetValue());
if (op1!=0 )
op3=0;
else
op3=1;
ltoa((long)op3, resbuf,10);
len=strlen(resbuf);
//if (len<9)
//{
for (k=0;k<len;k++)
result.SetAt (k,resbuf[k]);
result.SetAt (k,'\0');
//}
result.SetValue(resbuf);
stkTokens.Push(result);
}
//else if(sTemp==TRIANGLE_SIN)
else if(cm2==0)
{
x1=(float)atof(stkTokens.pop().GetValue());
res=(float)sin(x1);
bufftemp = _fcvt( res, 3, &decimal, &sign );
buffer.SetValue("");
if (sign==1)
buffer.SetValue("-");
if (decimal==0)
{ //buffer=buffer+'0'+'.'+bufftemp;
buffer.Connect("0.");
buffer.Connect(bufftemp);
}
else
{
for (yy=0;yy<decimal;yy++)
// buffer=buffer+bufftemp[yy];
bufftemp0[yy]=bufftemp[yy];
bufftemp0[yy]='\0';
buffer.Connect(bufftemp0);
buffer.Connect(".");
// buffer=buffer+'.';
yy0=0;
for (yy;(unsigned)yy<strlen(bufftemp);yy++)
// buffer=buffer+bufftemp[yy];
{
bufftemp0[yy0]=bufftemp[yy];
yy0++;
}
bufftemp0[yy0]='\0';
buffer.Connect(bufftemp0);
}
stkTokens.Push(buffer);
}
//else if(sTemp==TRIANGLE_COS)
else if(cm3==0)
{
x1=(float)atof(stkTokens.pop().GetValue());
res=(float)cos(x1);
bufftemp = _fcvt( res, 3, &decimal, &sign );
buffer.SetValue("");
if (sign==1)
buffer.SetValue("-");
if (decimal==0)
{ //buffer=buffer+'0'+'.'+bufftemp;
buffer.Connect("0.");
buffer.Connect(bufftemp);
}
else
{
for (yy=0;yy<decimal;yy++)
// buffer=buffer+bufftemp[yy];
bufftemp0[yy]=bufftemp[yy];
bufftemp0[yy]='\0';
buffer.Connect(bufftemp0);
buffer.Connect(".");
// buffer=buffer+'.';
yy0=0;
for (yy;(unsigned)yy<strlen(bufftemp);yy++)
// buffer=buffer+bufftemp[yy];
{
bufftemp0[yy0]=bufftemp[yy];
yy0++;
}
bufftemp0[yy0]='\0';
buffer.Connect(bufftemp0);
}
// result=buffer;
stkTokens.Push(buffer);
}
}
else
{
//if (sTemp=="+")
if (cm4==0)
{
x1=(float)atof(stkTokens.pop().GetValue());
x2=(float)atof(stkTokens.pop().GetValue());
res=x1+x2;
bufftemp = _fcvt( res, 3, &decimal, &sign );
buffer.SetValue("");
if (sign==1)
buffer.SetValue("-");
if (decimal==0)
{ // buffer=buffer+'0'+'.'+bufftemp;
buffer.Connect("0.");
buffer.Connect(bufftemp);
}
else
{
for (yy=0;yy<decimal;yy++)
// buffer=buffer+bufftemp[yy];
bufftemp0[yy]=bufftemp[yy];
bufftemp0[yy]='\0';
buffer.Connect(bufftemp0);
buffer.Connect(".");
// buffer=buffer+'.';
yy0=0;
for (yy;(unsigned)yy<strlen(bufftemp);yy++)
// buffer=buffer+bufftemp[yy];
{
bufftemp0[yy0]=bufftemp[yy];
yy0++;
}
bufftemp0[yy0]='\0';
buffer.Connect(bufftemp0);
}
//result=buffer;
stkTokens.Push(buffer);
}
//else if (sTemp=="-")
else if (cm5==0)
{
x1=(float)atof(stkTokens.pop().GetValue());
x2=(float)atof(stkTokens.pop().GetValue());
res=x2-x1;
buffer.SetValue ("");
bufftemp = _fcvt( res, 3, &decimal, &sign );
if (sign==1)
buffer.SetValue("-");
if (decimal==0)
{ //buffer=buffer+'0'+'.'+bufftemp;
buffer.Connect("0.");
buffer.Connect(bufftemp);
}
else
{
for (yy=0;yy<decimal;yy++)
// buffer=buffer+bufftemp[yy];
bufftemp0[yy]=bufftemp[yy];
bufftemp0[yy]='\0';
buffer.Connect(bufftemp0);
buffer.Connect(".");
// buffer=buffer+'.';
yy0=0;
for (yy;(unsigned)yy<strlen(bufftemp);yy++)
// buffer=buffer+bufftemp[yy];
{
bufftemp0[yy0]=bufftemp[yy];
yy0++;
}
bufftemp0[yy0]='\0';
buffer.Connect(bufftemp0);
}
// result=buffer;
stkTokens.Push(buffer);
}
//else if (sTemp=="*")
else if (cm6==0)
{
x1=(float)atof(stkTokens.pop().GetValue());
x2=(float)atof(stkTokens.pop().GetValue());
res=x1*x2;
buffer.SetValue("");
bufftemp = _fcvt( res, 3, &decimal, &sign );
if (sign==1)
buffer.SetValue("-");
if (decimal==0)
{ //buffer=buffer+'0'+'.'+bufftemp;
buffer.Connect("0.");
buffer.Connect(bufftemp);
}
else
{
for (yy=0;yy<decimal;yy++)
// buffer=buffer+bufftemp[yy];
bufftemp0[yy]=bufftemp[yy];
bufftemp0[yy]='\0';
buffer.Connect(bufftemp0);
buffer.Connect(".");
// buffer=buffer+'.';
yy0=0;
for (yy;(unsigned)yy<strlen(bufftemp);yy++)
// buffer=buffer+bufftemp[yy];
{
bufftemp0[yy0]=bufftemp[yy];
yy0++;
}
bufftemp0[yy0]='\0';
buffer.Connect(bufftemp0);
}
// result=buffer;
stkTokens.Push(buffer);
}
//else if (sTemp=="/")
else if (cm7==0)
{
x1=(float)atof(stkTokens.pop().GetValue());
x2=(float)atof(stkTokens.pop().GetValue());
res=x2/x1;
buffer.SetValue("");
bufftemp = _fcvt( res, 3, &decimal, &sign );
if (sign==1)
buffer.SetValue("-");
if (decimal==0)
{ //buffer=buffer+'0'+'.'+bufftemp;
buffer.Connect("0.");
buffer.Connect(bufftemp);
}
else
{
for (yy=0;yy<decimal;yy++)
// buffer=buffer+bufftemp[yy];
bufftemp0[yy]=bufftemp[yy];
bufftemp0[yy]='\0';
buffer.Connect(bufftemp0);
buffer.Connect(".");
// buffer=buffer+'.';
yy0=0;
for (yy;(unsigned)yy<strlen(bufftemp);yy++)
// buffer=buffer+bufftemp[yy];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -