// C++ program for infix to postfix conversion using stack. #include <iostream> #include <stack> #include <string> #include <unordered_map> using namespace std; bool isOperator(char c) { return (c == '+' || c == '-' || c == '*' || c == '/'); } int getPrecedence(char c) { if (c == '+' || c == '-') return 1; if (c == '*' || c == '/') return 2; return 0; } string infixToPostfix(string infix) { stack<char> s; string postfix = ""; unordered_map<char, int> precedence; precedence['+'] = 1; precedence['-'] = 1; precedence['*'] = 2; precedence['/'] = 2; for (char c : infix) { if (isalnum(c)) postfix += c; else if (c == '(') s.push(c); else if (c == ')') { while (!s.empty() && s.top() != '(') { postfix += s.top(); s.pop(); } s.pop(); } else { while (!s.empty() && precedence[c] <= precedence[s.top()]) { postfix += s.top(); s.pop(); } s.push(c); } } while (!s.empty()) { postfix += s.top(); s.pop(); } return postfix; } int main() { string infixExpression = "a+b*c-(d/e+f*g*h)"; string postfixExpression = infixToPostfix(infixExpression); cout << "Infix Expression: " << infixExpression << endl; cout << "Postfix Expression: " << postfixExpression << endl; return 0; }
// Java program for infix to postfix conversion using stack import java.util.HashMap; import java.util.Map; import java.util.Stack; public class InfixToPostfix { public static String infixToPostfix(String infix) { Stack<Character> stack = new Stack<>(); StringBuilder postfix = new StringBuilder(); Map<Character, Integer> precedence = new HashMap<>(); precedence.put('+', 1); precedence.put('-', 1); precedence.put('*', 2); precedence.put('/', 2); for (char c : infix.toCharArray()) { if (Character.isLetterOrDigit(c)) postfix.append(c); else if (c == '(') stack.push(c); else if (c == ')') { while (!stack.isEmpty() && stack.peek() != '(') postfix.append(stack.pop()); stack.pop(); } else { while (!stack.isEmpty() && precedence.getOrDefault(c, 0) <= precedence.getOrDefault(stack.peek(), 0)) postfix.append(stack.pop()); stack.push(c); } } while (!stack.isEmpty()) postfix.append(stack.pop()); return postfix.toString(); } public static void main(String[] args) { String infixExpression = "a+b*c-(d/e+f*g*h)"; String postfixExpression = infixToPostfix(infixExpression); System.out.println("Infix Expression: " + infixExpression); System.out.println("Postfix Expression: " + postfixExpression); } }
# Python code for infix to postfix conversion using stack def infix_to_postfix(infix): stack = [] postfix = "" precedence = {'+': 1, '-': 1, '*': 2, '/': 2} for c in infix: if c.isalnum(): postfix += c elif c == '(': stack.append(c) elif c == ')': while stack and stack[-1] != '(': postfix += stack.pop() stack.pop() else: while stack and precedence.get(c, 0) <= precedence.get(stack[-1], 0): postfix += stack.pop() stack.append(c) while stack: postfix += stack.pop() return postfix # Example usage infix_expression = "a+b*c-(d/e+f*g*h)" postfix_expression = infix_to_postfix(infix_expression) print("Infix Expression:", infix_expression) print("Postfix Expression:", postfix_expression)
// C-Sharp Code for infix to postfix conversion using stack using System; using System.Collections.Generic; public class InfixToPostfix { public static string InfixToPostfix(string infix) { Stack<char> stack = new Stack<char>(); System.Text.StringBuilder postfix = new System.Text.StringBuilder(); Dictionary<char, int> precedence = new Dictionary<char, int> { { '+', 1 }, { '-', 1 }, { '*', 2 }, { '/', 2 } }; foreach (char c in infix) { if (char.IsLetterOrDigit(c)) postfix.Append(c); else if (c == '(') stack.Push(c); else if (c == ')') { while (stack.Count > 0 && stack.Peek() != '(') postfix.Append(stack.Pop()); stack.Pop(); } else { while (stack.Count > 0 && precedence.GetValueOrDefault(c, 0) <= precedence.GetValueOrDefault(stack.Peek(), 0)) postfix.Append(stack.Pop()); stack.Push(c); } } while (stack.Count > 0) postfix.Append(stack.Pop()); return postfix.ToString(); } public static void Main() { string infixExpression = "a+b*c-(d/e+f*g*h)"; string postfixExpression = InfixToPostfix(infixExpression); Console.WriteLine("Infix Expression: " + infixExpression); Console.WriteLine("Postfix Expression: " + postfixExpression); } }

























