import java.util.*; public class Stackp{ static int Precedence(char ch){ switch (ch){ case '=': return 1; case '+': case '-': return 2; case '*': case '/': return 3; case '^': return 4; } return -1; } public static String InfixToPosfix(String exp){ Stack s = new Stack<>(); String result = ""; for(int i = 0;i< exp.length();i++){ char c = exp.charAt(i); if(Character.isLetter(c)){ result += c; } else if (c == '(') s.push(c); else if (c == ')') { while (!s.isEmpty() && s.peek() != '(') result += s.pop(); if (!s.isEmpty() && s.peek() != '(') return "Invalid Expression"; // invalid expression else s.pop(); } else{ while(!s.empty() && Precedence(c) <= Precedence(s.peek())) result += s.pop(); s.push(c); } } while(!s.empty()){ result +=s.pop(); } return result; } public static void main(String []args){ String result = InfixToPosfix("S=u*c^k/D-q*g^(n-b)"); System.out.println(result); } }