#include #include #include using namespace std; int precedence(char o) { switch (o) { case '^': return 3; case '*': case '/': return 2; case '+': case '-': return 1; default: return -1; } } int main() { string exp, exp2; stack s; cout << "Enter expression to convert: "; cin >> exp; int len = exp.length(); cout << "Postfix Expression: "; for(int i = 0; i < len; i++) { char ch = exp[i]; if (ch == '(') s.push(ch); else if (ch == ')') { while (!s.empty() && s.top() != '(') { cout << s.top(); s.pop(); } if (!s.empty()) s.pop(); } else if (ch == '+' || ch == '*' || ch == '-' || ch == '/' || ch == '^') { while(!s.empty() && s.top() != '(' && precedence(s.top()) >= precedence(ch)) { cout << s.top(); s.pop(); } s.push(ch); } else { cout << ch; } } while (!s.empty()) { cout << s.top(); s.pop(); } return 0; }