LogicExp.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. #include "stdafx.h"
  2. #include<iostream>
  3. #include<stack>
  4. #include<queue>
  5. #include<cstring>
  6. #include<vector>
  7. #include<map>
  8. #include "MathExpression.h"
  9. #include "XMLSerialization.h"
  10. #include "COTSUtilityDllFunExport.h"
  11. using namespace std;
  12. namespace expInterpreter {
  13. namespace {
  14. int CountStr(std::string T, std::string P)
  15. {
  16. int count = 0;
  17. int begin = 0;
  18. while ((begin = T.find(P, begin)) != string::npos)
  19. {
  20. count++;
  21. begin = begin + P.length();
  22. }
  23. return count;
  24. }
  25. void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c)
  26. {
  27. std::string::size_type pos1, pos2;
  28. pos2 = s.find(c);
  29. pos1 = 0;
  30. while (std::string::npos != pos2)
  31. {
  32. v.push_back(s.substr(pos1, pos2 - pos1));
  33. pos1 = pos2 + c.size();
  34. pos2 = s.find(c, pos1);
  35. }
  36. if (pos1 != s.length())
  37. v.push_back(s.substr(pos1));
  38. }
  39. void DevidExpToMathExp(std::vector<std::pair<string,string>>& mathExp, string exp)
  40. {
  41. xmls::ReplaceAll(exp, " ", "");
  42. std::vector<string> list1;
  43. SplitString(exp, list1, "and");
  44. for (auto s : list1)
  45. {
  46. if (s.find("or") != std::string::npos)
  47. {
  48. std::vector <string> list2;
  49. SplitString(s, list2, "or");
  50. for (auto s1 : list2)
  51. {
  52. std:: pair<string, string> p1;
  53. p1.first = s1;
  54. p1.second = s1;
  55. mathExp.push_back(p1);
  56. }
  57. }
  58. else
  59. {
  60. std::pair<string, string> p2;
  61. p2.first = s;
  62. p2.second = s;
  63. mathExp.push_back(p2);
  64. }
  65. }
  66. for (auto s2 = mathExp.begin(); s2 != mathExp.end(); s2++)
  67. {
  68. int startPos;
  69. int sMathLen;
  70. string sMath;
  71. int n1 = CountStr(s2->second, "(");
  72. int n2 = CountStr(s2->second, ")");
  73. if (n1 > n2)//contains unpaired "("
  74. {
  75. startPos = n1 - n2;
  76. sMathLen = s2->second.length() - startPos;
  77. sMath = s2->second.substr(startPos, sMathLen);
  78. s2->second = sMath;
  79. s2->first = sMath;
  80. }
  81. else if (n2 > n1)
  82. {
  83. startPos = n2 - n1;
  84. sMathLen = s2->second.length() - startPos;
  85. sMath = s2->second.substr(0, sMathLen);
  86. s2->second = sMath;
  87. s2->first = sMath;
  88. }
  89. }
  90. }
  91. int f(char c) // 求表达式的优先级
  92. {
  93. if (c == '(') return 4;
  94. if (c == '!') return 3;
  95. if (c == '&') return 2; // 相当于*
  96. if (c == '|') return 1; // 相当于+,优先级最低
  97. else
  98. return 0;
  99. }
  100. bool f2(char c) // 逻辑表达式数的转换
  101. {
  102. if (c == 'F')
  103. {
  104. return false; // F相当于0
  105. }
  106. else
  107. {
  108. return true; // V相当于1
  109. }
  110. }
  111. string MidToPost(const char* c) //求表达式对应的后缀表达式
  112. {
  113. stack<char> s; //字符串中去手动去空格
  114. string q = "";
  115. int n = strlen(c);
  116. for (unsigned int i = 0; i < n; i++)
  117. {
  118. if (c[i] != ' ') // 除去空格
  119. {
  120. // 如果遇到运算数,直接加入到队列中,用队列来放后缀表达式
  121. if (c[i] == 'F' || c[i] == 'V')
  122. {
  123. q += c[i];
  124. }
  125. else if (c[i] == '!' && !s.empty() && s.top() == '!')
  126. {
  127. s.pop(); // 如果遇到!而且栈顶也是!那么直接抵消出栈
  128. }
  129. else if (!s.size())
  130. {
  131. s.push(c[i]); // 如果栈为空,遇到运算符直接入栈
  132. }
  133. else if (c[i] == ')')
  134. { // 如果是右括号,则弹出对应左括号前的所有的运算符 ,加入到队列中
  135. while (s.top() != '(')
  136. {
  137. q += s.top();
  138. s.pop();
  139. }
  140. s.pop(); // 弹出左括号
  141. continue;
  142. }
  143. else if (f(s.top()) == 4 || (f(c[i]) > f(s.top())))
  144. {
  145. s.push(c[i]); // 如果栈顶是左括号,或者当前优先级高,都入栈
  146. }
  147. else if (f(s.top()) != 4 && f(c[i]) <= f(s.top()))
  148. {
  149. q += s.top();
  150. s.pop(); // 如果遇到运算符没有栈顶运算符级别高,出栈
  151. while (!s.empty() && f(s.top()) != 4 && f(c[i]) <= f(s.top()))
  152. {
  153. q += s.top(); // 从栈中弹出比当前优先级高的运算符
  154. s.pop();
  155. }
  156. s.push(c[i]); //将当前运算符加入到队列
  157. }
  158. }
  159. }
  160. while (!s.empty())
  161. {
  162. q += s.top(); // 最后将栈里面所有元素弹出加入到队列
  163. s.pop();
  164. }
  165. return q;
  166. }
  167. char GetValuePost(string q)
  168. { //后缀表达式求值
  169. bool r = true;
  170. char x, y, ans;
  171. stack<char> s;
  172. int n = q.size();
  173. for (unsigned int i = 0; i < n; i++)
  174. {
  175. if (q[i] == 'V' || q[i] == 'F')
  176. {
  177. s.push(q[i]);
  178. }
  179. else
  180. {
  181. if (q[i] == '&')
  182. {
  183. x = s.top();
  184. s.pop();
  185. y = s.top();
  186. s.pop();
  187. bool v1, v2;
  188. v1 = f2(x);
  189. v2 = f2(y);
  190. r = (v1 && v2);
  191. if (r == true)
  192. s.push('V');
  193. else
  194. s.push('F');
  195. }
  196. else if (q[i] == '|')
  197. {
  198. x = s.top();
  199. s.pop();
  200. y = s.top();
  201. s.pop();
  202. r = (f2(x) || f2(y));
  203. if (r == true)
  204. s.push('V');
  205. else
  206. s.push('F');
  207. }
  208. else
  209. {
  210. x = s.top();
  211. s.pop();
  212. if (f2(x) == 1)
  213. s.push('F');
  214. else
  215. s.push('V');
  216. }
  217. }
  218. ans = s.top();
  219. }
  220. return ans;
  221. }
  222. /* void RemoveLeftAndRightBrace(std::string& s)
  223. {
  224. }*/
  225. }
  226. /*int main1()
  227. {*/
  228. //"(Si+Al+Na)>60 and O>15 and ASPECT>10 and D_MEAN<20"
  229. //"(20+21+22)>60 and 16>15 and (5>10 and 10<20)"
  230. //"(20+21+22)>60 and 16>15 and (5>10 or 10<20)"
  231. /* std::string hybrids = "20+(5+1)*25-500>60 and (21>15 and (5>10 or 10<20))";
  232. bool v = CalcuExp(hybrids);
  233. cout << v << endl;*/
  234. /*}*/
  235. bool CalcuExp(std::string hybrids)
  236. {
  237. //hybrids = "(51.839426 + 34.298664) > 90and34.298664 > 30and51.839426 > 40and0 < 1.5and0 < 1.5and8.452528 < 3and5.409383 < 2and0 < 2and0 < 2and0 < 2and51.839426 < 60and2.809600 < 1.6";
  238. //hybrids = "(26.856649 + 23.877375 + 0.000000) > 30 and (0.000000 + 26.856649) > 5 and 23.877375 > 5 and 19.041451 < 5 and 13.989637 < 10";
  239. //hybrids = "(0 < 1or0 > 50)and0.925024 >= 15and0.925024 <= 100and48.318507 >= 20and48.318507 <= 100and0 < 5";
  240. //hybrids = "(19.445164 + 0.950784) >= 80and0.950784 / 19.445164 >= 0.4and0.950784 / 19.445164 < 6.2and0 < 3and0 < 3and0.411897 < 3and0 < 3and0 < 5and0 < 10and0 < 5and0 < 5";
  241. xmls::ReplaceAll(hybrids, " ", "");
  242. expInterpreter::Expression mathExpIpr;
  243. std::vector<std::pair<string,string>> mathexp;
  244. DevidExpToMathExp(mathexp, hybrids);//first ,we devid the expression into math expressions such as "20+(5+1)*25-500>60" ,we use "and" and "or" to separate it.
  245. for (auto e : mathexp)
  246. {
  247. string rst;
  248. string s = e.second; //every string contains one logic compare sign,such as "<" or ">" etc.
  249. /*cout << e.second << endl;*/
  250. auto r = s.find("<");// we check which sign it contains.
  251. auto r1 = s.find(">");
  252. auto r2 = s.find("=");
  253. auto r3 = s.find(">=");
  254. auto r4 = s.find("<=");
  255. if (r3 != std::string::npos)// firstly,wind look out the ">=" then "<=" then ">" and "<" "=" .the sequence is very important.
  256. {
  257. double result;
  258. vector<string> strs;
  259. SplitString(s, strs, ">=");
  260. string s1 = "";
  261. s1 = strs[0] + "-(" + strs[1] + ")";
  262. mathExpIpr.SetExprStr(s1.c_str());
  263. float rst1 = mathExpIpr.GetResult(&result);
  264. if (result >= 0)
  265. {
  266. rst = "V";
  267. }
  268. else
  269. {
  270. rst = "F";
  271. }
  272. }
  273. else if (r4 != std::string::npos)
  274. {
  275. double result;
  276. vector<string> strs;
  277. SplitString(s, strs, "<=");
  278. string s1 = "";
  279. s1 = strs[0] + "-(" + strs[1] + ")";
  280. mathExpIpr.SetExprStr(s1.c_str());
  281. int rst1 = mathExpIpr.GetResult(&result);
  282. if (result <= 0)
  283. {
  284. rst = "V";
  285. }
  286. else
  287. {
  288. rst = "F";
  289. }
  290. }else if(r != std::string::npos)//contains "<"
  291. {
  292. double result;
  293. vector<string> strs;
  294. SplitString(s, strs, "<");
  295. string s1 = strs[0] + "-(" + strs[1] + ")";//convert this exp to a menus exp and calculate its value and then compare to 0.
  296. mathExpIpr.SetExprStr(s1.c_str());
  297. float rst1 = mathExpIpr.GetResult(&result);//calculate this expression
  298. if (result < 0)
  299. {
  300. rst = "V";
  301. }
  302. else
  303. {
  304. rst = "F";
  305. }
  306. }
  307. else if (r1 != std::string::npos)
  308. {
  309. double result;
  310. vector<string> strs;
  311. SplitString(s, strs, ">");
  312. string s1 = strs[0] + "-(" + strs[1] + ")";
  313. mathExpIpr.SetExprStr(s1.c_str());
  314. float rst1 = mathExpIpr.GetResult(&result);
  315. if (result > 0)
  316. {
  317. rst = "V";
  318. }
  319. else
  320. {
  321. rst = "F";
  322. }
  323. }
  324. else if (r2 != std::string::npos)
  325. {
  326. double result;
  327. vector<string> strs;
  328. SplitString(s, strs, "=");
  329. string s1 = strs[0] + "-(" + strs[1] + ")";
  330. mathExpIpr.SetExprStr(s1.c_str());
  331. float rst1 = mathExpIpr.GetResult(&result);
  332. if (result == 0)
  333. {
  334. rst = "V";
  335. }
  336. else
  337. {
  338. rst = "F";
  339. }
  340. }
  341. e.second = rst;
  342. xmls::ReplaceFirst(hybrids, e.first, e.second);
  343. }
  344. xmls::ReplaceAll(hybrids, "and", "&");
  345. xmls::ReplaceAll(hybrids, "or", "|");//get a pure logic expression.
  346. //LogTrace(__FILE__, __LINE__, hybrids.c_str());
  347. string post;
  348. char ans;
  349. post = MidToPost(hybrids.c_str());
  350. //cout << post << endl;
  351. ans = GetValuePost(post);
  352. if (ans == 'V')
  353. {
  354. return true;
  355. }
  356. else
  357. {
  358. return false;
  359. }
  360. }
  361. }