Question: Could someone add notes to this code to help me understand what it is doing Example: //using the return variable as the recursive call perform

Could someone add notes to this code to help me understand what it is doing Example:

//using the return variable as the recursive call perform the step case. //and store in the return variable total = x + total; //then take the step towards the base case y = y - 1; 

CODE:

public class InfixToPostfix { Stack stack; private String infix; private String postfix = ""; public InfixToPostfix() { stack = new Stack(); } public String Convert() { for (int j = 0; j < infix.length(); j++) { char ch = infix.charAt(j); switch (ch) { case '+': case '-': Precdence(ch, 1); break; case '*': case '/': Precdence(ch, 2); break; case '(': stack.push(ch); break; case ')': gotParen(ch); break; default: postfix = postfix + ch; break; } } while (!stack.isEmpty()) { postfix = postfix + stack.pop(); } return postfix; } public void Precdence(char opThis, int prec1) { while (!stack.isEmpty()) { char opTop = stack.pop(); if (opTop == '(') { stack.push(opTop); break; } else { int prec2; if (opTop == '+' || opTop == '-') prec2 = 1; else prec2 = 2; if (prec2 < prec1) { stack.push(opTop); break; } else postfix = postfix + opTop; } } stack.push(opThis); } public void gotParen(char ch) { while (!stack.isEmpty()) { char chx = stack.pop(); if (chx == '(') break; else postfix = postfix + chx; } } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); System.out.println("Enter Infix Expression:"); String input = sc.nextLine(); String output; InfixToPostfix infixToPostfix = new InfixToPostfix(); infixToPostfix.infix = input; output = infixToPostfix.Convert(); System.out.println("Postfix expression is: " + output + ' '); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!