Question: Use the following algorithm to write a program to evaluate an arithmetic expression written in postfixed notation: We are using the textbook Data Structure and

Use the following algorithm to write a program to evaluate an arithmetic expression written in postfixed notation:

Use the following algorithm to write a program to evaluate an arithmeticexpression written in postfixed notation: We are using the textbook Data Structureand Algorithms using Java by William McAllister. start Initialize stack s toWe are using the textbook Data Structure and Algorithms using Java by William McAllister.

start Initialize stack s to empty Fetch the first token, t, from the postfixed expression Yes ist empty? result - s.pop() end No ist an operator? S.push(t) Yes 01 - s.pop() 02 - s.pop() apply operator t to 0 and 02 (V = 01 t 02) s.push(v) get the next token, t This should be coded within a method of a class named PostfixEvaluation. It could even be done within a main method. An arithmetic expression will be input as a String (by the user) from the console and will contain only integer operands and the postfix operators. The following code is to parse (i.e., remove) the integers and the operators from the input string, mathExpression: import java.util; String thisToken; StringTokenizer tokens = new StringTokenizer (mathExpression); while(tokens.hasMoreTokens() { thisToken = tokens.nextToken(); !! processing for thisToken goes here } The import statement in the code above should occur before the declaration of your class. Before this code but within your method, you should also declare and initialize your Stack data structure. You may modify one of the Stack classes presented in the text to use integers, but you may find it easier to use the Java API Stack class. Your program should prompt for a math expression, evaluate it, and output the result. It should then prompt for another expression to evaluate or to end the program. Be sure to provide error code that will provide an appropriate error message should the user input an expression that is non- integer, does not have the proper number of operands, or has an unknown operator. It should allow the user to re-enter the expression. The only operators that should be handled are plus(+), minus(-), multiply(*), and dividel/). Use the expressions from Table 3.2 of the text to test your program. Table 3.2 Evaluation of Two Postfixed Arithmetic Expressions 23+ 4* and 2 3 4 * + New Postfixed String Postfixed Operator Two Previous (value replaces operator Step String Encountered Operands Value and operands) 1 23+4* + 2,3 5 54 2 54* 20 20 3 20 none, so 23+4* evaluates to 20 5,4 3,4 2 12 + 1 2 3 234* + 2 12 + 14 12 14 2,12 14 none, so 2 34* + evaluates to 14

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!