1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408 |
- // 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*formula)
- :m_oprandNum(0),m_oprandANum(0),m_operatorNum(0)
- {
- m_operator = 0;
- m_oprand = 0;
- m_oprandA = 0;
- if (formula)
- {
- m_formula = formula;
- Initialize();
- }
- }
- CComputer::~CComputer()
- {
- if (m_operator)
- {
- delete[]m_operator;
- }
- if(m_oprand)
- {
- delete[]m_oprand;
- }
- if(m_oprandA)
- {
- delete[]m_oprandA;
- }
- }
- void CComputer::SetFormula(CString formula)
- {
- if (!formula)
- {
- return;
- }
- m_formula = formula;
- if (m_operator)
- {
- delete[]m_operator;
- m_operator = 0;
- }
- if (m_oprand)
- {
- delete[]m_oprand;
- m_oprand = 0;
- }
- if (m_oprandA)
- {
- delete[]m_oprandA;
- m_oprandA = 0;
- }
- m_oprandNum = 0,m_oprandANum = 0,m_operatorNum = 0;
- Initialize();
- }
- int CComputer::IsFormula()
- {
- stack<char> charStack;
- int i,num = m_formula.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_formula[i]==notChar[j])
- {
- CString s;
- s.Format("The %dth character,\"%c\" is invalidated!",i+1,m_formula[i]);
- //LOG4C((LOG_NOTICE, "< %s >",s));
- return -i-1;//the ith symbol is invalidating.
- }
- }
- }
- for (i=0;i<num;i++)
- {
- if (m_formula[i]=='(')
- {
- charStack.push('(');
- }
- if (m_formula[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 string = m_formula;
- bRet = bRet&&GetOperatorStack(Operator,string);
- bRet = bRet&&GetOperandStack(Oprand,string);
- 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_oprandNum = (int)Od.size(),m_operatorNum = (int)Op.size();
- if (m_operatorNum)
- {
- m_operator = new COperator[m_operatorNum];
- }
- if (m_oprandNum)
- {
- m_oprand = new COperand[m_oprandNum];
- }
- int i;
- for (i=0;i<m_operatorNum;i++)
- {
- COperator*op = Op.top();
- m_operator[m_operatorNum-i-1] = *op;
- Op.pop();
- delete op;
- }
- for (i=0;i<m_oprandNum;i++)
- {
- COperand*od = Od.top();
- m_oprand[m_oprandNum-i-1] = *od;
- Od.pop();
- delete od;
- }
- m_oprandA = new COperand[m_oprandNum];
- BOOL IsIn = FALSE;
- for (i=0;i<m_oprandNum;i++)
- {
- for (int j=0;j<m_oprandANum;j++)
- {
- if (m_oprand[i]==m_oprandA[j])
- {
- IsIn = TRUE;
- break;
- }
- }
- if (!IsIn&&!m_oprand[i].m_IsConst)
- {
- m_oprandA[m_oprandANum] = m_oprand[i];
- m_oprandANum++;
- }
- 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_name);
- 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_level>op->m_level))
- {
- tmpOO.push(op);
- if (op->m_type==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_type==UNARY)
- {
- if (Oprand.empty())
- {
- return BIGNUMBER;
- }
- oprand = Oprand.top();
- double x = computing(op,oprand);
- oprand->m_name.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_name.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_oprandANum)
- {
- //LOG4C((LOG_NOTICE, "computer error"));
- return BIGNUMBER;
- }
- OperatorStack Operator;
- OperandStack Oprand;
- for (i=0;i<m_operatorNum;i++)
- {
- COperator*op = new COperator;
- *op = m_operator[m_operatorNum-i-1];
- Operator.push(op);
- }
- for (i=0;i<m_oprandNum;i++)
- {
- COperand*od = new COperand;
- *od = m_oprand[m_oprandNum-i-1];
- for(int j=0;j<m_oprandANum;j++)
- {
- if (*od==m_oprandA[j])
- {
- od->m_name.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_level = level;
- op->m_operator = string.Mid(i,1);
- op->m_startIndex = i;
- if(i==0||string[i-1]=='#')
- {
- op->m_type = UNARY;
- }
- else
- {
- op->m_type = BINARY;
- }
- Operator.push(op);
- string.SetAt(i,'@');
- i++;
- }
- else if (string[i]=='*'||string[i]=='/')
- {
- COperator* op = new COperator;
- op->m_level = level+1;
- op->m_operator = string.Mid(i,1);
- op->m_startIndex = i;
- op->m_type = BINARY;
- Operator.push(op);
- string.SetAt(i,'@');
- i++;
- }
- else if (string[i]=='^')
- {
- COperator* op=new COperator;
- op->m_level = level+2;
- op->m_operator = string.Mid(i,1);
- op->m_startIndex = i;
- op->m_type = BINARY;
- Operator.push(op);
- string.SetAt(i,'@');
- i++;
- }
- else if (string.Mid(i,4)=="SQRT")
- {
- COperator* op=new COperator;
- op->m_level = 3+level;
- op->m_operator = "SQRT";
- op->m_startIndex = i;
- op->m_type = 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_level = 3+level;
- op->m_operator = "LOG";
- op->m_startIndex = i;
- op->m_type = 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_level = 3+level;
- op->m_operator = "SIN";
- op->m_startIndex = i;
- op->m_type = 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_level = 3+level;
- op->m_operator = "COS";
- op->m_startIndex = i;
- op->m_type = 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_level = 3+level;
- op->m_operator = "TAN";
- op->m_startIndex = i;
- op->m_type = 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_level = 3+level;
- op->m_operator = "COT";
- op->m_startIndex = i;
- op->m_type = 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_level = 3+level;
- op->m_operator = "ASIN";
- op->m_startIndex = i;
- op->m_type = 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_level = 3+level;
- op->m_operator = "ACOS";
- op->m_startIndex = i;
- op->m_type = 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_level = 3+level;
- op->m_operator = "ATAN";
- op->m_startIndex = i;
- op->m_type = 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_IsConst = TRUE;
- }
- oprand->m_startIndex = 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_IsConst&&!isdigit(string[i])&&(string[i]!='.'))
- {
- CString s;
- s.Format("Variant cann't start with digital (%d in expression)!",i+1-oprand->m_name.GetLength());
- if( oprand )
- delete oprand;
- return FALSE;
- }
- oprand->m_name+=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_name);
- if (op->m_operator=="-")
- {
- x = -x;
- }
- else if (op->m_operator=="LOG")
- {
- if(x>0)
- {
- x = log(x);
- }
- else
- {
- x = BIGNUMBER;
- }
- }
- else if (op->m_operator=="SQRT")
- {
- if ( x>=0 )
- {
- x = sqrt(x);
- }
- else
- {
- x = BIGNUMBER;
- }
- }
- else if (op->m_operator=="SIN")
- {
- x = sin(x);
- }
- else if (op->m_operator=="COS")
- {
- x = cos(x);
- }
- else if (op->m_operator=="TAN")
- {
- x = tan(x);
- }
- else if (op->m_operator=="COT")
- {
- if(fabs(sin(x))>DERROR)
- {
- x = cos(x)/sin(x);
- }
- else
- {
- x = BIGNUMBER;
- }
- }
- else if (op->m_operator=="ASIN")
- {
- x = asin(x);
- }
- else if (op->m_operator=="ACOS")
- {
- x = acos(x);
- }
- else if (op->m_operator=="ATAN")
- {
- x = atan(x);
- }
- return x;
- }
- double CComputer::computing(const COperator*op,const COperand*Loprand,const COperand*Roprand)
- {
- double x = BIGNUMBER;
- switch(op->m_operator[0])
- {
- case '+':
- x = atof(Loprand->m_name)+atof(Roprand->m_name);
- break;
- case '-':
- x = atof(Loprand->m_name)-atof(Roprand->m_name);
- break;
- case '*':
- x = atof(Loprand->m_name)*atof(Roprand->m_name);
- break;
- case '/':
- {
- double y = atof(Roprand->m_name);
- x = atof(Loprand->m_name);
- if (fabs(y)>DERROR)
- {
- x = x/y;
- }
- else
- {
- x = BIGNUMBER;
- }
- }
- break;
- case '^':
- {
- double y = atof(Roprand->m_name);
- x = atof(Loprand->m_name);
- x = pow(x,y);
- }
- break;
- }
- return x;
- }
- CString CComputer::GetDigitalString(double* variantValue,int num)
- {
- CString value=m_formula;
- if (num>=m_oprandANum)
- {
- int i,j;
- CString* ppchar;
- ppchar=new CString[m_oprandNum+1];
- for (i=0;i<=m_oprandNum;i++)
- {
- if (i==0)
- {
- ppchar[0]=m_formula.Left(m_oprand[0].m_startIndex);
- }
- else if (i<m_oprandNum)
- {
- WORD start=m_oprand[i-1].m_startIndex+m_oprand[i-1].m_name.GetLength();
- WORD length=m_oprand[i].m_startIndex-start;
- ppchar[i]=m_formula.Mid(start,length);
- }
- else
- {
- WORD start=m_oprand[i-1].m_startIndex+m_oprand[i-1].m_name.GetLength();
- ppchar[i]=m_formula.Mid(start);
- }
- }
- CString* digitchar = new CString[m_oprandNum];
- for (i=0;i<m_oprandNum;i++)
- {
- if (m_oprand[i].m_IsConst)
- {
- digitchar[i]=m_oprand[i].m_name;
- }
- else
- {
- for (j=0;j<m_oprandANum;j++)
- {
- if (m_oprand[i]==m_oprandA[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_oprandNum;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_oprandA;
- }
- COperator::COperator():m_startIndex(0),m_level(0),m_type(BINARY)
- {
- }
- COperator::~COperator()
- {
- }
- COperand::COperand():m_startIndex(0)
- {
- m_IsConst = FALSE;
- }
- COperand::~COperand()
- {
- }
- BOOL COperand::operator ==(const COperand & od)
- {
- BOOL bRet=TRUE;
- bRet = (m_IsConst==od.m_IsConst);
- bRet = (bRet&&(m_name==od.m_name));
- return bRet;
- }
- void CComputer::Destroy()
- {
- if (m_operator)
- {
- delete[]m_operator;
- m_operator = 0;
- }
- if (m_oprand)
- {
- delete[]m_oprand;
- m_oprand = 0 ;
- }
- if (m_oprandA)
- {
- delete[]m_oprandA;
- m_oprandA = 0;
- }
- m_oprandNum=0;
- m_oprandANum=0;
- m_operatorNum=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_level = level;
- op->m_operator = string.Mid(i,1);
- op->m_startIndex = i;
- if(i==0||string[i-1]=='#')
- {
- op->m_type=UNARY;
- }
- else
- {
- op->m_type = BINARY;
- }
- Operator.push(op);
- string.SetAt(i,'@');
- i++;
- }
- else if (string[i]=='*'||string[i]=='/')
- {
- COperator* op = new COperator;
- op->m_level = level+1;
- op->m_operator = string.Mid(i,1);
- op->m_startIndex = i;
- op->m_type = BINARY;
- Operator.push(op);
- string.SetAt(i,'@');
- i++;
- }
- else if (string[i]=='^')
- {
- COperator* op = new COperator;
- op->m_level = level+2;
- op->m_operator = string.Mid(i,1);
- op->m_startIndex = i;
- op->m_type = BINARY;
- Operator.push(op);
- string.SetAt(i,'@');
- i++;
- }
- else if (string.Mid(i,4)=="SQRT")
- {
- COperator* op = new COperator;
- op->m_level = 3+level;
- op->m_operator = "SQRT";
- op->m_startIndex = i;
- op->m_type = 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_level = 3+level;
- op->m_operator = "LOG";
- op->m_startIndex = i;
- op->m_type = 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_level = 3+level;
- op->m_operator = "SIN";
- op->m_startIndex = i;
- op->m_type = 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_level = 3+level;
- op->m_operator = "COS";
- op->m_startIndex = i;
- op->m_type = 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_level = 3+level;
- op->m_operator = "TAN";
- op->m_startIndex = i;
- op->m_type = 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_level = 3+level;
- op->m_operator = "COT";
- op->m_startIndex = i;
- op->m_type = 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_level = 3+level;
- op->m_operator = "ASIN";
- op->m_startIndex = i;
- op->m_type = 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_level = 3+level;
- op->m_operator = "ACOS";
- op->m_startIndex = i;
- op->m_type = 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_level = 3+level;
- op->m_operator = "ATAN";
- op->m_startIndex = i;
- op->m_type = 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_level = 3+level;
- op->m_operator = "ABS";
- op->m_startIndex = i;
- op->m_type = 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_level = 3+level;
- op->m_operator = "MOD";
- op->m_startIndex = i;
- op->m_type = 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_level = 3+level;
- op->m_operator = "RND";
- op->m_startIndex = i;
- op->m_type = 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_level=3+level;
- op->m_operator = "==";
- op->m_startIndex = i;
- op->m_type = 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_level = 3+level;
- op->m_operator = ">=";
- op->m_startIndex = i;
- op->m_type = 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_level = 3+level;
- op->m_operator = "<=";
- op->m_startIndex = i;
- op->m_type = 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_level = 3+level;
- op->m_operator = "!=";
- op->m_startIndex = i;
- op->m_type = BINARY;
- Operator.push(op);
- string.SetAt(i,'@');string.SetAt(i+1,'@');
- i+=2;
- }
- else if (string[i]=='>')
- {
- COperator* op = new COperator;
- op->m_level = 3+level;
- op->m_operator = ">";
- op->m_startIndex = i;
- op->m_type = BINARY;
- Operator.push(op);
- string.SetAt(i,'@');
- i++;
- }
- else if (string[i]=='<')
- {
- COperator* op = new COperator;
- op->m_level = 3+level;
- op->m_operator = "<";
- op->m_startIndex = i;
- op->m_type = BINARY;
- Operator.push(op);
- string.SetAt(i,'@');
- i++;
- }
- else if (string.Mid(i,3)=="NOT")
- {
- COperator* op = new COperator;
- op->m_level = 3+level;
- op->m_operator = "NOT";
- op->m_startIndex = i;
- op->m_type = 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_level = 3+level;
- op->m_operator = "AND";
- op->m_startIndex = i;
- op->m_type = 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_level = 3+level;
- op->m_operator = "OR";
- op->m_startIndex = i;
- op->m_type = 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_level = 3+level;
- op->m_operator = "XOR";
- op->m_startIndex = i;
- op->m_type = 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_name);
- if (op->m_operator=="-")
- {
- x = -x;
- }
- else if (op->m_operator=="SQRT")
- {
- if (x>=0)
- {
- x = sqrt(x);
- }
- else
- {
- x = BIGNUMBER;
- }
- }
- else if (op->m_operator=="SIN")
- {
- x = sin(x);
- }
- else if (op->m_operator=="COS")
- {
- x = cos(x);
- }
- else if (op->m_operator=="TAN")
- {
- x = tan(x);
- }
- else if (op->m_operator=="COT")
- {
- if (fabs(sin(x))>DERROR)
- {
- x = cos(x)/sin(x);
- }
- else
- {
- x = BIGNUMBER;
- }
- }
- else if (op->m_operator=="NOT")
- {
- if (x!=0)
- {
- x = 0;
- }
- else
- {
- x = 1;
- }
- }
- else if (op->m_operator=="RND")
- {
- x = (int)x;
- }
- return x;
- }
- double CRealComputer::computing(const COperator*op,const COperand*Loprand,const COperand*Roprand)
- {
- double x = BIGNUMBER;
- switch (op->m_operator[0])
- {
- case '+':
- x = atof(Loprand->m_name)+atof(Roprand->m_name);
- break;
- case '-':
- x = atof(Loprand->m_name)-atof(Roprand->m_name);
- break;
- case '*':
- x = atof(Loprand->m_name)*atof(Roprand->m_name);
- break;
- case '/':
- {
- double y = atof(Roprand->m_name);
- x = atof(Loprand->m_name);
- if (fabs(y)>DERROR)
- {
- x = x/y;
- }
- else
- {
- x = BIGNUMBER;
- }
- }
- break;
- case '^':
- {
- double y = atof(Roprand->m_name);
- x = atof(Loprand->m_name);
- x = pow(x,y);
- }
- break;
- }
- if (op->m_operator=="==")
- {
- if (MyRound(atof(Loprand->m_name)) == MyRound(atof(Roprand->m_name)) )
- {
- x = 1;
- }
- else
- {
- x = 0;
- }
- }
- else if (op->m_operator==">=")
- {
- if (atof(Loprand->m_name)>=atof(Roprand->m_name))
- {
- x = 1;
- }
- else
- {
- x = 0;
- }
- }
- else if (op->m_operator=="<=")
- {
- if (atof(Loprand->m_name)<=atof(Roprand->m_name))
- {
- x = 1;
- }
- else
- {
- x = 0;
- }
- }
- else if (op->m_operator=="!=")
- {
- if (atof(Loprand->m_name)!=atof(Roprand->m_name))
- {
- x = 1;
- }
- else
- {
- x = 0;
- }
- }
- else if (op->m_operator==">")
- {
- if (atof(Loprand->m_name)>atof(Roprand->m_name))
- {
- x = 1;
- }
- else
- {
- x = 0;
- }
- }
- else if (op->m_operator=="<")
- {
- if (atof(Loprand->m_name)<atof(Roprand->m_name))
- {
- x = 1;
- }
- else
- {
- x = 0;
- }
- }
- else if (op->m_operator=="AND")
- {
- if (MyRound(atof(Loprand->m_name)) && MyRound(atof(Roprand->m_name)))
- {
- x = 1;
- }
- else
- {
- x = 0;
- }
- }
- else if (op->m_operator=="OR")
- {
- if (MyRound(atof(Loprand->m_name)) || MyRound(atof(Roprand->m_name)))
- {
- x = 1;
- }
- else
- {
- x = 0;
- }
- }
- else if(op->m_operator=="XOR")
- {
- if (MyRound(atof(Loprand->m_name))&&MyRound(atof(Roprand->m_name)))
- {
- x = 0;
- }
- else if ((!MyRound(atof(Loprand->m_name))) && (!MyRound(atof(Roprand->m_name))))
- {
- x = 0;
- }
- else
- {
- x = 1;
- }
- }
- else if (op->m_operator=="MOD")
- {
- x =MyRound(atof(Loprand->m_name)) % MyRound(atof(Roprand->m_name));
- }
- return x;
- }
- int CRealComputer::IsFormula()
- {
- stack<char> charStack;
- int i,num=m_formula.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_formula[i]==notChar[j])
- {
- CString s;
- s.Format("The %dth character,\"%c\" is invalidated!",i+1,m_formula[i]);
- return -i-1;//the ith symbol is invalidating.
- }
- }
- }
- for (i=0;i<num;i++)
- {
- if (m_formula[i]=='(')
- {
- charStack.push('(');
- }
- if (m_formula[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_oprandANum;i++)
- {
- float tmp = pVariantsManager->GetAnalogValue(m_oprandA[i].m_name);
- //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_oprandANum);
- return result;
- }
|