Spaces between tokens are allowed but not required the


You are to write a program name calculator.java that evaluates an infix expression entered by the user. The expression may contain the following tokens:
(1) Integer constants (a series of decimal digits).
(2) x (representing a value to be supplied later).
(3) Unary operators (+ and -).
(4) Binary operators (+, -, *, / and %).
(5) Parentheses
Spaces between tokens are allowed but not required. The program will convert the expression to postfix (RPN) form and display the converted expression. In the postfix version of the expression, unary + will be removed, and the unary - operator will be replaced by the _ (underscore) character. The program will repeatedly prompt the user for the value of x, displaying the result from evaluating the expression each time. When the user enters the letter q instead of a number, the program terminates.

The following example illustrates the behavior of the program (user input is in bold and red):
Porgram output is in bold and green.

Enter infix expression: (x + 1) * (x - 2) / -4
Converted expression: x 1 + x 2 - * 4 _ /

Enter value of x: 5
Answer to expression: -4

Enter value of x: 7
Answer to expression: -10

Enter value of x: q

If the infix expression contains an error of any kind, the program must display the message Error in expression (with an optional explanation) and then terminate. The following examples illustrate various types of errors:

Enter infix expression: 1 2 +
Error in expression!! No operator between operands. Also last token must be an operand.

Enter infix expression: 10.4
Error in expression!! Cannot accept floating point numbers.

Enter infix expression: 1 ( + 2)
Error in expression!! No operator between operand and left parentheses.

Enter infix expression: 5 - (x - 2))
Error in expression!! No matching left parentheses for a right parentheses.

Enter infix expression: 1 ** 2
Error in expression!! The * operator cannot be preceded by a * operator.

The output of your program must match the format illustrated in this example.

Here are some other additional requirements for this program:

(1) You must use stack objects during the translation from infix to postfix and during the evaluation of the postfix expression.

(2) Operators must have the correct precedence and associativity. Unary operators take precedence over *, % and /, which in turn take precedence over binary + and -. Unary operators are right associative; binary operators are left associative.

Request for Solution File

Ask an Expert for Answer!!
Basic Computer Science: Spaces between tokens are allowed but not required the
Reference No:- TGS0645105

Expected delivery within 24 Hours