Question: The menu: 1 . Enter a fully parenthesized expression. 2 . Evaluate a fully parenthesized expression. 3 . Convert a fully parenthesized expression to post
The menu:
Enter a fully parenthesized expression.
Evaluate a fully parenthesized expression.
Convert a fully parenthesized expression to postfix.
Evaluate postfix
Quit
Enter your choice
public class StackDriver
public static void mainString args
Scanner in new ScannerSystemin;
String expression ;
boolean quit false;
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 :
if expression.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 && precedencech precedencechar operators.top
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; System.out.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
