Problem Statement: (The following problem is based on a problem in a number of Java texts, including the text by Gaddis.) Given a numeric expression in prefix form, develop a Java program to evaluate the expression. ================================================================================ NOTE 1: Your program should internally convert the prefix expression into a binary tree before evaluating it Feel free to use any other data structures, in addition to the binary tree, that you may deem necessary to set up and evaluate your prefix expression ================================================================================ The following site provides some explanation on prefix expressions, as well as other forms of numeric expressions: Infix, Prefix and Postfix Expressions (Links to an external site.) Input Data: Repeatedly, enter one prefix expression (as a string) at a time at the keyboard alternatively, you may set up an input data file and get the data from the file Stop the inputting, when the user does not want to enter any additional strings NOTE 1: To simplify matters, assume that only non-negative integers are used the allowed operations are binary addition and multiplication operations Processing: Set up a method (or methods) to evaluate the given expression. Output Data: Echo back the entered input string, along with the result of the evaluation. Example 1: The prefix expression is: + 7 35 The expression evaluates to 42. Example 2: The prefix expression is: * + 16 4 + 3 1 The expression evaluates to 80.
Problem Statement:
(The following problem is based on a problem in a number of Java texts, including the text by Gaddis.)
Given a numeric expression in prefix form, develop a Java program to evaluate the expression.
================================================================================
NOTE 1:
- Your program should internally convert the prefix expression into a binary tree before evaluating it
- Feel free to use any other data structures, in addition to the binary tree, that you may deem necessary to set up and evaluate your prefix expression
================================================================================
The following site provides some explanation on prefix expressions, as well as other forms of numeric expressions:
Infix, Prefix and Postfix Expressions (Links to an external site.)
Input Data:
- Repeatedly, enter one prefix expression (as a string) at a time at the keyboard
- alternatively, you may set up an input data file and get the data from the file
- Stop the inputting, when the user does not want to enter any additional strings
NOTE 1: To simplify matters,
-
- assume that only non-negative integers are used
- the allowed operations are binary addition and multiplication operations
Processing:
Set up a method (or methods) to evaluate the given expression.
Output Data:
Echo back the entered input string, along with the result of the evaluation.
Example 1:
The prefix expression is: + 7 35
The expression evaluates to 42.
Example 2:
The prefix expression is: * + 16 4 + 3 1
The expression evaluates to 80.
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 7 images