LogicExp.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. ans = 'F';
  172. stack<char> s;
  173. int n = q.size();
  174. for (unsigned int i = 0; i < n; i++)
  175. {
  176. if (q[i] == 'V' || q[i] == 'F')
  177. {
  178. s.push(q[i]);
  179. }
  180. else
  181. {
  182. if (q[i] == '&')
  183. {
  184. x = s.top();
  185. s.pop();
  186. y = s.top();
  187. s.pop();
  188. bool v1, v2;
  189. v1 = f2(x);
  190. v2 = f2(y);
  191. r = (v1 && v2);
  192. if (r == true)
  193. s.push('V');
  194. else
  195. s.push('F');
  196. }
  197. else if (q[i] == '|')
  198. {
  199. x = s.top();
  200. s.pop();
  201. y = s.top();
  202. s.pop();
  203. r = (f2(x) || f2(y));
  204. if (r == true)
  205. s.push('V');
  206. else
  207. s.push('F');
  208. }
  209. else
  210. {
  211. x = s.top();
  212. s.pop();
  213. if (f2(x) == 1)
  214. s.push('F');
  215. else
  216. s.push('V');
  217. }
  218. }
  219. ans = s.top();
  220. }
  221. return ans;
  222. }
  223. /* void RemoveLeftAndRightBrace(std::string& s)
  224. {
  225. }*/
  226. }
  227. /*int main1()
  228. {*/
  229. //"(Si+Al+Na)>60 and O>15 and ASPECT>10 and D_MEAN<20"
  230. //"(20+21+22)>60 and 16>15 and (5>10 and 10<20)"
  231. //"(20+21+22)>60 and 16>15 and (5>10 or 10<20)"
  232. /* std::string hybrids = "20+(5+1)*25-500>60 and (21>15 and (5>10 or 10<20))";
  233. bool v = CalcuExp(hybrids);
  234. cout << v << endl;*/
  235. /*}*/
  236. bool CalcuExp(std::string hybrids)
  237. {
  238. //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";
  239. //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";
  240. //hybrids = "(0 < 1or0 > 50)and0.925024 >= 15and0.925024 <= 100and48.318507 >= 20and48.318507 <= 100and0 < 5";
  241. //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";
  242. xmls::ReplaceAll(hybrids, " ", "");
  243. expInterpreter::Expression mathExpIpr;
  244. std::vector<std::pair<string,string>> mathexp;
  245. 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.
  246. for (auto e : mathexp)
  247. {
  248. string rst;
  249. string s = e.second; //every string contains one logic compare sign,such as "<" or ">" etc.
  250. /*cout << e.second << endl;*/
  251. auto r = s.find("<");// we check which sign it contains.
  252. auto r1 = s.find(">");
  253. auto r2 = s.find("=");
  254. auto r3 = s.find(">=");
  255. auto r4 = s.find("<=");
  256. if (r3 != std::string::npos)// firstly,wind look out the ">=" then "<=" then ">" and "<" "=" .the sequence is very important.
  257. {
  258. double result;
  259. vector<string> strs;
  260. SplitString(s, strs, ">=");
  261. string s1 = "";
  262. s1 = strs[0] + "-(" + strs[1] + ")";
  263. mathExpIpr.SetExprStr(s1.c_str());
  264. float rst1 = mathExpIpr.GetResult(&result);
  265. if (result >= 0)
  266. {
  267. rst = "V";
  268. }
  269. else
  270. {
  271. rst = "F";
  272. }
  273. }
  274. else if (r4 != std::string::npos)
  275. {
  276. double result;
  277. vector<string> strs;
  278. SplitString(s, strs, "<=");
  279. string s1 = "";
  280. s1 = strs[0] + "-(" + strs[1] + ")";
  281. mathExpIpr.SetExprStr(s1.c_str());
  282. int rst1 = mathExpIpr.GetResult(&result);
  283. if (result <= 0)
  284. {
  285. rst = "V";
  286. }
  287. else
  288. {
  289. rst = "F";
  290. }
  291. }else if(r != std::string::npos)//contains "<"
  292. {
  293. double result;
  294. vector<string> strs;
  295. SplitString(s, strs, "<");
  296. string s1 = strs[0] + "-(" + strs[1] + ")";//convert this exp to a menus exp and calculate its value and then compare to 0.
  297. mathExpIpr.SetExprStr(s1.c_str());
  298. float rst1 = mathExpIpr.GetResult(&result);//calculate this expression
  299. if (result < 0)
  300. {
  301. rst = "V";
  302. }
  303. else
  304. {
  305. rst = "F";
  306. }
  307. }
  308. else if (r1 != std::string::npos)
  309. {
  310. double result;
  311. vector<string> strs;
  312. SplitString(s, strs, ">");
  313. string s1 = strs[0] + "-(" + strs[1] + ")";
  314. mathExpIpr.SetExprStr(s1.c_str());
  315. float rst1 = mathExpIpr.GetResult(&result);
  316. if (result > 0)
  317. {
  318. rst = "V";
  319. }
  320. else
  321. {
  322. rst = "F";
  323. }
  324. }
  325. else if (r2 != std::string::npos)
  326. {
  327. double result;
  328. vector<string> strs;
  329. SplitString(s, strs, "=");
  330. string s1 = strs[0] + "-(" + strs[1] + ")";
  331. mathExpIpr.SetExprStr(s1.c_str());
  332. float rst1 = mathExpIpr.GetResult(&result);
  333. if (result == 0)
  334. {
  335. rst = "V";
  336. }
  337. else
  338. {
  339. rst = "F";
  340. }
  341. }
  342. e.second = rst;
  343. xmls::ReplaceFirst(hybrids, e.first, e.second);
  344. }
  345. xmls::ReplaceAll(hybrids, "and", "&");
  346. xmls::ReplaceAll(hybrids, "or", "|");//get a pure logic expression.
  347. //LogTrace(__FILE__, __LINE__, hybrids.c_str());
  348. string post;
  349. char ans;
  350. post = MidToPost(hybrids.c_str());
  351. //cout << post << endl;
  352. ans = GetValuePost(post);
  353. if (ans == 'V')
  354. {
  355. return true;
  356. }
  357. else
  358. {
  359. return false;
  360. }
  361. }
  362. }