Computer.cpp 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  1. // StringResolution.cpp: implementation of the CComputer class.
  2. #include "stdafx.h"
  3. #include "computer.h"
  4. #include <math.h>
  5. #include "kernel.h"
  6. #ifdef _DEBUG
  7. #undef THIS_FILE
  8. static char THIS_FILE[]=__FILE__;
  9. #define new DEBUG_NEW
  10. #endif
  11. extern int MyRound(double value);
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. CComputer::CComputer(char*pszFormula)
  16. :m_nOprandNum(0),m_nOprandANum(0),m_nOperatorNum(0)
  17. {
  18. m_pOperator = NULL;
  19. m_pOprand = NULL;
  20. m_pOprandA = NULL;
  21. if (pszFormula)
  22. {
  23. m_strFormula = pszFormula;
  24. Initialize();
  25. }
  26. }
  27. CComputer::~CComputer()
  28. {
  29. if (m_pOperator)
  30. delete[]m_pOperator;
  31. m_pOperator = NULL;
  32. if(m_pOprand)
  33. delete[]m_pOprand;
  34. m_pOprand = NULL;
  35. if (m_pOprandA)
  36. delete[]m_pOprandA;
  37. m_pOprandA = NULL;
  38. }
  39. void CComputer::SetFormula(CString strFormula)
  40. {
  41. if (!strFormula)
  42. {
  43. return;
  44. }
  45. m_strFormula = strFormula;
  46. if (m_pOperator)
  47. {
  48. delete[]m_pOperator;
  49. m_pOperator = NULL;
  50. }
  51. if (m_pOprand)
  52. {
  53. delete[]m_pOprand;
  54. m_pOprand = NULL;
  55. }
  56. if (m_pOprandA)
  57. {
  58. delete[]m_pOprandA;
  59. m_pOprandA = NULL;
  60. }
  61. m_nOprandNum = 0,m_nOprandANum = 0,m_nOperatorNum = 0;
  62. Initialize();
  63. }
  64. int CComputer::IsFormula()
  65. {
  66. stack<char> charStack;
  67. int i,num = m_strFormula.GetLength();
  68. char notChar[] = {';','\'',':',' ','[',']','{','}','\\','|',',','\"','&','%','$','@','#','`','~','?'};//'!', modify by 2012.06.01
  69. int notNum = sizeof(notChar);
  70. for (i=0;i<num;i++)
  71. {
  72. for (int j=0;j<notNum;j++)
  73. {
  74. if (m_strFormula[i]==notChar[j])
  75. {
  76. CString s;
  77. s.Format("The %dth character,\"%c\" is invalidated!",i+1,m_strFormula[i]);
  78. //LOG4C((LOG_NOTICE, "< %s >",s));
  79. return -i-1;//the ith symbol is invalidating.
  80. }
  81. }
  82. }
  83. for (i=0;i<num;i++)
  84. {
  85. if (m_strFormula[i]=='(')
  86. {
  87. charStack.push('(');
  88. }
  89. if (m_strFormula[i]==')')
  90. {
  91. if (charStack.empty())
  92. {
  93. return 1;//right bracket if more.
  94. }
  95. else
  96. {
  97. charStack.pop();
  98. }
  99. }
  100. }
  101. if (!charStack.empty())
  102. {
  103. return 2;//left bracket is more.
  104. }
  105. return 0;
  106. }
  107. BOOL CComputer::GetStack(OperatorStack &Operator, OperandStack &Oprand)
  108. {
  109. if (IsFormula())
  110. {
  111. return FALSE;//illogicality return
  112. }
  113. BOOL bRet = TRUE;
  114. EmptyStack(Operator);
  115. EmptyStack(Oprand);
  116. CString str = m_strFormula;
  117. bRet = bRet&&GetOperatorStack(Operator,str);
  118. bRet = bRet&&GetOperandStack(Oprand,str);
  119. return bRet;
  120. }
  121. BOOL CComputer::Initialize()
  122. {
  123. BOOL bRet = FALSE;
  124. OperatorStack Op; OperandStack Od;
  125. bRet = GetStack(Op,Od);
  126. if (!bRet)
  127. {
  128. EmptyStack(Op);
  129. EmptyStack(Od);
  130. return FALSE;
  131. }
  132. m_nOprandNum = (int)Od.size(),m_nOperatorNum = (int)Op.size();
  133. if (m_nOperatorNum)
  134. {
  135. m_pOperator = new COperator[m_nOperatorNum];
  136. }
  137. if (m_nOprandNum)
  138. {
  139. m_pOprand = new COperand[m_nOprandNum];
  140. }
  141. int i;
  142. for (i=0;i<m_nOperatorNum;i++)
  143. {
  144. COperator*op = Op.top();
  145. m_pOperator[m_nOperatorNum-i-1] = *op;
  146. Op.pop();
  147. delete op;
  148. }
  149. for (i=0;i<m_nOprandNum;i++)
  150. {
  151. COperand*od = Od.top();
  152. m_pOprand[m_nOprandNum-i-1] = *od;
  153. Od.pop();
  154. delete od;
  155. }
  156. m_pOprandA = new COperand[m_nOprandNum];
  157. BOOL IsIn = FALSE;
  158. for (i=0;i<m_nOprandNum;i++)
  159. {
  160. for (int j=0;j<m_nOprandANum;j++)
  161. {
  162. if (m_pOprand[i]==m_pOprandA[j])
  163. {
  164. IsIn = TRUE;
  165. break;
  166. }
  167. }
  168. if (!IsIn&&!m_pOprand[i].m_bIsConst)
  169. {
  170. m_pOprandA[m_nOprandANum] = m_pOprand[i];
  171. m_nOprandANum++;
  172. }
  173. IsIn = FALSE;
  174. }
  175. return TRUE;
  176. }
  177. double CComputer::Computer(OperatorStack &Operator, OperandStack &Oprand)
  178. {
  179. double value =0.0;
  180. if (Operator.empty())
  181. {
  182. if (Oprand.size()==1)
  183. {
  184. COperand* od = Oprand.top();
  185. value = atof(od->m_strName);
  186. delete od;
  187. return value;
  188. }
  189. else
  190. {
  191. while (!Oprand.empty())
  192. {
  193. COperand*od = Oprand.top();
  194. Oprand.pop();
  195. delete od;
  196. }
  197. return BIGNUMBER;
  198. }
  199. }
  200. OperatorStack tmpOO;
  201. OperandStack tmpOD;
  202. COperator* op = 0,*op1 = 0;
  203. COperand *oprand = 0,*oprand1 = 0;
  204. op = Operator.top();
  205. Operator.pop();
  206. if (!Operator.empty())
  207. {
  208. op1 = Operator.top();
  209. }
  210. while (op1&&(op1->m_nLevel > op->m_nLevel))
  211. {
  212. tmpOO.push(op);
  213. if (op->m_nType==BINARY)
  214. {
  215. if (!Oprand.empty())
  216. {
  217. oprand = Oprand.top();
  218. Oprand.pop();
  219. tmpOD.push(oprand);
  220. }
  221. }
  222. op = op1;
  223. Operator.pop();
  224. if (!Operator.empty())
  225. {
  226. op1 = Operator.top();
  227. }
  228. else
  229. {
  230. op1 = 0;
  231. }
  232. }
  233. if (op->m_nType==UNARY)
  234. {
  235. if (Oprand.empty())
  236. {
  237. return BIGNUMBER;
  238. }
  239. oprand = Oprand.top();
  240. double x = computing(op,oprand);
  241. oprand->m_strName.Format("%g",x);
  242. }
  243. else
  244. {
  245. if (Oprand.empty())
  246. {
  247. return BIGNUMBER;
  248. }
  249. oprand1 = Oprand.top();
  250. Oprand.pop();
  251. if (Oprand.empty())
  252. {
  253. return BIGNUMBER;
  254. }
  255. oprand = Oprand.top();
  256. double x = computing(op,oprand1,oprand);
  257. oprand->m_strName.Format("%g",x);
  258. delete oprand1;
  259. }
  260. delete op;
  261. while (!tmpOO.empty())
  262. {
  263. op = tmpOO.top();
  264. tmpOO.pop();
  265. Operator.push(op);
  266. }
  267. while (!tmpOD.empty())
  268. {
  269. oprand = tmpOD.top();
  270. tmpOD.pop();
  271. Oprand.push(oprand);
  272. }
  273. return Computer(Operator,Oprand);
  274. }
  275. double CComputer::computer(double variantValue[], int num)
  276. {
  277. double value = 0.0;
  278. int i;
  279. if (num<m_nOprandANum)
  280. {
  281. //LOG4C((LOG_NOTICE, "computer error"));
  282. return BIGNUMBER;
  283. }
  284. OperatorStack Operator;
  285. OperandStack Oprand;
  286. for (i=0;i<m_nOperatorNum;i++)
  287. {
  288. COperator*op = new COperator;
  289. *op = m_pOperator[m_nOperatorNum-i-1];
  290. Operator.push(op);
  291. }
  292. for (i=0;i<m_nOprandNum;i++)
  293. {
  294. COperand*od = new COperand;
  295. *od = m_pOprand[m_nOprandNum-i-1];
  296. for(int j=0;j<m_nOprandANum;j++)
  297. {
  298. if (*od==m_pOprandA[j])
  299. {
  300. od->m_strName.Format("%g",variantValue[j]);
  301. }
  302. }
  303. Oprand.push(od);
  304. }
  305. value = Computer(Operator,Oprand);
  306. return value;
  307. }
  308. CString CComputer::GetErrorInformation()
  309. {
  310. CString value;
  311. return value;
  312. }
  313. BOOL CComputer::EmptyStack(OperatorStack Op)
  314. {
  315. while(!Op.empty())
  316. {
  317. COperator*op = Op.top();
  318. Op.pop();
  319. delete op;
  320. }
  321. return TRUE;
  322. }
  323. BOOL CComputer::EmptyStack(OperandStack Od)
  324. {
  325. while (!Od.empty())
  326. {
  327. COperand*od = Od.top();
  328. Od.pop();
  329. delete od;
  330. }
  331. return TRUE;
  332. }
  333. BOOL CComputer::GetOperatorStack(OperatorStack &Operator, CString &string)
  334. {
  335. BOOL bRet = TRUE;
  336. int num = string.GetLength();
  337. int i = 0;
  338. int level = 0;
  339. while (i<num)
  340. {
  341. if (string[i]=='(')
  342. {
  343. level+=LEVELS;
  344. string.SetAt(i,'#');
  345. i++;
  346. }
  347. else if (string[i]==')')
  348. {
  349. level-=LEVELS;
  350. string.SetAt(i,'@');
  351. i++;
  352. }
  353. else if (string[i]=='+'||string[i]=='-')
  354. {
  355. COperator* op=new COperator;
  356. op->m_nLevel = level;
  357. op->m_strOperator = string.Mid(i,1);
  358. op->m_nStartIndex = i;
  359. if(i==0||string[i-1]=='#')
  360. {
  361. op->m_nType = UNARY;
  362. }
  363. else
  364. {
  365. op->m_nType = BINARY;
  366. }
  367. Operator.push(op);
  368. string.SetAt(i,'@');
  369. i++;
  370. }
  371. else if (string[i]=='*'||string[i]=='/')
  372. {
  373. COperator* op = new COperator;
  374. op->m_nLevel = level+1;
  375. op->m_strOperator = string.Mid(i,1);
  376. op->m_nStartIndex = i;
  377. op->m_nType = BINARY;
  378. Operator.push(op);
  379. string.SetAt(i,'@');
  380. i++;
  381. }
  382. else if (string[i]=='^')
  383. {
  384. COperator* op=new COperator;
  385. op->m_nLevel = level+2;
  386. op->m_strOperator = string.Mid(i,1);
  387. op->m_nStartIndex = i;
  388. op->m_nType = BINARY;
  389. Operator.push(op);
  390. string.SetAt(i,'@');
  391. i++;
  392. }
  393. else if (string.Mid(i,4)=="SQRT")
  394. {
  395. COperator* op=new COperator;
  396. op->m_nLevel = 3+level;
  397. op->m_strOperator = "SQRT";
  398. op->m_nStartIndex = i;
  399. op->m_nType = UNARY;
  400. Operator.push(op);
  401. string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');string.SetAt(i+3,'@');
  402. i+=3;
  403. }
  404. else if (string.Mid(i,3)=="LOG")
  405. {
  406. COperator* op = new COperator;
  407. op->m_nLevel = 3+level;
  408. op->m_strOperator = "LOG";
  409. op->m_nStartIndex = i;
  410. op->m_nType = UNARY;
  411. Operator.push(op);
  412. string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
  413. i+=3;
  414. }
  415. else if (string.Mid(i,3)=="SIN")
  416. {
  417. COperator* op = new COperator;
  418. op->m_nLevel = 3+level;
  419. op->m_strOperator = "SIN";
  420. op->m_nStartIndex = i;
  421. op->m_nType = UNARY;
  422. Operator.push(op);
  423. string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
  424. i+=3;
  425. }
  426. else if (string.Mid(i,3)=="COS")
  427. {
  428. COperator* op = new COperator;
  429. op->m_nLevel = 3+level;
  430. op->m_strOperator = "COS";
  431. op->m_nStartIndex = i;
  432. op->m_nType = UNARY;
  433. Operator.push(op);
  434. string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
  435. i+=3;
  436. }
  437. else if (string.Mid(i,3)=="TAN")
  438. {
  439. COperator* op=new COperator;
  440. op->m_nLevel = 3+level;
  441. op->m_strOperator = "TAN";
  442. op->m_nStartIndex = i;
  443. op->m_nType = UNARY;
  444. Operator.push(op);
  445. string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
  446. i+=3;
  447. }
  448. else if (string.Mid(i,3)=="COT")
  449. {
  450. COperator* op = new COperator;
  451. op->m_nLevel = 3+level;
  452. op->m_strOperator = "COT";
  453. op->m_nStartIndex = i;
  454. op->m_nType = UNARY;
  455. Operator.push(op);
  456. string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
  457. i+=3;
  458. }
  459. else if (string.Mid(i,4)=="ASIN")
  460. {
  461. COperator* op = new COperator;
  462. op->m_nLevel = 3+level;
  463. op->m_strOperator = "ASIN";
  464. op->m_nStartIndex = i;
  465. op->m_nType = UNARY;
  466. Operator.push(op);
  467. string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');string.SetAt(i+3,'@');
  468. i+=3;
  469. }
  470. else if (string.Mid(i,4)=="ACOS")
  471. {
  472. COperator* op = new COperator;
  473. op->m_nLevel = 3+level;
  474. op->m_strOperator = "ACOS";
  475. op->m_nStartIndex = i;
  476. op->m_nType = UNARY;
  477. Operator.push(op);
  478. string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');string.SetAt(i+3,'@');
  479. i+=3;
  480. }
  481. else if (string.Mid(i,4)=="ATAN")
  482. {
  483. COperator* op = new COperator;
  484. op->m_nLevel = 3+level;
  485. op->m_strOperator = "ATAN";
  486. op->m_nStartIndex = i;
  487. op->m_nType = UNARY;
  488. Operator.push(op);
  489. string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');string.SetAt(i+3,'@');
  490. i+=3;
  491. }
  492. else
  493. {
  494. i++;
  495. }
  496. }
  497. return bRet;
  498. }
  499. BOOL CComputer::GetOperandStack(OperandStack &Oprand, CString &string)
  500. {
  501. int i = 0;
  502. int num = string.GetLength();
  503. while (i<num)
  504. {
  505. if (string[i]=='#'||string[i]=='@')
  506. {
  507. i++;
  508. }
  509. else
  510. {
  511. COperand * oprand=new COperand;
  512. if(isdigit(string[i]))
  513. {
  514. oprand->m_bIsConst = TRUE;
  515. }
  516. oprand->m_nStartIndex = i;
  517. BOOL HasPoint = FALSE;
  518. while (i<num&&string[i]!='#'&&string[i]!='@')
  519. {
  520. if (string[i]=='.')
  521. {
  522. if (HasPoint)
  523. {
  524. if( oprand )
  525. delete oprand;
  526. return FALSE;
  527. }
  528. else
  529. {
  530. HasPoint=TRUE;
  531. }
  532. }
  533. if (oprand->m_bIsConst&&!isdigit(string[i])&&(string[i]!='.'))
  534. {
  535. CString s;
  536. s.Format("Variant cann't start with digital (%d in expression)!",i+1-oprand->m_strName.GetLength());
  537. if( oprand )
  538. delete oprand;
  539. return FALSE;
  540. }
  541. oprand->m_strName+=string.Mid(i,1);
  542. i++;
  543. }
  544. Oprand.push(oprand);
  545. }
  546. }
  547. return TRUE;
  548. }
  549. int CComputer::GetErrorNumber()
  550. {
  551. return 0;
  552. }
  553. double CComputer::computing(const COperator*op,const COperand*oprand)
  554. {
  555. double x = atof(oprand->m_strName);
  556. if (op->m_strOperator=="-")
  557. {
  558. x = -x;
  559. }
  560. else if (op->m_strOperator=="LOG")
  561. {
  562. if(x>0)
  563. {
  564. x = log(x);
  565. }
  566. else
  567. {
  568. x = BIGNUMBER;
  569. }
  570. }
  571. else if (op->m_strOperator=="SQRT")
  572. {
  573. if ( x>=0 )
  574. {
  575. x = sqrt(x);
  576. }
  577. else
  578. {
  579. x = BIGNUMBER;
  580. }
  581. }
  582. else if (op->m_strOperator=="SIN")
  583. {
  584. x = sin(x);
  585. }
  586. else if (op->m_strOperator=="COS")
  587. {
  588. x = cos(x);
  589. }
  590. else if (op->m_strOperator=="TAN")
  591. {
  592. x = tan(x);
  593. }
  594. else if (op->m_strOperator=="COT")
  595. {
  596. if(fabs(sin(x))>DERROR)
  597. {
  598. x = cos(x)/sin(x);
  599. }
  600. else
  601. {
  602. x = BIGNUMBER;
  603. }
  604. }
  605. else if (op->m_strOperator=="ASIN")
  606. {
  607. x = asin(x);
  608. }
  609. else if (op->m_strOperator=="ACOS")
  610. {
  611. x = acos(x);
  612. }
  613. else if (op->m_strOperator=="ATAN")
  614. {
  615. x = atan(x);
  616. }
  617. return x;
  618. }
  619. double CComputer::computing(const COperator*op,const COperand*Loprand,const COperand*Roprand)
  620. {
  621. double x = BIGNUMBER;
  622. switch(op->m_strOperator[0])
  623. {
  624. case '+':
  625. x = atof(Loprand->m_strName)+atof(Roprand->m_strName);
  626. break;
  627. case '-':
  628. x = atof(Loprand->m_strName)-atof(Roprand->m_strName);
  629. break;
  630. case '*':
  631. x = atof(Loprand->m_strName)*atof(Roprand->m_strName);
  632. break;
  633. case '/':
  634. {
  635. double y = atof(Roprand->m_strName);
  636. x = atof(Loprand->m_strName);
  637. if (fabs(y)>DERROR)
  638. {
  639. x = x/y;
  640. }
  641. else
  642. {
  643. x = BIGNUMBER;
  644. }
  645. }
  646. break;
  647. case '^':
  648. {
  649. double y = atof(Roprand->m_strName);
  650. x = atof(Loprand->m_strName);
  651. x = pow(x,y);
  652. }
  653. break;
  654. }
  655. return x;
  656. }
  657. CString CComputer::GetDigitalString(double* variantValue,int num)
  658. {
  659. CString value=m_strFormula;
  660. if (num>=m_nOprandANum)
  661. {
  662. int i,j;
  663. CString* ppchar;
  664. ppchar=new CString[m_nOprandNum+1];
  665. for (i=0;i<=m_nOprandNum;i++)
  666. {
  667. if (i==0)
  668. {
  669. ppchar[0]=m_strFormula.Left(m_pOprand[0].m_nStartIndex);
  670. }
  671. else if (i<m_nOprandNum)
  672. {
  673. WORD start=m_pOprand[i-1].m_nStartIndex+m_pOprand[i-1].m_strName.GetLength();
  674. WORD length=m_pOprand[i].m_nStartIndex-start;
  675. ppchar[i]=m_strFormula.Mid(start,length);
  676. }
  677. else
  678. {
  679. WORD start=m_pOprand[i-1].m_nStartIndex+m_pOprand[i-1].m_strName.GetLength();
  680. ppchar[i]=m_strFormula.Mid(start);
  681. }
  682. }
  683. CString* digitchar = new CString[m_nOprandNum];
  684. for (i=0;i<m_nOprandNum;i++)
  685. {
  686. if (m_pOprand[i].m_bIsConst)
  687. {
  688. digitchar[i]=m_pOprand[i].m_strName;
  689. }
  690. else
  691. {
  692. for (j=0;j<m_nOprandANum;j++)
  693. {
  694. if (m_pOprand[i]==m_pOprandA[j])
  695. {
  696. if (variantValue[j]<0)
  697. {
  698. digitchar[i].Format("(%g)",variantValue[j]);
  699. }
  700. else
  701. {
  702. digitchar[i].Format("%g",variantValue[j]);
  703. }
  704. } // end if
  705. } // end for
  706. } // end else
  707. }
  708. value = ppchar[0];
  709. for (i=0;i<m_nOprandNum;i++)
  710. {
  711. value += digitchar[i]+ppchar[i+1];
  712. }
  713. delete[]ppchar;
  714. delete[]digitchar;
  715. }
  716. return value;
  717. }
  718. BOOL CComputer::ExpressionIsError()
  719. {
  720. #ifndef _ERROR_INFO_H_
  721. return 0;
  722. #else
  723. GET_EXPRESSION_ERROR
  724. #endif
  725. }
  726. const COperand* CComputer::GetVariantTable()
  727. {
  728. return m_pOprandA;
  729. }
  730. COperator::COperator():m_nStartIndex(0),m_nLevel(0),m_nType(BINARY)
  731. {
  732. }
  733. COperator::~COperator()
  734. {
  735. }
  736. COperand::COperand():m_nStartIndex(0)
  737. {
  738. m_bIsConst = FALSE;
  739. }
  740. COperand::~COperand()
  741. {
  742. }
  743. BOOL COperand::operator ==(const COperand & od)
  744. {
  745. BOOL bRet=TRUE;
  746. bRet = (m_bIsConst==od.m_bIsConst);
  747. bRet = (bRet&&(m_strName==od.m_strName));
  748. return bRet;
  749. }
  750. void CComputer::Destroy()
  751. {
  752. if (m_pOperator)
  753. {
  754. delete[]m_pOperator;
  755. m_pOperator = NULL;
  756. }
  757. if (m_pOprand)
  758. {
  759. delete[]m_pOprand;
  760. m_pOprand = NULL;
  761. }
  762. if (m_pOprandA)
  763. {
  764. delete[]m_pOprandA;
  765. m_pOprandA = NULL;
  766. }
  767. m_nOprandNum=0;
  768. m_nOprandANum=0;
  769. m_nOperatorNum=0;
  770. }
  771. BOOL CRealComputer::GetOperatorStack(OperatorStack&Operator,CString &string)
  772. {
  773. BOOL bRet = TRUE;
  774. int num = string.GetLength();
  775. int i = 0;
  776. int level = 0;
  777. while (i<num)
  778. {
  779. if (string[i]=='(')
  780. {
  781. level+=LEVELS;
  782. string.SetAt(i,'#');
  783. i++;
  784. }
  785. else if (string[i]==')')
  786. {
  787. level-=LEVELS;
  788. string.SetAt(i,'@');
  789. i++;
  790. }
  791. else if (string[i]=='+'||string[i]=='-')
  792. {
  793. COperator* op = new COperator;
  794. op->m_nLevel = level;
  795. op->m_strOperator = string.Mid(i,1);
  796. op->m_nStartIndex = i;
  797. if(i==0||string[i-1]=='#')
  798. {
  799. op->m_nType=UNARY;
  800. }
  801. else
  802. {
  803. op->m_nType = BINARY;
  804. }
  805. Operator.push(op);
  806. string.SetAt(i,'@');
  807. i++;
  808. }
  809. else if (string[i]=='*'||string[i]=='/')
  810. {
  811. COperator* op = new COperator;
  812. op->m_nLevel = level+1;
  813. op->m_strOperator = string.Mid(i,1);
  814. op->m_nStartIndex = i;
  815. op->m_nType = BINARY;
  816. Operator.push(op);
  817. string.SetAt(i,'@');
  818. i++;
  819. }
  820. else if (string[i]=='^')
  821. {
  822. COperator* op = new COperator;
  823. op->m_nLevel = level+2;
  824. op->m_strOperator = string.Mid(i,1);
  825. op->m_nStartIndex = i;
  826. op->m_nType = BINARY;
  827. Operator.push(op);
  828. string.SetAt(i,'@');
  829. i++;
  830. }
  831. else if (string.Mid(i,4)=="SQRT")
  832. {
  833. COperator* op = new COperator;
  834. op->m_nLevel = 3+level;
  835. op->m_strOperator = "SQRT";
  836. op->m_nStartIndex = i;
  837. op->m_nType = UNARY;
  838. Operator.push(op);
  839. string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');string.SetAt(i+3,'@');
  840. i+=4;
  841. }
  842. else if (string.Mid(i,3)=="LOG")
  843. {
  844. COperator* op = new COperator;
  845. op->m_nLevel = 3+level;
  846. op->m_strOperator = "LOG";
  847. op->m_nStartIndex = i;
  848. op->m_nType = UNARY;
  849. Operator.push(op);
  850. string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
  851. i+=3;
  852. }
  853. else if (string.Mid(i,3)=="SIN")
  854. {
  855. COperator* op = new COperator;
  856. op->m_nLevel = 3+level;
  857. op->m_strOperator = "SIN";
  858. op->m_nStartIndex = i;
  859. op->m_nType = UNARY;
  860. Operator.push(op);
  861. string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
  862. i+=3;
  863. }
  864. else if (string.Mid(i,3)=="COS")
  865. {
  866. COperator* op = new COperator;
  867. op->m_nLevel = 3+level;
  868. op->m_strOperator = "COS";
  869. op->m_nStartIndex = i;
  870. op->m_nType = UNARY;
  871. Operator.push(op);
  872. string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
  873. i+=3;
  874. }
  875. else if (string.Mid(i,3)=="TAN")
  876. {
  877. COperator* op = new COperator;
  878. op->m_nLevel = 3+level;
  879. op->m_strOperator = "TAN";
  880. op->m_nStartIndex = i;
  881. op->m_nType = UNARY;
  882. Operator.push(op);
  883. string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
  884. i+=3;
  885. }
  886. else if (string.Mid(i,3)=="COT")
  887. {
  888. COperator* op = new COperator;
  889. op->m_nLevel = 3+level;
  890. op->m_strOperator = "COT";
  891. op->m_nStartIndex = i;
  892. op->m_nType = UNARY;
  893. Operator.push(op);
  894. string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
  895. i+=3;
  896. }
  897. else if (string.Mid(i,4)=="ASIN")
  898. {
  899. COperator* op = new COperator;
  900. op->m_nLevel = 3+level;
  901. op->m_strOperator = "ASIN";
  902. op->m_nStartIndex = i;
  903. op->m_nType = UNARY;
  904. Operator.push(op);
  905. string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');string.SetAt(i+3,'@');
  906. i+=4;
  907. }
  908. else if (string.Mid(i,4)=="ACOS")
  909. {
  910. COperator* op = new COperator;
  911. op->m_nLevel = 3+level;
  912. op->m_strOperator = "ACOS";
  913. op->m_nStartIndex = i;
  914. op->m_nType = UNARY;
  915. Operator.push(op);
  916. string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');string.SetAt(i+3,'@');
  917. i+=4;
  918. }
  919. else if (string.Mid(i,4)=="ATAN")
  920. {
  921. COperator* op = new COperator;
  922. op->m_nLevel = 3+level;
  923. op->m_strOperator = "ATAN";
  924. op->m_nStartIndex = i;
  925. op->m_nType = UNARY;
  926. Operator.push(op);
  927. string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');string.SetAt(i+3,'@');
  928. i+=4;
  929. }
  930. else if (string.Mid(i,3)=="ABS")
  931. {
  932. COperator* op = new COperator;
  933. op->m_nLevel = 3+level;
  934. op->m_strOperator = "ABS";
  935. op->m_nStartIndex = i;
  936. op->m_nType = UNARY;
  937. Operator.push(op);
  938. string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
  939. i+=3;
  940. }
  941. else if (string.Mid(i,3)=="MOD")
  942. {
  943. COperator* op = new COperator;
  944. op->m_nLevel = 3+level;
  945. op->m_strOperator = "MOD";
  946. op->m_nStartIndex = i;
  947. op->m_nType = BINARY;
  948. Operator.push(op);
  949. string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
  950. i+=3;
  951. }
  952. else if (string.Mid(i,3)=="RND")
  953. {
  954. COperator* op = new COperator;
  955. op->m_nLevel = 3+level;
  956. op->m_strOperator = "RND";
  957. op->m_nStartIndex = i;
  958. op->m_nType = UNARY;
  959. Operator.push(op);
  960. string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
  961. i+=3;
  962. }
  963. else if (string.Mid(i,2)=="==")
  964. {
  965. COperator* op = new COperator;
  966. op->m_nLevel=3+level;
  967. op->m_strOperator = "==";
  968. op->m_nStartIndex = i;
  969. op->m_nType = BINARY;
  970. Operator.push(op);
  971. string.SetAt(i,'@');string.SetAt(i+1,'@');
  972. i+=2;
  973. }
  974. else if (string.Mid(i,2)==">=")
  975. {
  976. COperator* op = new COperator;
  977. op->m_nLevel = 3+level;
  978. op->m_strOperator = ">=";
  979. op->m_nStartIndex = i;
  980. op->m_nType = BINARY;
  981. Operator.push(op);
  982. string.SetAt(i,'@');string.SetAt(i+1,'@');
  983. i+=2;
  984. }
  985. else if (string.Mid(i,2)=="<=")
  986. {
  987. COperator* op = new COperator;
  988. op->m_nLevel = 3+level;
  989. op->m_strOperator = "<=";
  990. op->m_nStartIndex = i;
  991. op->m_nType = BINARY;
  992. Operator.push(op);
  993. string.SetAt(i,'@');string.SetAt(i+1,'@');
  994. i+=2;
  995. }
  996. else if (string.Mid(i,2)=="!=")//modify by 2012.06.01 <>
  997. {
  998. COperator* op = new COperator;
  999. op->m_nLevel = 3+level;
  1000. op->m_strOperator = "!=";
  1001. op->m_nStartIndex = i;
  1002. op->m_nType = BINARY;
  1003. Operator.push(op);
  1004. string.SetAt(i,'@');string.SetAt(i+1,'@');
  1005. i+=2;
  1006. }
  1007. else if (string[i]=='>')
  1008. {
  1009. COperator* op = new COperator;
  1010. op->m_nLevel = 3+level;
  1011. op->m_strOperator = ">";
  1012. op->m_nStartIndex = i;
  1013. op->m_nType = BINARY;
  1014. Operator.push(op);
  1015. string.SetAt(i,'@');
  1016. i++;
  1017. }
  1018. else if (string[i]=='<')
  1019. {
  1020. COperator* op = new COperator;
  1021. op->m_nLevel = 3+level;
  1022. op->m_strOperator = "<";
  1023. op->m_nStartIndex = i;
  1024. op->m_nType = BINARY;
  1025. Operator.push(op);
  1026. string.SetAt(i,'@');
  1027. i++;
  1028. }
  1029. else if (string.Mid(i,3)=="NOT")
  1030. {
  1031. COperator* op = new COperator;
  1032. op->m_nLevel = 3+level;
  1033. op->m_strOperator = "NOT";
  1034. op->m_nStartIndex = i;
  1035. op->m_nType = UNARY;
  1036. Operator.push(op);
  1037. string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
  1038. i+=3;
  1039. }
  1040. else if (string.Mid(i,3)=="AND")
  1041. {
  1042. COperator* op = new COperator;
  1043. op->m_nLevel = 3+level;
  1044. op->m_strOperator = "AND";
  1045. op->m_nStartIndex = i;
  1046. op->m_nType = BINARY;
  1047. Operator.push(op);
  1048. string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
  1049. i+=3;
  1050. }
  1051. else if (string.Mid(i,2)=="OR")
  1052. {
  1053. COperator* op = new COperator;
  1054. op->m_nLevel = 3+level;
  1055. op->m_strOperator = "OR";
  1056. op->m_nStartIndex = i;
  1057. op->m_nType = BINARY;
  1058. Operator.push(op);
  1059. string.SetAt(i,'@');string.SetAt(i+1,'@');
  1060. i+=2;
  1061. }
  1062. else if (string.Mid(i,3)=="XOR")
  1063. {
  1064. COperator* op = new COperator;
  1065. op->m_nLevel = 3+level;
  1066. op->m_strOperator = "XOR";
  1067. op->m_nStartIndex = i;
  1068. op->m_nType = BINARY;
  1069. Operator.push(op);
  1070. string.SetAt(i,'@');string.SetAt(i+1,'@');string.SetAt(i+2,'@');
  1071. i+=3;
  1072. }
  1073. else
  1074. {
  1075. i++;
  1076. }
  1077. }
  1078. return bRet;
  1079. }
  1080. double CRealComputer::computing(const COperator*op,const COperand*oprand)
  1081. {
  1082. double x = atof(oprand->m_strName);
  1083. if (op->m_strOperator=="-")
  1084. {
  1085. x = -x;
  1086. }
  1087. else if (op->m_strOperator=="SQRT")
  1088. {
  1089. if (x>=0)
  1090. {
  1091. x = sqrt(x);
  1092. }
  1093. else
  1094. {
  1095. x = BIGNUMBER;
  1096. }
  1097. }
  1098. else if (op->m_strOperator=="SIN")
  1099. {
  1100. x = sin(x);
  1101. }
  1102. else if (op->m_strOperator=="COS")
  1103. {
  1104. x = cos(x);
  1105. }
  1106. else if (op->m_strOperator=="TAN")
  1107. {
  1108. x = tan(x);
  1109. }
  1110. else if (op->m_strOperator=="COT")
  1111. {
  1112. if (fabs(sin(x))>DERROR)
  1113. {
  1114. x = cos(x)/sin(x);
  1115. }
  1116. else
  1117. {
  1118. x = BIGNUMBER;
  1119. }
  1120. }
  1121. else if (op->m_strOperator=="NOT")
  1122. {
  1123. if (x!=0)
  1124. {
  1125. x = 0;
  1126. }
  1127. else
  1128. {
  1129. x = 1;
  1130. }
  1131. }
  1132. else if (op->m_strOperator=="RND")
  1133. {
  1134. x = (int)x;
  1135. }
  1136. return x;
  1137. }
  1138. double CRealComputer::computing(const COperator*op,const COperand*Loprand,const COperand*Roprand)
  1139. {
  1140. double x = BIGNUMBER;
  1141. switch (op->m_strOperator[0])
  1142. {
  1143. case '+':
  1144. x = atof(Loprand->m_strName)+atof(Roprand->m_strName);
  1145. break;
  1146. case '-':
  1147. x = atof(Loprand->m_strName)-atof(Roprand->m_strName);
  1148. break;
  1149. case '*':
  1150. x = atof(Loprand->m_strName)*atof(Roprand->m_strName);
  1151. break;
  1152. case '/':
  1153. {
  1154. double y = atof(Roprand->m_strName);
  1155. x = atof(Loprand->m_strName);
  1156. if (fabs(y)>DERROR)
  1157. {
  1158. x = x/y;
  1159. }
  1160. else
  1161. {
  1162. x = BIGNUMBER;
  1163. }
  1164. }
  1165. break;
  1166. case '^':
  1167. {
  1168. double y = atof(Roprand->m_strName);
  1169. x = atof(Loprand->m_strName);
  1170. x = pow(x,y);
  1171. }
  1172. break;
  1173. }
  1174. if (op->m_strOperator=="==")
  1175. {
  1176. if (MyRound(atof(Loprand->m_strName)) == MyRound(atof(Roprand->m_strName)) )
  1177. {
  1178. x = 1;
  1179. }
  1180. else
  1181. {
  1182. x = 0;
  1183. }
  1184. }
  1185. else if (op->m_strOperator==">=")
  1186. {
  1187. if (atof(Loprand->m_strName)>=atof(Roprand->m_strName))
  1188. {
  1189. x = 1;
  1190. }
  1191. else
  1192. {
  1193. x = 0;
  1194. }
  1195. }
  1196. else if (op->m_strOperator=="<=")
  1197. {
  1198. if (atof(Loprand->m_strName)<=atof(Roprand->m_strName))
  1199. {
  1200. x = 1;
  1201. }
  1202. else
  1203. {
  1204. x = 0;
  1205. }
  1206. }
  1207. else if (op->m_strOperator=="!=")
  1208. {
  1209. if (atof(Loprand->m_strName)!=atof(Roprand->m_strName))
  1210. {
  1211. x = 1;
  1212. }
  1213. else
  1214. {
  1215. x = 0;
  1216. }
  1217. }
  1218. else if (op->m_strOperator==">")
  1219. {
  1220. if (atof(Loprand->m_strName)>atof(Roprand->m_strName))
  1221. {
  1222. x = 1;
  1223. }
  1224. else
  1225. {
  1226. x = 0;
  1227. }
  1228. }
  1229. else if (op->m_strOperator=="<")
  1230. {
  1231. if (atof(Loprand->m_strName)<atof(Roprand->m_strName))
  1232. {
  1233. x = 1;
  1234. }
  1235. else
  1236. {
  1237. x = 0;
  1238. }
  1239. }
  1240. else if (op->m_strOperator=="AND")
  1241. {
  1242. if (MyRound(atof(Loprand->m_strName)) && MyRound(atof(Roprand->m_strName)))
  1243. {
  1244. x = 1;
  1245. }
  1246. else
  1247. {
  1248. x = 0;
  1249. }
  1250. }
  1251. else if (op->m_strOperator=="OR")
  1252. {
  1253. if (MyRound(atof(Loprand->m_strName)) || MyRound(atof(Roprand->m_strName)))
  1254. {
  1255. x = 1;
  1256. }
  1257. else
  1258. {
  1259. x = 0;
  1260. }
  1261. }
  1262. else if(op->m_strOperator=="XOR")
  1263. {
  1264. if (MyRound(atof(Loprand->m_strName))&&MyRound(atof(Roprand->m_strName)))
  1265. {
  1266. x = 0;
  1267. }
  1268. else if ((!MyRound(atof(Loprand->m_strName))) && (!MyRound(atof(Roprand->m_strName))))
  1269. {
  1270. x = 0;
  1271. }
  1272. else
  1273. {
  1274. x = 1;
  1275. }
  1276. }
  1277. else if (op->m_strOperator=="MOD")
  1278. {
  1279. x =MyRound(atof(Loprand->m_strName)) % MyRound(atof(Roprand->m_strName));
  1280. }
  1281. return x;
  1282. }
  1283. int CRealComputer::IsFormula()
  1284. {
  1285. stack<char> charStack;
  1286. int i,num=m_strFormula.GetLength();
  1287. char notChar[] = {';','\'',':',' ','[',']','{','}','\\','|',',','&','%','$','@','#','`','~','?'};//'!', modify by 2012.06.01
  1288. int notNum = sizeof notChar;
  1289. for (i=0;i<num;i++)
  1290. {
  1291. for (int j=0;j<notNum;j++)
  1292. {
  1293. if (m_strFormula[i]==notChar[j])
  1294. {
  1295. CString s;
  1296. s.Format("The %dth character,\"%c\" is invalidated!",i+1,m_strFormula[i]);
  1297. return -i-1;//the ith symbol is invalidating.
  1298. }
  1299. }
  1300. }
  1301. for (i=0;i<num;i++)
  1302. {
  1303. if (m_strFormula[i]=='(')
  1304. {
  1305. charStack.push('(');
  1306. }
  1307. if (m_strFormula[i]==')')
  1308. {
  1309. if (charStack.empty())
  1310. {
  1311. return 1;//right bracket if more.
  1312. }
  1313. else
  1314. {
  1315. charStack.pop();
  1316. }
  1317. }
  1318. }
  1319. if (!charStack.empty())
  1320. {
  1321. return 2; //left bracket is more.
  1322. }
  1323. return 0;
  1324. }
  1325. double CRealComputer::GetResult()
  1326. {
  1327. double data[100] = {0.0};
  1328. for (int i=0;i<m_nOprandANum;i++)
  1329. {
  1330. float tmp = 0.0;//pVariantsManager->GetAnalogValue(m_pOprandA[i].m_strName);
  1331. //if( -1 != m_formula.Find("Caross_1_StartORShutStatus") )
  1332. // LOG4C((LOG_NOTICE, "m_strDynShow = < %s >,m_oprandA[i].m_name=%s, m_oprandANum = %d",m_formula,m_oprandA[i].m_name,m_oprandANum ));
  1333. if ( (int)tmp!=VARIANT_NOTFOUND )
  1334. {
  1335. data[i] = tmp;
  1336. }
  1337. else
  1338. {
  1339. }
  1340. }
  1341. double result = computer(data, m_nOprandANum);
  1342. return result;
  1343. }