(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. 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/subtraction, multiplication, division, and modulus 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.
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/subtraction, multiplication, division, and modulus 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 6 steps with 3 images