Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Engineering Expert

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.

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M9885737

Have any Question?


Related Questions in Computer Engineering

Despite being noble gases xenon and radon actually form a

Despite being noble gases, xenon and radon actually form a small number of compounds. What is the partial pressure in atmospheres of radon in a mixture at STP composed of equal masses of xenon and radon?

Describe the definition of ransomware and what is wannacry

Describe the definition of ransomware. And what is wannacry threat?

Are us executives paid too much particularly compared to

Are U.S. Executives paid too much particularly compared to the average worker in their organization?

Excel discussionconsider this using the mouse to point and

Excel discussion Consider this.... Using the mouse to point and click is one way to work on a computer. Often, the same work can be accomplished using just the keyboard, using shortcut keyboard combinations. For example, ...

Question you are in a social situation talking with

Question : You are in a social situation talking with coworkers, friends, or family members who are not familiar with the concept of networking, except for the idea that they turn on their computer and surf the World Wid ...

Reusability of prior designs is critical when creating

Reusability of prior designs is critical when creating best-practice solutions in the enterprise. Capturing such designs provides useful guidance for Enterprise Architects. However, cataloguing such blueprints for reuse ...

Question what are the risks of transmitting data over an

Question : What are the risks of transmitting data over an unsecured Wi-Fi network? What steps are necessary to secure Wi-Fi Protected Access Network? Distinguish between client/server and file server architecture. Is te ...

Scruffie the cat has 15 to spend each month on cat toys

Scruffie the cat has $15 to spend each month on cat toys, which cost $3 each, and cat treats, which cost $1.50 each. Draw a budget line to show the combination of each good that scuffie can afford if she spends her entir ...

The switch statementhere is the question in its entirety

The switch statement Here is the question in its entirety, but the actual problem is at the end. I just felt that I would give an overview of what I am asking. The program has to be in c++ (.cpp) and must compile as such ...

A standard 52 card playing deck assigns a unique

A standard 52 card playing deck assigns a unique combination of 13 ranks (2,3,4,5,6,7,8,9,10,J,Q,K,A) and 4 suits (Clubs, Diamonds, Hearts, Spades) to each card (13  4 = 52). Suppose 5 cards are selected at random. Deriv ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As