Infix to Postfix Notation

#include <iostream> #include <stack> #include <string> using namespace std; int main() { stack <char> S; char ch; string line; getline(cin, line); for (int i = 0; line[i]; i++) { if (line[i] == ' ') continue; else if (line[i] == '(' || line[i] == '+' || line[i] == '-' || line[i] == '*' || line[i] == '/' || line[i] == '%' || line[i] == '^') S.push(line[i]); else if (line[i] == ')') { while (S.top() != '(') { cout << S.top() << " "; S.pop(); } S.pop(); } else cout << line[i] << " "; } while (!S.empty()) { ch = S.top(); if (ch != '(') cout << ch << " "; S.pop(); } cout << endl; return 0; }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.