#include <iostream>
#include <stack>
#include <string>
using namespace std;
// Function to return precedence of operators
int precedence(char op) {
if (op == '^') return 3;
else if (op == '*' || op == '/') return 2;
else if (op == '+' || op == '-') return 1;
else return -1; // For parentheses
}
// Function to convert infix expression to postfix
string infixToPostfix(string infix) {
stack<char> st; // Stack to hold operators
string postfix = ""; // Output postfix expression
for (int i = 0; i < [Link](); i++) {
char ch = infix[i];
// If the character is an operand, add it to the output
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
|| (ch >= '0' && ch <= '9')) { postfix += ch; }
// If the character is '(', push it onto the stack
else if (ch == '(') {
[Link](ch);
}
// If the character is ')', pop and add to output
// until '(' is encountered
else if (ch == ')') {
while (![Link]() && [Link]() != '(') {
postfix += [Link]();
[Link]();
}
[Link](); // Pop '(' from the stack
}
// If the character is an operator
else {
while (![Link]() && precedence([Link]()) >= precedence(ch)) {
postfix += [Link]();
[Link]();
}
[Link](ch); // Push the current operator onto the stack
}
}
// Pop all remaining operators from the stack
while (![Link]()) {
postfix += [Link]();
[Link]();
}
return postfix;
}
int main() {
string infix;
cout << "Enter an infix expression: ";
cin >> infix;
string postfix = infixToPostfix(infix);
cout << "Postfix expression: " << postfix << endl;
return 0;
}