当前位置:   article > 正文

四则表达式计算

四则表达式计算

四则运算表达式的计算步骤:
(1)利用栈模型将四则表达式转换为后缀表达式;
(2)利用栈模型对后缀表达式进行计算;
需要注意的点:(1)3+2*{1+2*[-4/(8-6)+7]}  这里有一个-4需要处理
                         (2)5-3+9*6*(6-10-2)     中缀表达式中可能出现非个位数的数值如10

  1. #include "iostream"
  2. using namespace std;
  3. #include <string>
  4. #include <stack>
  5. #include <sstream>
  6. #include <vector>
  7. #include "math.h"
  8. void Infix2Suffix(string& str, char* strtmp)
  9. {
  10. // 需要注意负号表示负数的问题
  11. stack<char> s;
  12. int len = 0;
  13. for (unsigned int i=0; i<str.length(); i++)
  14. {
  15. if (str[i] >= 48 && str[i] <= 57)
  16. {
  17. strtmp[len] = str[i];
  18. len++;
  19. if (i<str.length() && (str[i+1] <48 || str[i+1]>57))
  20. {
  21. strtmp[len] = ' ';
  22. len++;
  23. }
  24. }
  25. else if (str[i] == '[' || str[i] == '{' || str[i] == '(')
  26. s.push(str[i]);
  27. else if(str[i] == '+')
  28. {
  29. if (s.empty())
  30. s.push(str[i]);
  31. else
  32. {
  33. while (!s.empty())
  34. {
  35. if (s.top() == '-' || s.top() == '*' || s.top() == '/'|| s.top() == '+')
  36. {
  37. strtmp[len] = s.top();
  38. len++;
  39. strtmp[len] = ' ';
  40. len++;
  41. s.pop();
  42. if (s.empty())
  43. {
  44. s.push(str[i]);
  45. break;
  46. }
  47. }
  48. else if ( s.top() == '(' || s.top() == '{' || s.top() == '[')
  49. {
  50. s.push(str[i]);
  51. break;
  52. }
  53. }
  54. }
  55. }
  56. else if(str[i] == '-')
  57. {
  58. // 得先判断减号是不是代表负数
  59. if (i==0 || ((str[i-1] == '+' || str[i-1] == '*' || str[i-1] == '/' || str[i-1] == '(' || str[i-1] == '[' || str[i-1] == '{')))
  60. {
  61. strtmp[len] = '0';
  62. len++;
  63. strtmp[len] = ' ';
  64. len++;
  65. }
  66. if (s.empty())
  67. s.push(str[i]);
  68. else
  69. {
  70. while (!s.empty())
  71. {
  72. if (s.top() == '+' || s.top() == '*' || s.top() == '/' ||s.top() == '-')
  73. {
  74. strtmp[len] = s.top();
  75. len++;
  76. strtmp[len] = ' ';
  77. len++;
  78. s.pop();
  79. if (s.empty())
  80. {
  81. s.push(str[i]);
  82. break;
  83. }
  84. }
  85. else if (s.top() == '(' || s.top() == '{' || s.top() == '[')
  86. {
  87. s.push(str[i]);
  88. break;
  89. }
  90. }
  91. }
  92. }
  93. else if(str[i] == '*')
  94. {
  95. if(s.empty())
  96. s.push(str[i]);
  97. else
  98. {
  99. while (!s.empty())
  100. {
  101. if ( s.top() == '/'||s.top() == '*' )
  102. {
  103. strtmp[len] = s.top();
  104. len++;
  105. strtmp[len] = ' ';
  106. len++;
  107. s.pop();
  108. if (s.empty())
  109. {
  110. s.push(str[i]);
  111. break;
  112. }
  113. }
  114. else if (s.top() == '+' ||s.top() == '-' || s.top() == '(' || s.top() == '{' || s.top() == '[')
  115. {
  116. s.push(str[i]);
  117. break;
  118. }
  119. }
  120. }
  121. }
  122. else if(str[i] == '/')
  123. {
  124. if (s.empty())
  125. s.push(str[i]);
  126. else
  127. {
  128. while (!s.empty())
  129. {
  130. if ( s.top() == '*' || s.top() == '/' )
  131. {
  132. strtmp[len] = s.top();
  133. len++;
  134. strtmp[len] = ' ';
  135. len++;
  136. s.pop();
  137. if (s.empty())
  138. {
  139. s.push(str[i]);
  140. break;
  141. }
  142. }
  143. else if (s.top() == '+' ||s.top() == '-' || s.top() == '(' || s.top() == '{' || s.top() == '[')
  144. {
  145. s.push(str[i]);
  146. break;
  147. }
  148. }
  149. }
  150. }
  151. else if (str[i] == ')')
  152. {
  153. while (s.top() != '(')
  154. {
  155. strtmp[len] = s.top();
  156. len++;
  157. strtmp[len] = ' ';
  158. len++;
  159. s.pop();
  160. }
  161. s.pop();
  162. }
  163. else if (str[i] == ']')
  164. {
  165. while (s.top() != '[')
  166. {
  167. strtmp[len] = s.top();
  168. len++;
  169. strtmp[len] = ' ';
  170. len++;
  171. s.pop();
  172. }
  173. s.pop();
  174. }
  175. else if (str[i] == '}')
  176. {
  177. while (s.top() != '{')
  178. {
  179. strtmp[len] = s.top();
  180. len++;
  181. strtmp[len] = ' ';
  182. len++;
  183. s.pop();
  184. }
  185. s.pop();
  186. }
  187. }
  188. while (!s.empty())
  189. {
  190. strtmp[len] = s.top();
  191. len++;
  192. strtmp[len] = ' ';
  193. len++;
  194. s.pop();
  195. }
  196. }
  197. int getNumber(string& tmp)
  198. {
  199. stack<int> a;
  200. int tmp1 = 0;
  201. int tmp2 = 0;
  202. for (int i=0; i<tmp.length(); i++)
  203. {
  204. if (tmp[i] >=48 && tmp[i] <=57)
  205. {
  206. int count = 0;
  207. int j = i+1;
  208. while (j<tmp.length() && (tmp[j]>=48 && tmp[j]<=57) )
  209. j++;
  210. for (int k=i; k<j; k++)
  211. {
  212. count += (tmp[k]-48)*pow((double)10, (double)(j-i-1));
  213. }
  214. i = j;
  215. a.push(count);
  216. }
  217. else
  218. {
  219. if (tmp[i] == '+')
  220. {
  221. tmp1 = a.top();
  222. a.pop();
  223. tmp2 = a.top();
  224. a.pop();
  225. tmp1 = tmp2 + tmp1;
  226. a.push(tmp1);
  227. }
  228. else if (tmp[i] == '-')
  229. {
  230. tmp1 = a.top();
  231. a.pop();
  232. tmp2 = a.top();
  233. a.pop();
  234. tmp1 = tmp2 - tmp1;
  235. a.push(tmp1);
  236. }
  237. else if (tmp[i] == '*')
  238. {
  239. tmp1 = a.top();
  240. a.pop();
  241. tmp2 = a.top();
  242. a.pop();
  243. tmp1 = tmp2 * tmp1;
  244. a.push(tmp1);
  245. }
  246. else if (tmp[i] == '/')
  247. {
  248. tmp1 = a.top();
  249. a.pop();
  250. tmp2 = a.top();
  251. a.pop();
  252. tmp1 = tmp2 / tmp1;
  253. a.push(tmp1);
  254. }
  255. }
  256. }
  257. return a.top();;
  258. }
  259. int main(void)
  260. {
  261. string str;
  262. cin >> str;
  263. char strtmp[100] = {0};
  264. Infix2Suffix(str, strtmp);
  265. string strout = strtmp;
  266. //cout << strtmp << endl;
  267. cout << getNumber(strout) << endl;
  268. return 0;
  269. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/830113
推荐阅读
相关标签
  

闽ICP备14008679号