SHARE
TWEET

Untitled

a guest Feb 22nd, 2020 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include<stack>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main() {
  7.     stack<char> operators;
  8.     string input;
  9.     string output = "";
  10.     cout << "Please enter your infix expression without spaces:\n";
  11.     cin >> input;
  12.     for (int i = 0; i < input.length(); i++) {
  13.         switch (input[i]) {
  14.         case '(':
  15.             operators.push(input[i]);
  16.             break;
  17.         case ')':
  18.             while (operators.top() != '(')
  19.             {
  20.                 output += operators.top();
  21.                 operators.pop();
  22.             }
  23.             operators.pop();
  24.             break;
  25.         case '^':
  26.             while (!operators.empty() && operators.top() == '^')
  27.             {
  28.                 output += operators.top();
  29.                 operators.pop();
  30.             }
  31.             operators.push(input[i]);
  32.             break;
  33.         case '*':
  34.         case '/':
  35.             while (!operators.empty() && (operators.top() == '^' || operators.top() == '/' || operators.top() == '*'))
  36.             {
  37.                 output += operators.top();
  38.                 operators.pop();
  39.             }
  40.             operators.push(input[i]);
  41.             break;
  42.         case '+':
  43.         case '-':
  44.             while (!operators.empty() && (operators.top() == '^' || operators.top() == '/' || operators.top() == '*' || operators.top() == '+' || operators.top() == '-'))
  45.             {
  46.                 output += operators.top();
  47.                 operators.pop();
  48.             }
  49.             operators.push(input[i]);
  50.             break;
  51.         default:
  52.             output += input[i];
  53.         }
  54.     }
  55.     while (!operators.empty()) {
  56.         output += operators.top();
  57.         operators.pop();
  58.     }
  59.     cout << "\nThe converted postfix expression is:\n" << output << "\n";
  60.     return 0;
  61. }
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