Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Mathematical expressions usually use infix notation in which the operator is in between the operands: (1+ (2 3)). With prefix notation, the operator comes

image

Mathematical expressions usually use infix notation in which the operator is in between the operands: (1+ (2 3)). With prefix notation, the operator comes first: (+1(*23)). 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: (+1234) evaluates to 10. This also works for and /. (-10 123) is interpreted as 10-1-2-3 and evaluates to 4. Here is the recursive definition of your expressions: expression: integer value or: (operator value_list) value_list: expression or: expression value_list Operator: or-or/or* 3.2. Notes Your program can assume that the input is well-formed. 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 eval(String e) which evaluates e. Ife is an integer (the base case), just return its value. If e isn't 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. 3.3. Required Main Class PrefixEvaluation 3.4. Required Input A series of prefix expressions followed by "done". Mathematical expressions usually use infix notation in which the operator is in between the operands: (1+ (2 3)). With prefix notation, the operator comes first: (+1(*23)). 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: (+1234) evaluates to 10. This also works for and /. (-10 123) is interpreted as 10-1-2-3 and evaluates to 4. Here is the recursive definition of your expressions: expression: integer value or: (operator value_list) value_list: expression or: expression value_list Operator: or-or/or* 3.2. Notes Your program can assume that the input is well-formed. 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 eval(String e) which evaluates e. Ife is an integer (the base case), just return its value. If e isn't 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. 3.3. Required Main Class PrefixEvaluation 3.4. Required Input A series of prefix expressions followed by "done". Page < 6 of 6 - ZOOM + --------- 3.5. 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: 3 Evaluates to 3 Enter an expression: ( +12) Evaluates to 3 Enter an expression: ( / 16 3 ) Evaluates to 5 Enter an expression: ( -10 1 2 3 ) Evaluates to 4 Enter an expression: ( / 120 2 3 4 ) Evaluates to 5 Enter an expression: ( + 1 ( -12 10 ) 3 ( / 20 5 ) 5 ( * 23 ) ) Evaluates to 21 Enter an expression: ( + ( ( * ( / 12 4 ) 2 ) ( + 37 ) ) 10 ) Evaluates to 6 Enter an expression: Done

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Java How To Program Late Objects Version

Authors: Paul Deitel, Deitel & Associates

8th Edition

0136123716, 9780136123712

More Books

Students also viewed these Programming questions