Question: public class StackDriver { public static void main ( String [ ] args ) { Scanner in = new Scanner ( System . in )

public class StackDriver { public static void main(String []args){ Scanner in = new Scanner(System.in); System.out.print("Enter any parenthesized expression: "); String s = in.nextLine(); while (!quit){ System.out.println("Menu:"); System.out.println("1. Enter a fully parenthesized expression."); System.out.println("2. Evaluate a fully parenthesized expression."); System.out.println("3. Convert a fully parenthesized expression to post-fix."); System.out.println("4. Evaluate post-fix."); System.out.println("5. Quit"); System.out.print("Enter your choice: "); int choice = in.nextInt(); in.nextLine(); switch (choice){ case 1: System.out.print("Enter a fully parenthesized expression: "); expression = in.nextLine(); if (isLegal(expression)){ System.out.println(expression +" is a legal expression."); } else { System.out.println(expression +" is not a legal expression."); } break; case 2: if(!expression.isEmpty()){ evaluateExpr(expression); } else { System.out.println("No expression entered."); } break; case 3: if (!expression.isEmpty()){ String postFix = convertToPostfix(expression); System.out.println("Post-fix notation: "+ postFix); } else { System.out.println("No expression entered."); } break; case 4: System.out.println("Would you like to enter a new post-fix expression? (y/n)"); String useExisting = in.nextLine(); if (useExisting.equalsIgnoreCase("y")){ System.out.print("Enter post-fix expression: "); String postFix = in.nextLine(); evaluatePostfix(postFix); } else if (!expression.isEmpty()){ String postFix = convertToPostfix(expression); evaluatePostfix(postFix); } else { System.out.println("No expression available."); } break; case 5: quit = true; System.out.println("Exiting program..."); break; default: System.out.println("Invalid choice."); }} public static String convertToPostfix(String str){ StringBuilder result = new StringBuilder(); ListStack operators = new ListStack(); for (int i =0; i < str.length(); i++){ char ch = str.charAt(i); if (Character.isDigit(ch)){ result.append(ch).append(''); } else if (ch =='('){ operators.push(ch); } else if (ch ==')'){ while ((char) operators.top()!='('){ result.append((char) operators.topAndPop()).append(''); } operators.pop(); } else if (ch =='+'|| ch =='-'|| ch =='*'|| ch =='/'|| ch =='%'){ while (!operators.isEmpty()){ result.append((char) operators.topAndPop()).append(''); } operators.push(ch); }} while (!operators.isEmpty()){ result.append((char) operators.topAndPop()).append(''); } return result.toString().trim(); } public static void evaluatePostfix(String postfix){ ListStack stack = new ListStack(); for (int i =0; i < postfix.length(); i++){ char ch = postfix.charAt(i); if (Character.isDigit(ch)){ stack.push((int)(ch -'0')); } else if (ch =='+'|| ch =='-'|| ch =='*'|| ch =='/'|| ch =='%'){ int op2= stack.topAndPop(); int op1= stack.topAndPop(); switch (ch){ case '+': stack.push(op1+ op2); break; case '-': stack.push(op1- op2); break; case '*': stack.push(op1* op2); break; case '/': stack.push(op1/ op2); break; case '%': stack.push(op1% op2); break; }System.out.println("Postfix result: "+ stack.topAndPop());

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 Programming Questions!