Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with an RPN Calculator C++ program: -Implement a RPN calculator: develop a C++ program that implements an interactive calculator for postfix arithmetic.

I need help with an RPN Calculator C++ program:

-Implement a RPN calculator: develop a C++ program that implements an interactive calculator for postfix arithmetic. -first step is to create a dynamic stack implementation that can handle evaluation of an arbitrary long expression (you are not allowed to use the collection classes). -Your program will then check to see whether the given expression contains a syntax error, a stack underflow error, or a too many operands error, in that order. If the syntax of the expression is erroneous, your program should display an appropriate error message. -program has to use a stack data structure to evaluate each valid postfix arithmetic expression. Each operand must be converted from an input substring to the numerical value represented by that substring, and then pushed onto the operand stack. To evaluate each operator, your program must pop the stack to retrieve the operand(s), perform the specified operation on the operand(s), and push the result back onto the stack. Note that for subtraction and division, operand order is critical: the first number popped from the stack should be used as the second operand, while the second number popped from the stack should be used as the first operand. Your program must interactively display the value of the given expression. After processing the first expression, your program should ask the user whether he or she wishes to enter another expression, and should repeat the entire expression evaluation process until the user responds negatively.

Four error conditions can occur:

SYNTAX ERROR: valid postfix expressions should consist only of blanks, positive decimal integer operands, and the binary integer operators +, -, *, /, and an unary operator sqrt. The operands and operators will be separated by one or more blanks. Zero or more blanks may appear at either the beginning or the end of the expression. Negative operands are not allowed in the input expression (although the evaluation of a given expression may produce a negative result).

STACK UNDERFLOW: postfix expressions are evaluated from left to right. An underflow error will occur if, at any time during expression evaluation, the number of operators

encountered up to that point does not match the number of operands.

TOO MANY OPERANDS: If, after evaluating the entire postfix expression, the stack contains more than one value, then the given postfix expression contained too many operands.

DIVIDE BY ZERO: If the top operand on the stack is a 0 when the / operator is detected, then a divide-by-zero error will occur. This error cannot be readily detected without evaluation of the expression.

Examples of valid postfix expressions include the following:

1 2 (returns 12)

2 16 sqrt (returns 4)

3 100 567 - (returns -467)

4 1 3 + 4 5 * / (returns [1 + 3] / [4 * 5] = 4 / 20 = 0) 11 22 33 44 55 66 77 88 + - * / / * +

The following are examples of invalid postfix expressions:

12 3f - 1+ 1 2*/ 34- 11 23 79 * 8 3+ 46* 1 33 22 11 + - / (divide by zero error)

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

Step: 3

blur-text-image

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

Web Database Development Step By Step

Authors: Jim Buyens

1st Edition

0735609667, 978-0735609662

More Books

Students also viewed these Databases questions

Question

6. Identify characteristics of whiteness.

Answered: 1 week ago

Question

9. Explain the relationship between identity and communication.

Answered: 1 week ago