Question: public class StackDriver { public static void main ( String [ ] args ) { Scanner in = new Scanner ( System . in )
public class StackDriver public static void mainString args Scanner in new ScannerSystemin; System.out.printEnter any parenthesized expression: ; String s innextLine; while quit System.out.printlnMenu:; System.out.println Enter a fully parenthesized expression."; System.out.println Evaluate a fully parenthesized expression."; System.out.println Convert a fully parenthesized expression to postfix."; System.out.println Evaluate postfix."; System.out.println Quit"; System.out.printEnter your choice: ; int choice innextInt; innextLine; switch choice case : System.out.printEnter a fully parenthesized expression: ; expression innextLine; if isLegalexpression System.out.printlnexpression is a legal expression."; else System.out.printlnexpression is not a legal expression."; break; case : ifexpression.isEmpty evaluateExprexpression; else System.out.printlnNo expression entered."; break; case : if expression.isEmpty String postFix convertToPostfixexpression; System.out.printlnPostfix notation: postFix; else System.out.printlnNo expression entered."; break; case : System.out.printlnWould you like to enter a new postfix expression? yn; String useExisting innextLine; if useExistingequalsIgnoreCasey System.out.printEnter postfix expression: ; String postFix innextLine; evaluatePostfixpostFix; else if expression.isEmpty String postFix convertToPostfixexpression; evaluatePostfixpostFix; else System.out.printlnNo expression available."; break; case : quit true; System.out.printlnExiting program..."; break; default: System.out.printlnInvalid choice."; public static String convertToPostfixString str StringBuilder result new StringBuilder; ListStack operators new ListStack; for int i ; i strlength; i char ch strcharAti; if CharacterisDigitch result.appendchappend; else if ch operators.pushch; else if ch while char operators.top result.appendchar operators.topAndPopappend; operators.pop; else if ch ch ch ch ch while operators.isEmpty result.appendchar operators.topAndPopappend; operators.pushch; while operators.isEmpty result.appendchar operators.topAndPopappend; return result.toStringtrim; public static void evaluatePostfixString postfix ListStack stack new ListStack; for int i ; i postfix.length; i char ch postfix.charAti; if CharacterisDigitch stack.pushintch ; else if ch ch ch ch ch int op stack.topAndPop; int op stack.topAndPop; switch ch case : stack.pushop op; break; case : stack.pushop op; break; case : stack.pushop op; break; case : stack.pushop op; break; case : stack.pushop op; break; Systemout.printlnPostfix result: stack.topAndPop;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
