fork download
  1. /*
  2.  * Author's name : Adrian Statescu
  3.  * Website : http://a...content-available-to-author-only...u.ch
  4.  * Date : 8:38 AM Monday, Nov 02, 2020
  5.  * File : evalexp.cpp
  6.  *
  7.  */
  8. #include <iostream>
  9. #include <fstream>
  10. #include <string>
  11. #include <iterator>
  12. #include <stack>
  13. #include <algorithm>
  14. #include <stdexcept>
  15. #include <map>
  16. #include <cctype>
  17. #include <cmath>
  18.  
  19. using namespace std;
  20.  
  21. long doOperation(char op, long left, long right) {
  22.  
  23. switch( op ) {
  24.  
  25. case '+': return left+right;
  26. case '-': return left-right;
  27. case '*': return left*right;
  28. case '/': return (left/right);
  29.  
  30. default: {
  31. return 0;
  32. }
  33.  
  34. }
  35. }
  36.  
  37.  
  38. void computeNextInStack(stack<long> & numberStack, stack<char> & operatorStack) {
  39.  
  40. char op = operatorStack.top();
  41. operatorStack.pop();
  42.  
  43. long right = numberStack.top();
  44. numberStack.pop();
  45.  
  46. long left = numberStack.top();
  47. numberStack.pop();
  48.  
  49. long result = doOperation(op, left, right);
  50.  
  51. numberStack.push( result );
  52. }
  53.  
  54. unsigned int priority(char op) {
  55.  
  56. switch( op ) {
  57.  
  58. case '+':
  59. case '-':
  60. return 1;
  61. case '*':
  62. case '/':
  63. return 2;
  64. default:
  65. return 0;
  66. }
  67. }
  68.  
  69.  
  70. long parseExpression(string const expression) {
  71.  
  72. stack<long> numberStack;
  73. stack<char> operatorStack;
  74.  
  75.  
  76. string::const_iterator pos = expression.begin();
  77.  
  78.  
  79. while(pos != expression.end()) {
  80.  
  81. char current = *pos;
  82.  
  83. switch(current) {
  84.  
  85. case '+':
  86. case '-':
  87. case '*':
  88. case '/':
  89.  
  90. {
  91.  
  92. while(
  93. !operatorStack.empty() &&
  94.  
  95. priority(operatorStack.top()) >= priority(current)
  96. )
  97.  
  98. {
  99. computeNextInStack(numberStack, operatorStack);
  100. }
  101.  
  102. operatorStack.push(current);
  103.  
  104. break;
  105. }
  106.  
  107.  
  108. //if we have an opening paranthes, then push it on the stack, simply
  109. case '(': {
  110.  
  111. operatorStack.push(current);
  112.  
  113. break;
  114. }
  115.  
  116. //if we have a closing paranthes, then keep calculating until opending paranthes
  117. case ')': {
  118.  
  119. if(operatorStack.empty()) {
  120.  
  121. //thow a runtime error. Syntax error in expression.
  122. //no matching ')' found
  123. return 0;
  124. }
  125.  
  126. while(operatorStack.top() != '(') {
  127.  
  128. computeNextInStack(numberStack, operatorStack);
  129.  
  130.  
  131. if(operatorStack.empty()) {
  132.  
  133. //thow a runtime error. Syntax error in expression.
  134. //no matching ')' found
  135. return 0;
  136. }
  137. }
  138.  
  139. operatorStack.pop();
  140. break;
  141. }
  142.  
  143.  
  144.  
  145. default: {
  146.  
  147. if(isdigit(current)) {
  148. //we parse the number
  149.  
  150. long number = 0;
  151.  
  152. while( pos != expression.end() && isdigit(*pos)) {
  153.  
  154. number *= 10;
  155. number += (*pos)-'0';
  156. pos++;
  157.  
  158. }
  159.  
  160.  
  161. numberStack.push(static_cast<long>(number));
  162.  
  163. pos--;
  164.  
  165. } else {
  166. //
  167. }
  168.  
  169. break;
  170. }
  171. }
  172.  
  173. pos++;
  174. }
  175.  
  176. while(!operatorStack.empty()) {
  177.  
  178. computeNextInStack(numberStack, operatorStack);
  179. }
  180.  
  181. return numberStack.top();
  182. }
  183.  
  184.  
  185. int main(int argc, char const *argv[])
  186. {
  187. string expression;
  188.  
  189. getline(cin, expression);
  190.  
  191. long result = parseExpression(expression);
  192.  
  193. cout<<result<<endl;
  194.  
  195.  
  196. return 0;
  197. }
  198.  
Success #stdin #stdout 0s 4244KB
stdin
((1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 1) / 2 + ( 5 * 3 - 1 ) / 2 ) * 2 + 2020
stdout
2090