SHARE
TWEET

Untitled

a guest Sep 17th, 2019 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // infix expression evaluation
  2. // implemented with STL vector
  3. // evalues expression with multiple digit numbers, * / + - and {} [] () paratheis
  4. // assume the expression is well formatted with balanced braces.
  5.  
  6. #include <iostream>
  7. #include <stack>
  8. #include <string>
  9.  
  10. using namespace std;
  11.  
  12. //----------------------------------------------------------------
  13. // Generates a stack of string tokens representing postfix
  14. // notation for the input line
  15. void generateRPN(string line, stack<string> &postfix);
  16.  
  17. //----------------------------------------------------------------
  18. // Returns a the result of evaluating the passed postfix token
  19. // stack as a decimal string
  20. string evaluateRPN(stack<string> &postfix)
  21. {
  22.     //this is going to be the reversed stack just to make it numbers first then operands later
  23.     stack<string> reversedStack;
  24.  
  25.     // just going to push starting from the top of postfix stack
  26.     int number1 = 0;
  27.     int number2 = 0;
  28.     int smallAnswer = 0;
  29.     string ssmallAnswer;
  30.  
  31.     while (!postfix.empty()) {
  32.         reversedStack.push(postfix.top());
  33.         postfix.pop();
  34.     }
  35.  
  36.     stack<string> numbersStack;
  37.  
  38.     while (!reversedStack.empty()) {
  39.         if (isdigit(reversedStack.top()[0])) {
  40.             numbersStack.push(reversedStack.top());
  41.             reversedStack.pop();
  42.         } else {
  43.             if (reversedStack.top() == "-") {
  44.                 number2 = stoi(numbersStack.top());
  45.                 numbersStack.pop();
  46.                 number1 = stoi(numbersStack.top());
  47.                 numbersStack.pop();
  48.                 smallAnswer = number1 - number2;
  49.                 numbersStack.push(to_string(smallAnswer));
  50.             } else if (reversedStack.top() == "+") {
  51.                 number2 = stoi(numbersStack.top());
  52.                 numbersStack.pop();
  53.                 number1 = stoi(numbersStack.top());
  54.                 numbersStack.pop();
  55.                 smallAnswer = number1 + number2;
  56.                 numbersStack.push(to_string(smallAnswer));
  57.             } else if (reversedStack.top() == "*") {
  58.                 number2 = stoi(numbersStack.top());
  59.                 numbersStack.pop();
  60.                 number1 = stoi(numbersStack.top());
  61.                 numbersStack.pop();
  62.                 smallAnswer = number1 * number2;
  63.                 numbersStack.push(to_string(smallAnswer));
  64.             } else { // (reversedStack.top() == "/")
  65.                 number2 = stoi(numbersStack.top());
  66.                 numbersStack.pop();
  67.                 number1 = stoi(numbersStack.top());
  68.                 numbersStack.pop();
  69.                 smallAnswer = number1 / number2;
  70.                 numbersStack.push(to_string(smallAnswer));
  71.             }
  72.             reversedStack.pop();
  73.         }
  74.     }
  75.     return 0;
  76.  
  77. }
  78.  
  79. //----------------------------------------------------------------
  80. int main(int argc, char ** argv)
  81. {
  82.     string line;
  83.     cout << "Expression: ";
  84.     while ( getline(cin, line) )
  85.     {
  86.         // skip empty line
  87.         if (line.length() == 0)
  88.             continue;
  89.  
  90.         // Makes line lowercase for checking to be case insensitive
  91.         //transform(line.begin(), line.end(), line.begin(), ::toupper);
  92.  
  93.         // exits program if 'exit' is entered
  94.  
  95.  
  96.  
  97.         stack<string> postfix;
  98.  
  99.         generateRPN(line, postfix);
  100.  
  101.         cout << "Answer: " << evaluateRPN(postfix) << endl;
  102.         line.clear();
  103.         cout << "Expression: ";
  104.     }
  105.     return 0;
  106. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top