12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403 |
- // StringResolution.cpp: implementation of the CComputer class.
- #include "stdafx.h"
- #include "computer.h"
- #include <math.h>
- #include "kernel.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- extern int MyRound(double value);
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CComputer::CComputer(char*pszFormula)
- :m_nOprandNum(0),m_nOprandANum(0),m_nOperatorNum(0)
- {
- m_pOperator = NULL;
- m_pOprand = NULL;
- m_pOprandA = NULL;
- if (pszFormula)
- {
- m_strFormula = pszFormula;
- Initialize();
- }
- }
- CComputer::~CComputer()
- {
- if (m_pOperator)
- delete[]m_pOperator;
- m_pOperator = NULL;
- if(m_pOprand)
- delete[]m_pOprand;
- m_pOprand = NULL;
- if (m_pOprandA)
- delete[]m_pOprandA;
- m_pOprandA = NULL;
- }
- void CComputer::SetFormula(CString strFormula)
- {
- if (!strFormula)
- {
- return;
- }
- m_strFormula = strFormula;
- if (m_pOperator)
- {
- delete[]m_pOperator;
- m_pOperator = NULL;
- }
- if (m_pOprand)
- {
- delete[]m_pOprand;
- m_pOprand = NULL;
- }
- if (m_pOprandA)
- {
- delete[]m_pOprandA;
- m_pOprandA = NULL;
- }
- m_nOprandNum = 0,m_nOprandANum = 0,m_nOperatorNum = 0;
- Initialize();
- }
- int CComputer::IsFormula()
- {
- stack<char> charStack;
- int i,num = m_strFormula.GetLength();
- char notChar[] = {';','\'',':',' ','[',']','{','}','\\','|',',','\"','&','%','$','@','#','`','~','?'};//'!', modify by 2012.06.01
- int notNum = sizeof(notChar);
- for (i=0;i<num;i++)
- {
- for (int j=0;j<notNum;j++)
- {
- if (m_strFormula[i]==notChar[j])
- {
- CString s;
- s.Format("The %dth character,\"%c\" is invalidated!",i+1,m_strFormula[i]);
- //LOG4C((LOG_NOTICE, "< %s >",s));
- return -i-1;//the ith symbol is invalidating.
- }
- }
- }
- for (i=0;i<num;i++)
- {
- if (m_strFormula[i]=='(')
- {
- charStack.push('(');
- }
- if (m_strFormula[i]==')')
- {
- if (charStack.empty())
- {
- return 1;//right bracket if more.
- }
- else
- {
- charStack.pop();
- }
- }
- }
- if (!charStack.empty())
- {
- return 2;//left bracket is more.
- }
- return 0;
- }
- BOOL CComputer::GetStack(OperatorStack &Operator, OperandStack &Oprand)
- {
- if (IsFormula())
- {
- return FALSE;//illogicality return
- }
- BOOL bRet = TRUE;
- EmptyStack(Operator);
- EmptyStack(Oprand);
- CString str = m_strFormula;
- bRet = bRet&&GetOperatorStack(Operator,str);
- bRet = bRet&&GetOperandStack(Oprand,str);
- return bRet;
- }
- BOOL CComputer::Initialize()
- {
- BOOL bRet = FALSE;
- OperatorStack Op; OperandStack Od;
- bRet = GetStack(Op,Od);
- if (!bRet)
- {
- EmptyStack(Op);
- EmptyStack(Od);
- return FALSE;
- }
- m_nOprandNum = (int)Od.size(),m_nOperatorNum = (int)Op.size();
- if (m_nOperatorNum)
- {
- m_pOperator = new COperator[m_nOperatorNum];
- }
- if (m_nOprandNum)
- {
- m_pOprand = new COperand[m_nOprandNum];
- }
- int i;
- for (i=0;i<m_nOperatorNum;i++)
- {
- COperator*op = Op.top();
- m_pOperator[m_nOperatorNum-i-1] = *op;
- Op.pop();
- delete op;
- }
- for (i=0;i<m_nOprandNum;i++)
- {
- COperand*od = Od.top();
- m_pOprand[m_nOprandNum-i-1] = *od;
- Od.pop();
- delete od;
- }
- m_pOprandA = new COperand[m_nOprandNum];
- BOOL IsIn = FALSE;
- for (i=0;i<m_nOprandNum;i++)
- {
- for (int j=0;j<m_nOprandANum;j++)
- {
- if (m_pOprand[i]==m_pOprandA[j])
- {
- IsIn = TRUE;
- break;
- }
- }
- if (!IsIn&&!m_pOprand[i].m_bIsConst)
- {
- m_pOprandA[m_nOprandANum] = m_pOprand[i];
- m_nOprandANum++;
- }
- IsIn = FALSE;
- }
- return TRUE;
- }
- double CComputer::Computer(OperatorStack &Operator, OperandStack &Oprand)
- {
- double value =0.0;
- if (Operator.empty())
- {
- if (Oprand.size()==1)
- {
- COperand* od = Oprand.top();
- value = atof(od->m_strName);
- delete od;
- return value;
- }
- else
- {
- while (!Oprand.empty())
- {
- COperand*od = Oprand.top();
- Oprand.pop();
- delete od;
- }
- return BIGNUMBER;
- }
- }
- OperatorStack tmpOO;
- OperandStack tmpOD;
- COperator* op = 0,*op1 = 0;
- COperand *oprand = 0,*oprand1 = 0;
- op = Operator.top();
- Operator.pop();
- if (!Operator.empty())
- {
- op1 = Operator.top();
- }
- while (op1&&(op1->m_nLevel > op->m_nLevel))
- {
- tmpOO.push(op);
- if (op->m_nType==BINARY)
- {
- if (!Oprand.empty())
- {
- oprand = Oprand.top();
- Oprand.pop();
- tmpOD.push(oprand);
- }
- }
- op = op1;
- Operator.pop();
- if (!Operator.empty())
- {
- op1 = Operator.top();
- }
- else
- {
- op1 = 0;
- }
- }
- if (op->m_nType==UNARY)
- {
- if (Oprand.empty())
- {
- return BIGNUMBER;
- }
- oprand = Oprand.top();
- double x = computing(op,oprand);
- oprand->m_strName.Format("%g",x);
- }
- else
- {
- if (Oprand.empty())
- {
- return BIGNUMBER;
- }
- oprand1 = Oprand.top();
- Oprand.pop();
- if (Oprand.empty())
- {
- return BIGNUMBER;
- }
- oprand = Oprand.top();
- double x = computing(op,oprand1,oprand);
- oprand->m_strName.Format("%g",x);
- delete oprand1;
- }
- delete op;
- while (!tmpOO.empty())
- {
- op = tmpOO.top();
- tmpOO.pop();
- Operator.push(op);
- }
- while (!tmpOD.empty())
- {
- oprand = tmpOD.top();
- tmpOD.pop();
- Oprand.push(oprand);
- }
- return Computer(Operator,Oprand);
- }
- double CComputer::computer(double variantValue[], int num)
- {
- double value = 0.0;
- int i;
- if (num<m_nOprandANum)
- {
- //LOG4C((LOG_NOTICE, "computer error"));
- return BIGNUMBER;
- }
- OperatorStack Operator;
- OperandStack Oprand;
- for (i=0;i<m_nOperatorNum;i++)
- {
- COperator*op = new COperator;
- *op = m_pOperator[m_nOperatorNum-i-1];
- Operator.push(op);
- }
- for (i=0;i<m_nOprandNum;i++)
- {
- COperand*od = new COperand;
- *od = m_pOprand[m_nOprandNum-i-1];
- for(int j=0;j<m_nOprandANum;j++)
- {
- if (*od==m_pOprandA[j])
- {
- od->m_strName.Format("%g",variantValue[j]);
- }
- }
- Oprand.push(od);
- }
- value = Computer(Operator,Oprand);
- return value;
- }
- CString CComputer::GetErrorInformation()
- {
- CString value;
- return value;
- }
- BOOL CComputer::EmptyStack(OperatorStack Op)
- {
- while(!Op.empty())
- {
- COperator*op = Op.top();
- Op.pop();
- delete op;
- }
- return TRUE;
- }
- BOOL CComputer::EmptyStack(OperandStack Od)
- {
- while (!Od.empty())
- {
- COperand*od = Od.top();
- Od.pop();
- delete od;
- }
- return TRUE;
- }
- BOOL CComputer::GetOperatorStack(OperatorStack &Operator, CString &string)
- {
- BOOL bRet = TRUE;
- int num = string.GetLength();
- int i = 0;
- int level = 0;
- while (i<num)
- {
- if (string[i]=='(')
- {
- level+=LEVELS;
- string.SetAt(i,'#');
- i++;
- }
- else if (string[i]==')')
- {
- level-=LEVELS;
- string.SetAt(i,'@');
- i++;
- }
- else if (string[i]=='+'||string[i]=='-')
- {
- COperator* op=new COperator;
- op->m_nLevel = level;
- op->m_strOperator = string.Mid(i,1);
- op->m_nStartIndex = i;
- if(i==0||string[i-1]=='#')
- {
- op->m_nType = UNARY;
- }
- else
- {
- op->m_nType = BINARY;
- }
- Operator.push(op);
- string.SetAt(i,'@');
- i++;
- }
- else if (string[i]=='*'||string[i]=='/')
- {
- COperator* op = new COperator;
- op->m_nLevel = level+1;
- op->m_strOperator = string.Mid(i,1);
- op->m_nStartIndex = i;
- op->m_nType = BINARY;
- Operator.push(op);
- string.SetAt(i,'@');
- i++;
- }
- else if (string[i]=='^')
- {
- COperator* op=new COperator;
- op->m_nLevel = level+2;
- op->m_strOperator = string.Mid(i,1);
- op->m_nStartIndex = i;
- op->m_nType = BINARY;
- Operator.push(op);
- string.SetAt(i,'@');
- i++;
- }
- else if (string.Mid(i,4)=="SQRT")
- {
- COperator* op=new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "SQRT";
- op->m_nStartIndex = i;
- op->m_nType = UNARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');string.SetAt(i+3,'@');
- i+=3;
- }
- else if (string.Mid(i,3)=="LOG")
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "LOG";
- op->m_nStartIndex = i;
- op->m_nType = UNARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
- i+=3;
- }
- else if (string.Mid(i,3)=="SIN")
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "SIN";
- op->m_nStartIndex = i;
- op->m_nType = UNARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
- i+=3;
- }
- else if (string.Mid(i,3)=="COS")
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "COS";
- op->m_nStartIndex = i;
- op->m_nType = UNARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
- i+=3;
- }
- else if (string.Mid(i,3)=="TAN")
- {
- COperator* op=new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "TAN";
- op->m_nStartIndex = i;
- op->m_nType = UNARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
- i+=3;
- }
- else if (string.Mid(i,3)=="COT")
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "COT";
- op->m_nStartIndex = i;
- op->m_nType = UNARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
- i+=3;
- }
- else if (string.Mid(i,4)=="ASIN")
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "ASIN";
- op->m_nStartIndex = i;
- op->m_nType = UNARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');string.SetAt(i+3,'@');
- i+=3;
- }
- else if (string.Mid(i,4)=="ACOS")
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "ACOS";
- op->m_nStartIndex = i;
- op->m_nType = UNARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');string.SetAt(i+3,'@');
- i+=3;
- }
- else if (string.Mid(i,4)=="ATAN")
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "ATAN";
- op->m_nStartIndex = i;
- op->m_nType = UNARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');string.SetAt(i+3,'@');
- i+=3;
- }
- else
- {
- i++;
- }
- }
- return bRet;
- }
- BOOL CComputer::GetOperandStack(OperandStack &Oprand, CString &string)
- {
- int i = 0;
- int num = string.GetLength();
- while (i<num)
- {
- if (string[i]=='#'||string[i]=='@')
- {
- i++;
- }
- else
- {
- COperand * oprand=new COperand;
- if(isdigit(string[i]))
- {
- oprand->m_bIsConst = TRUE;
- }
- oprand->m_nStartIndex = i;
- BOOL HasPoint = FALSE;
- while (i<num&&string[i]!='#'&&string[i]!='@')
- {
- if (string[i]=='.')
- {
- if (HasPoint)
- {
- if( oprand )
- delete oprand;
- return FALSE;
- }
- else
- {
- HasPoint=TRUE;
- }
- }
- if (oprand->m_bIsConst&&!isdigit(string[i])&&(string[i]!='.'))
- {
- CString s;
- s.Format("Variant cann't start with digital (%d in expression)!",i+1-oprand->m_strName.GetLength());
- if( oprand )
- delete oprand;
- return FALSE;
- }
- oprand->m_strName+=string.Mid(i,1);
- i++;
- }
- Oprand.push(oprand);
- }
- }
- return TRUE;
- }
- int CComputer::GetErrorNumber()
- {
- return 0;
- }
- double CComputer::computing(const COperator*op,const COperand*oprand)
- {
- double x = atof(oprand->m_strName);
- if (op->m_strOperator=="-")
- {
- x = -x;
- }
- else if (op->m_strOperator=="LOG")
- {
- if(x>0)
- {
- x = log(x);
- }
- else
- {
- x = BIGNUMBER;
- }
- }
- else if (op->m_strOperator=="SQRT")
- {
- if ( x>=0 )
- {
- x = sqrt(x);
- }
- else
- {
- x = BIGNUMBER;
- }
- }
- else if (op->m_strOperator=="SIN")
- {
- x = sin(x);
- }
- else if (op->m_strOperator=="COS")
- {
- x = cos(x);
- }
- else if (op->m_strOperator=="TAN")
- {
- x = tan(x);
- }
- else if (op->m_strOperator=="COT")
- {
- if(fabs(sin(x))>DERROR)
- {
- x = cos(x)/sin(x);
- }
- else
- {
- x = BIGNUMBER;
- }
- }
- else if (op->m_strOperator=="ASIN")
- {
- x = asin(x);
- }
- else if (op->m_strOperator=="ACOS")
- {
- x = acos(x);
- }
- else if (op->m_strOperator=="ATAN")
- {
- x = atan(x);
- }
- return x;
- }
- double CComputer::computing(const COperator*op,const COperand*Loprand,const COperand*Roprand)
- {
- double x = BIGNUMBER;
- switch(op->m_strOperator[0])
- {
- case '+':
- x = atof(Loprand->m_strName)+atof(Roprand->m_strName);
- break;
- case '-':
- x = atof(Loprand->m_strName)-atof(Roprand->m_strName);
- break;
- case '*':
- x = atof(Loprand->m_strName)*atof(Roprand->m_strName);
- break;
- case '/':
- {
- double y = atof(Roprand->m_strName);
- x = atof(Loprand->m_strName);
- if (fabs(y)>DERROR)
- {
- x = x/y;
- }
- else
- {
- x = BIGNUMBER;
- }
- }
- break;
- case '^':
- {
- double y = atof(Roprand->m_strName);
- x = atof(Loprand->m_strName);
- x = pow(x,y);
- }
- break;
- }
- return x;
- }
- CString CComputer::GetDigitalString(double* variantValue,int num)
- {
- CString value=m_strFormula;
- if (num>=m_nOprandANum)
- {
- int i,j;
- CString* ppchar;
- ppchar=new CString[m_nOprandNum+1];
- for (i=0;i<=m_nOprandNum;i++)
- {
- if (i==0)
- {
- ppchar[0]=m_strFormula.Left(m_pOprand[0].m_nStartIndex);
- }
- else if (i<m_nOprandNum)
- {
- WORD start=m_pOprand[i-1].m_nStartIndex+m_pOprand[i-1].m_strName.GetLength();
- WORD length=m_pOprand[i].m_nStartIndex-start;
- ppchar[i]=m_strFormula.Mid(start,length);
- }
- else
- {
- WORD start=m_pOprand[i-1].m_nStartIndex+m_pOprand[i-1].m_strName.GetLength();
- ppchar[i]=m_strFormula.Mid(start);
- }
- }
- CString* digitchar = new CString[m_nOprandNum];
- for (i=0;i<m_nOprandNum;i++)
- {
- if (m_pOprand[i].m_bIsConst)
- {
- digitchar[i]=m_pOprand[i].m_strName;
- }
- else
- {
- for (j=0;j<m_nOprandANum;j++)
- {
- if (m_pOprand[i]==m_pOprandA[j])
- {
- if (variantValue[j]<0)
- {
- digitchar[i].Format("(%g)",variantValue[j]);
- }
- else
- {
- digitchar[i].Format("%g",variantValue[j]);
- }
- } // end if
- } // end for
- } // end else
- }
- value = ppchar[0];
- for (i=0;i<m_nOprandNum;i++)
- {
- value += digitchar[i]+ppchar[i+1];
- }
- delete[]ppchar;
- delete[]digitchar;
- }
- return value;
- }
- BOOL CComputer::ExpressionIsError()
- {
- #ifndef _ERROR_INFO_H_
- return 0;
- #else
- GET_EXPRESSION_ERROR
- #endif
- }
- const COperand* CComputer::GetVariantTable()
- {
- return m_pOprandA;
- }
- COperator::COperator():m_nStartIndex(0),m_nLevel(0),m_nType(BINARY)
- {
- }
- COperator::~COperator()
- {
- }
- COperand::COperand():m_nStartIndex(0)
- {
- m_bIsConst = FALSE;
- }
- COperand::~COperand()
- {
- }
- BOOL COperand::operator ==(const COperand & od)
- {
- BOOL bRet=TRUE;
- bRet = (m_bIsConst==od.m_bIsConst);
- bRet = (bRet&&(m_strName==od.m_strName));
- return bRet;
- }
- void CComputer::Destroy()
- {
- if (m_pOperator)
- {
- delete[]m_pOperator;
- m_pOperator = NULL;
- }
- if (m_pOprand)
- {
- delete[]m_pOprand;
- m_pOprand = NULL;
- }
- if (m_pOprandA)
- {
- delete[]m_pOprandA;
- m_pOprandA = NULL;
- }
- m_nOprandNum=0;
- m_nOprandANum=0;
- m_nOperatorNum=0;
- }
- BOOL CRealComputer::GetOperatorStack(OperatorStack&Operator,CString &string)
- {
- BOOL bRet = TRUE;
- int num = string.GetLength();
- int i = 0;
- int level = 0;
- while (i<num)
- {
- if (string[i]=='(')
- {
- level+=LEVELS;
- string.SetAt(i,'#');
- i++;
- }
- else if (string[i]==')')
- {
- level-=LEVELS;
- string.SetAt(i,'@');
- i++;
- }
- else if (string[i]=='+'||string[i]=='-')
- {
- COperator* op = new COperator;
- op->m_nLevel = level;
- op->m_strOperator = string.Mid(i,1);
- op->m_nStartIndex = i;
- if(i==0||string[i-1]=='#')
- {
- op->m_nType=UNARY;
- }
- else
- {
- op->m_nType = BINARY;
- }
- Operator.push(op);
- string.SetAt(i,'@');
- i++;
- }
- else if (string[i]=='*'||string[i]=='/')
- {
- COperator* op = new COperator;
- op->m_nLevel = level+1;
- op->m_strOperator = string.Mid(i,1);
- op->m_nStartIndex = i;
- op->m_nType = BINARY;
- Operator.push(op);
- string.SetAt(i,'@');
- i++;
- }
- else if (string[i]=='^')
- {
- COperator* op = new COperator;
- op->m_nLevel = level+2;
- op->m_strOperator = string.Mid(i,1);
- op->m_nStartIndex = i;
- op->m_nType = BINARY;
- Operator.push(op);
- string.SetAt(i,'@');
- i++;
- }
- else if (string.Mid(i,4)=="SQRT")
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "SQRT";
- op->m_nStartIndex = i;
- op->m_nType = UNARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');string.SetAt(i+3,'@');
- i+=4;
- }
- else if (string.Mid(i,3)=="LOG")
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "LOG";
- op->m_nStartIndex = i;
- op->m_nType = UNARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
- i+=3;
- }
- else if (string.Mid(i,3)=="SIN")
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "SIN";
- op->m_nStartIndex = i;
- op->m_nType = UNARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
- i+=3;
- }
- else if (string.Mid(i,3)=="COS")
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "COS";
- op->m_nStartIndex = i;
- op->m_nType = UNARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
- i+=3;
- }
- else if (string.Mid(i,3)=="TAN")
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "TAN";
- op->m_nStartIndex = i;
- op->m_nType = UNARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
- i+=3;
- }
- else if (string.Mid(i,3)=="COT")
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "COT";
- op->m_nStartIndex = i;
- op->m_nType = UNARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
- i+=3;
- }
- else if (string.Mid(i,4)=="ASIN")
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "ASIN";
- op->m_nStartIndex = i;
- op->m_nType = UNARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');string.SetAt(i+3,'@');
- i+=4;
- }
- else if (string.Mid(i,4)=="ACOS")
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "ACOS";
- op->m_nStartIndex = i;
- op->m_nType = UNARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');string.SetAt(i+3,'@');
- i+=4;
- }
- else if (string.Mid(i,4)=="ATAN")
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "ATAN";
- op->m_nStartIndex = i;
- op->m_nType = UNARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');string.SetAt(i+3,'@');
- i+=4;
- }
- else if (string.Mid(i,3)=="ABS")
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "ABS";
- op->m_nStartIndex = i;
- op->m_nType = UNARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
- i+=3;
- }
- else if (string.Mid(i,3)=="MOD")
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "MOD";
- op->m_nStartIndex = i;
- op->m_nType = BINARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
- i+=3;
- }
- else if (string.Mid(i,3)=="RND")
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "RND";
- op->m_nStartIndex = i;
- op->m_nType = UNARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
- i+=3;
- }
- else if (string.Mid(i,2)=="==")
- {
- COperator* op = new COperator;
- op->m_nLevel=3+level;
- op->m_strOperator = "==";
- op->m_nStartIndex = i;
- op->m_nType = BINARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');
- i+=2;
- }
- else if (string.Mid(i,2)==">=")
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = ">=";
- op->m_nStartIndex = i;
- op->m_nType = BINARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');
- i+=2;
- }
- else if (string.Mid(i,2)=="<=")
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "<=";
- op->m_nStartIndex = i;
- op->m_nType = BINARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');
- i+=2;
- }
- else if (string.Mid(i,2)=="!=")//modify by 2012.06.01 <>
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "!=";
- op->m_nStartIndex = i;
- op->m_nType = BINARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');
- i+=2;
- }
- else if (string[i]=='>')
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = ">";
- op->m_nStartIndex = i;
- op->m_nType = BINARY;
- Operator.push(op);
- string.SetAt(i,'@');
- i++;
- }
- else if (string[i]=='<')
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "<";
- op->m_nStartIndex = i;
- op->m_nType = BINARY;
- Operator.push(op);
- string.SetAt(i,'@');
- i++;
- }
- else if (string.Mid(i,3)=="NOT")
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "NOT";
- op->m_nStartIndex = i;
- op->m_nType = UNARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
- i+=3;
- }
- else if (string.Mid(i,3)=="AND")
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "AND";
- op->m_nStartIndex = i;
- op->m_nType = BINARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
- i+=3;
- }
- else if (string.Mid(i,2)=="OR")
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "OR";
- op->m_nStartIndex = i;
- op->m_nType = BINARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');
- i+=2;
- }
- else if (string.Mid(i,3)=="XOR")
- {
- COperator* op = new COperator;
- op->m_nLevel = 3+level;
- op->m_strOperator = "XOR";
- op->m_nStartIndex = i;
- op->m_nType = BINARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
- i+=3;
- }
- else
- {
- i++;
- }
- }
- return bRet;
- }
- double CRealComputer::computing(const COperator*op,const COperand*oprand)
- {
- double x = atof(oprand->m_strName);
- if (op->m_strOperator=="-")
- {
- x = -x;
- }
- else if (op->m_strOperator=="SQRT")
- {
- if (x>=0)
- {
- x = sqrt(x);
- }
- else
- {
- x = BIGNUMBER;
- }
- }
- else if (op->m_strOperator=="SIN")
- {
- x = sin(x);
- }
- else if (op->m_strOperator=="COS")
- {
- x = cos(x);
- }
- else if (op->m_strOperator=="TAN")
- {
- x = tan(x);
- }
- else if (op->m_strOperator=="COT")
- {
- if (fabs(sin(x))>DERROR)
- {
- x = cos(x)/sin(x);
- }
- else
- {
- x = BIGNUMBER;
- }
- }
- else if (op->m_strOperator=="NOT")
- {
- if (x!=0)
- {
- x = 0;
- }
- else
- {
- x = 1;
- }
- }
- else if (op->m_strOperator=="RND")
- {
- x = (int)x;
- }
- return x;
- }
- double CRealComputer::computing(const COperator*op,const COperand*Loprand,const COperand*Roprand)
- {
- double x = BIGNUMBER;
- switch (op->m_strOperator[0])
- {
- case '+':
- x = atof(Loprand->m_strName)+atof(Roprand->m_strName);
- break;
- case '-':
- x = atof(Loprand->m_strName)-atof(Roprand->m_strName);
- break;
- case '*':
- x = atof(Loprand->m_strName)*atof(Roprand->m_strName);
- break;
- case '/':
- {
- double y = atof(Roprand->m_strName);
- x = atof(Loprand->m_strName);
- if (fabs(y)>DERROR)
- {
- x = x/y;
- }
- else
- {
- x = BIGNUMBER;
- }
- }
- break;
- case '^':
- {
- double y = atof(Roprand->m_strName);
- x = atof(Loprand->m_strName);
- x = pow(x,y);
- }
- break;
- }
- if (op->m_strOperator=="==")
- {
- if (MyRound(atof(Loprand->m_strName)) == MyRound(atof(Roprand->m_strName)) )
- {
- x = 1;
- }
- else
- {
- x = 0;
- }
- }
- else if (op->m_strOperator==">=")
- {
- if (atof(Loprand->m_strName)>=atof(Roprand->m_strName))
- {
- x = 1;
- }
- else
- {
- x = 0;
- }
- }
- else if (op->m_strOperator=="<=")
- {
- if (atof(Loprand->m_strName)<=atof(Roprand->m_strName))
- {
- x = 1;
- }
- else
- {
- x = 0;
- }
- }
- else if (op->m_strOperator=="!=")
- {
- if (atof(Loprand->m_strName)!=atof(Roprand->m_strName))
- {
- x = 1;
- }
- else
- {
- x = 0;
- }
- }
- else if (op->m_strOperator==">")
- {
- if (atof(Loprand->m_strName)>atof(Roprand->m_strName))
- {
- x = 1;
- }
- else
- {
- x = 0;
- }
- }
- else if (op->m_strOperator=="<")
- {
- if (atof(Loprand->m_strName)<atof(Roprand->m_strName))
- {
- x = 1;
- }
- else
- {
- x = 0;
- }
- }
- else if (op->m_strOperator=="AND")
- {
- if (MyRound(atof(Loprand->m_strName)) && MyRound(atof(Roprand->m_strName)))
- {
- x = 1;
- }
- else
- {
- x = 0;
- }
- }
- else if (op->m_strOperator=="OR")
- {
- if (MyRound(atof(Loprand->m_strName)) || MyRound(atof(Roprand->m_strName)))
- {
- x = 1;
- }
- else
- {
- x = 0;
- }
- }
- else if(op->m_strOperator=="XOR")
- {
- if (MyRound(atof(Loprand->m_strName))&&MyRound(atof(Roprand->m_strName)))
- {
- x = 0;
- }
- else if ((!MyRound(atof(Loprand->m_strName))) && (!MyRound(atof(Roprand->m_strName))))
- {
- x = 0;
- }
- else
- {
- x = 1;
- }
- }
- else if (op->m_strOperator=="MOD")
- {
- x =MyRound(atof(Loprand->m_strName)) % MyRound(atof(Roprand->m_strName));
- }
- return x;
- }
- int CRealComputer::IsFormula()
- {
- stack<char> charStack;
- int i,num=m_strFormula.GetLength();
- char notChar[] = {';','\'',':',' ','[',']','{','}','\\','|',',','&','%','$','@','#','`','~','?'};//'!', modify by 2012.06.01
- int notNum = sizeof notChar;
- for (i=0;i<num;i++)
- {
- for (int j=0;j<notNum;j++)
- {
- if (m_strFormula[i]==notChar[j])
- {
- CString s;
- s.Format("The %dth character,\"%c\" is invalidated!",i+1,m_strFormula[i]);
- return -i-1;//the ith symbol is invalidating.
- }
- }
- }
- for (i=0;i<num;i++)
- {
- if (m_strFormula[i]=='(')
- {
- charStack.push('(');
- }
- if (m_strFormula[i]==')')
- {
- if (charStack.empty())
- {
- return 1;//right bracket if more.
- }
- else
- {
- charStack.pop();
- }
- }
- }
- if (!charStack.empty())
- {
- return 2; //left bracket is more.
- }
- return 0;
- }
- double CRealComputer::GetResult()
- {
- double data[100] = {0.0};
- for (int i=0;i<m_nOprandANum;i++)
- {
- float tmp = 0.0;//pVariantsManager->GetAnalogValue(m_pOprandA[i].m_strName);
- //if( -1 != m_formula.Find("Caross_1_StartORShutStatus") )
- // LOG4C((LOG_NOTICE, "m_strDynShow = < %s >,m_oprandA[i].m_name=%s, m_oprandANum = %d",m_formula,m_oprandA[i].m_name,m_oprandANum ));
- if ( (int)tmp!=VARIANT_NOTFOUND )
- {
- data[i] = tmp;
- }
- else
- {
-
- }
- }
- double result = computer(data, m_nOprandANum);
- return result;
- }
|