Computer.cpp 25 KB

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