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. using namespace std;
  11. namespace expInterpreter {
  12. namespace {
  13. int CountStr(std::string T, std::string P)
  14. {
  15. int count = 0;
  16. int begin = 0;
  17. while ((begin = T.find(P, begin)) != string::npos)
  18. {
  19. count++;
  20. begin = begin + P.length();
  21. }
  22. return count;
  23. }
  24. void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c)
  25. {
  26. std::string::size_type pos1, pos2;
  27. pos2 = s.find(c);
  28. pos1 = 0;
  29. while (std::string::npos != pos2)
  30. {
  31. v.push_back(s.substr(pos1, pos2 - pos1));
  32. pos1 = pos2 + c.size();
  33. pos2 = s.find(c, pos1);
  34. }
  35. if (pos1 != s.length())
  36. v.push_back(s.substr(pos1));
  37. }
  38. void DevidExpToMathExp(std::vector<std::pair<string,string>>& mathExp, string exp)
  39. {
  40. xmls::ReplaceAll(exp, " ", "");
  41. std::vector<string> list1;
  42. SplitString(exp, list1, "and");
  43. for (auto s : list1)
  44. {
  45. if (s.find("or") != std::string::npos)
  46. {
  47. std::vector <string> list2;
  48. SplitString(s, list2, "or");
  49. for (auto s1 : list2)
  50. {
  51. std:: pair<string, string> p1;
  52. p1.first = s1;
  53. p1.second = s1;
  54. mathExp.push_back(p1);
  55. }
  56. }
  57. else
  58. {
  59. std::pair<string, string> p2;
  60. p2.first = s;
  61. p2.second = s;
  62. mathExp.push_back(p2);
  63. }
  64. }
  65. for (auto s2 = mathExp.begin(); s2 != mathExp.end(); s2++)
  66. {
  67. int startPos;
  68. int sMathLen;
  69. string sMath;
  70. int n1 = CountStr(s2->second, "(");
  71. int n2 = CountStr(s2->second, ")");
  72. if (n1 > n2)//contains unpaired "("
  73. {
  74. startPos = n1 - n2;
  75. sMathLen = s2->second.length() - startPos;
  76. sMath = s2->second.substr(startPos, sMathLen);
  77. s2->second = sMath;
  78. s2->first = sMath;
  79. }
  80. else if (n2 > n1)
  81. {
  82. startPos = n2 - n1;
  83. sMathLen = s2->second.length() - startPos;
  84. sMath = s2->second.substr(0, sMathLen);
  85. s2->second = sMath;
  86. s2->first = sMath;
  87. }
  88. }
  89. }
  90. int f(char c) // 求表达式的优先级
  91. {
  92. if (c == '(') return 4;
  93. if (c == '!') return 3;
  94. if (c == '&') return 2; // 相当于*
  95. if (c == '|') return 1; // 相当于+,优先级最低
  96. else
  97. return 0;
  98. }
  99. bool f2(char c) // 逻辑表达式数的转换
  100. {
  101. if (c == 'F')
  102. {
  103. return false; // F相当于0
  104. }
  105. else
  106. {
  107. return true; // V相当于1
  108. }
  109. }
  110. string MidToPost(const char* c) //求表达式对应的后缀表达式
  111. {
  112. stack<char> s; //字符串中去手动去空格
  113. string q = "";
  114. int n = strlen(c);
  115. for (unsigned int i = 0; i < n; i++)
  116. {
  117. if (c[i] != ' ') // 除去空格
  118. {
  119. // 如果遇到运算数,直接加入到队列中,用队列来放后缀表达式
  120. if (c[i] == 'F' || c[i] == 'V')
  121. {
  122. q += c[i];
  123. }
  124. else if (c[i] == '!' && !s.empty() && s.top() == '!')
  125. {
  126. s.pop(); // 如果遇到!而且栈顶也是!那么直接抵消出栈
  127. }
  128. else if (!s.size())
  129. {
  130. s.push(c[i]); // 如果栈为空,遇到运算符直接入栈
  131. }
  132. else if (c[i] == ')')
  133. { // 如果是右括号,则弹出对应左括号前的所有的运算符 ,加入到队列中
  134. while (s.top() != '(')
  135. {
  136. q += s.top();
  137. s.pop();
  138. }
  139. s.pop(); // 弹出左括号
  140. continue;
  141. }
  142. else if (f(s.top()) == 4 || (f(c[i]) > f(s.top())))
  143. {
  144. s.push(c[i]); // 如果栈顶是左括号,或者当前优先级高,都入栈
  145. }
  146. else if (f(s.top()) != 4 && f(c[i]) <= f(s.top()))
  147. {
  148. q += s.top();
  149. s.pop(); // 如果遇到运算符没有栈顶运算符级别高,出栈
  150. while (!s.empty() && f(s.top()) != 4 && f(c[i]) <= f(s.top()))
  151. {
  152. q += s.top(); // 从栈中弹出比当前优先级高的运算符
  153. s.pop();
  154. }
  155. s.push(c[i]); //将当前运算符加入到队列
  156. }
  157. }
  158. }
  159. while (!s.empty())
  160. {
  161. q += s.top(); // 最后将栈里面所有元素弹出加入到队列
  162. s.pop();
  163. }
  164. return q;
  165. }
  166. char GetValuePost(string q)
  167. { //后缀表达式求值
  168. bool r = true;
  169. char x, y, ans;
  170. stack<char> s;
  171. int n = q.size();
  172. for (unsigned int i = 0; i < n; i++)
  173. {
  174. if (q[i] == 'V' || q[i] == 'F')
  175. {
  176. s.push(q[i]);
  177. }
  178. else
  179. {
  180. if (q[i] == '&')
  181. {
  182. x = s.top();
  183. s.pop();
  184. y = s.top();
  185. s.pop();
  186. bool v1, v2;
  187. v1 = f2(x);
  188. v2 = f2(y);
  189. r = (v1 && v2);
  190. if (r == true)
  191. s.push('V');
  192. else
  193. s.push('F');
  194. }
  195. else if (q[i] == '|')
  196. {
  197. x = s.top();
  198. s.pop();
  199. y = s.top();
  200. s.pop();
  201. r = (f2(x) || f2(y));
  202. if (r == true)
  203. s.push('V');
  204. else
  205. s.push('F');
  206. }
  207. else
  208. {
  209. x = s.top();
  210. s.pop();
  211. if (f2(x) == 1)
  212. s.push('F');
  213. else
  214. s.push('V');
  215. }
  216. }
  217. ans = s.top();
  218. }
  219. return ans;
  220. }
  221. /* void RemoveLeftAndRightBrace(std::string& s)
  222. {
  223. }*/
  224. }
  225. /*int main1()
  226. {*/
  227. //"(Si+Al+Na)>60 and O>15 and ASPECT>10 and D_MEAN<20"
  228. //"(20+21+22)>60 and 16>15 and (5>10 and 10<20)"
  229. //"(20+21+22)>60 and 16>15 and (5>10 or 10<20)"
  230. /* std::string hybrids = "20+(5+1)*25-500>60 and (21>15 and (5>10 or 10<20))";
  231. bool v = CalcuExp(hybrids);
  232. cout << v << endl;*/
  233. /*}*/
  234. bool CalcuExp(std::string hybrids)
  235. {
  236. //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";
  237. //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";
  238. //hybrids = "(0 < 1or0 > 50)and0.925024 >= 15and0.925024 <= 100and48.318507 >= 20and48.318507 <= 100and0 < 5";
  239. //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";
  240. xmls::ReplaceAll(hybrids, " ", "");
  241. expInterpreter::Expression mathExpIpr;
  242. std::vector<std::pair<string,string>> mathexp;
  243. 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.
  244. //
  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. string post;
  347. char ans;
  348. post = MidToPost(hybrids.c_str());
  349. //cout << post << endl;
  350. ans = GetValuePost(post);
  351. if (ans == 'V')
  352. {
  353. return true;
  354. }
  355. else
  356. {
  357. return false;
  358. }
  359. }
  360. }