Advertisement
skb50bd

Infix_to_Postfix

Jun 7th, 2016
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. int main() {
  9.     stack <char> S;
  10.     char ch;
  11.     string line;
  12.  
  13.     getline(cin, line);
  14.  
  15.     for (int i = 0; line[i]; i++) {
  16.         if (line[i] == ' ')
  17.             continue;
  18.         else if (line[i] == '(' || line[i] == '+' || line[i] == '-' || line[i] == '*' || line[i] == '/' || line[i] == '%' || line[i] == '^')
  19.             S.push(line[i]);
  20.         else if (line[i] == ')') {
  21.             while (S.top() != '(') {
  22.                 cout << S.top() << " ";
  23.                 S.pop();
  24.             }
  25.             S.pop();
  26.         }
  27.         else
  28.             cout << line[i] << " ";
  29.     }
  30.     while (!S.empty()) {
  31.         ch = S.top();
  32.         if (ch != '(')
  33.             cout << ch << " ";
  34.         S.pop();
  35.     }
  36.     cout << endl;
  37.  
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement