Question
How to fix these code to print correct Postfix (Infix to postfix ) Now, print the wrong output ( *** Don't use Java's stack !!!*****)
How to fix these code to print correct Postfix
(Infix to postfix )
Now, print the wrong output
( *** Don't use Java's stack !!!*****)
Don't use Java's stack !!!*****
class stack211 { static String inf = "1+2*3+4*5+6"; static char[] myStack = new char[40]; static int stackTop = -1; public static void main(String[] args) { inToPost(inf); } static void inToPost(String s) { String post = ""; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == '(') { Push(c); System.out.println("pushing ( "); } else if (Character.isLetterOrDigit(c)) post = post + c; else if (c == ')') { while (!isEmpty() && myStack[stackTop] != '(') post = post + Pop(); } else Pop(); while (!isEmpty() && Calcu(c) <= Calcu(myStack[stackTop])) post = post + Pop(); Push(c); } while (!isEmpty()) post = post + Pop(); System.out.println("infix: " + inf); System.out.println("postfix: " + post ); } public static int Calcu(char operator) { switch (operator) { case '-': case '+': return 1; case '/': case '*': return 2; case '^': return 3; } return -1; } public static void Push(char c) { stackTop++; myStack[stackTop] = c; } public static char Pop() { char c = myStack[stackTop]; stackTop--; return c; } public static boolean isEmpty() { if (stackTop < 0) return true; else return false; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started