Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Prefix Expression Evaluation 3 . 1 . Problem Description Mathematical expressions usually use infix notation in which the operator is in between the operands: (
Prefix Expression Evaluation
Problem Description
Mathematical expressions usually use infix notation in which the operator is in between the operands: With prefix notation, the operator comes first:
You are to write a program that evaluates expressions written in prefix notation. The values will be all integers and the only operators you need to handle are and all of which retain their traditional meanings.
Note that with prefix notation, we are not limited to just two operands. That is we can add any number of values: evaluates to This also works for and is interpreted as and evaluates to
Here is the recursive definition of your expressions:
expression: integervalue
or: operator valuelist
valuelist : expression
or: expression valuelist
Operator: or or or
Notes
Your program can assume that the input is wellformed. For example, there will be no bad operators, floating point values, unmatched parentheses, or missing arguments. All the tokens in the input will be separated by whitespace space tab, or newline which means that you can use Scanner.getNext to get each token.
Your program is to continue to accept expressions until the user enters done Ignore case on this comparison.
Your output must include your name.
Your solution must be recursive.
Turn in only your source file: PrefixEvaluation.java.
Make sure your class is not in a package that is it is in the default package
Hint: Consider writing a single recursive method int evalString e which evaluates e If e is an integer the base case just return its value. If e isnt an integer, it must be start with a recursive case Read the operator next, then read, evaluate and store ArrayList expressions until you find the matching Once you have your values the operands to the operation perform the required operation and return its result.
Required Main Class
PrefixEvaluation
Required Input
A series of prefix expressions followed by done
Page
Required Output
For each expression, evaluate the expression and print the result. You can use the examples below as a set of test cases. Your output should look like the following input is in GREEN
Prefix Evaluation Your Name
Enter an expression:
Evaluates to
Enter an expression:
Evaluates to
Enter an expression:
Evaluates to
Enter an expression:
Evaluates to
Enter an expression:
Evaluates to
Enter an expression:
Evaluates to
Enter an expression:
Evaluates to
Enter an expression: DoneI need help
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started