In this program you will implement a simple reverse polish

Assignment Help C/C++ Programming
Reference no: EM13375994

In this program, you will implement a simple Reverse Polish Notation (RPN) calculator. RPN is a notation in which arithmetic expressions are written using operands followed by operators (it is also called postfix notation). For example, the expression 2 + 3 is written in RPN form as

2 3 +

More complicated expressions can be written in RPN form without the use of parentheses. For
example, the expression


((2 + 3)*(7 - 4))/5

can be written in RPN form as
2 3 + 7 4 - * 5 /

An RPN calculator can be implemented using a stack to store operands.
The algorithm for evaluating an expression in RPN form can be described as follows. The RPN
expression is provided in the form of a string consisting of tokens. Each token is either an
operand (a double precision floating point number) or an operator (one of '+', '-',
'*','/'). The algorithm evaluates the expression using the following steps:


1) Read a token
2) If the token is an operand, push it on the stack
3) If the token is an operator, apply it to the two top-most operands on the stack. Remove
the two operands used from the stack. Then store the result of the operation on the top of
the stack
4) If no more tokens are available, the final result is the operand at the top of the stack.
Otherwise go to 1)

In the example of the expression given above, 2 3 + 7 4 - * 5 / , the stack would have
the following contents at each step of the calculation

2 // push operand 2
2 3 // push operand 3
5 // apply operator + to 2 and 3, result is 5
5 7 // push operand 7
5 7 4 // push operand 4
5 3 // apply operator - to 7 and 4, result is 3
15 // apply operator * to 5 and 3, result is 15
15 5 // push operand 5
3 // apply operator / to 15 and 5, result is 3

The final result is 3

RPN program specification
The RPN calculator program should read the RPN expression as an entire line from stdin.
Input will consist of a single line. After completing the evaluation of the expression, the program
should print the contents of the entire stack, starting with the top operand and printing each
operand on a separate line. Use the default precision for double precision numbers.

Error conditions(Invalid input)
If any operand is invalid, i.e. the token cannot be converted to a double, or if any operator is
not in the set {'+','-','*','/'}, the program should print

invalid input

and exit.

Stack underflow
If an operator is read and there are currently fewer than two operands on the stack, the operation
cannot be performed. In that case, the program should print

stack underflow

and exit.

Division by zero
If the operator '/' is read and the operand currently on the top of the stack is zero, the division
cannot be performed. In that case, the program should print

division by zero

and exit.

The above error messages should be printed on stdout. Do not use stderr.
Implementation+

The program should use the STL stack container adapter to store the double precision
operands. Use the methods described in Lecture 15 to read entire lines of input and analyze each
token to ensure that the input is valid.

Reference no: EM13375994

Questions Cloud

Q1 let us consider a three-phase balanced y-connected : q1. let us consider a three-phase balanced y-connected source with vab400 30angv rms and 50 hz. this source supplies
For given color image with illumination colorwhite the : for given color image with illumination colorwhite the problem is to remove this illuminant color from the image. to
A bernstein copula is a nonparametric copula which can be : a bernstein copula is a nonparametric copula which can be described by a given grid resolution and an associated
The circuit arrangements for each of the installations the : the circuit arrangements for each of the installations the final circuit cable selection the maximum demand estimation
In this program you will implement a simple reverse polish : in this program you will implement a simple reverse polish notation rpn calculator. rpn is a notation in which
Java program the program has a page that shows the users : java program the program has a page that shows the users name and program name. a second jpanel that shows 4 buttons
One way of purifying gaseous h2 is to pass it under high : one way of purifying gaseous h2 is to pass it under high pressure through the holes of a metals crystal structure.
1nbsp below are six statementsnbsp indicate whether each : 1nbsp below are six statements.nbsp indicate whether each one pertains to microeconomics mic or macroeconomics
Lets take a look at how we might model the eect of : lets take a look at how we might model the eect of increased income or wealth on peoples preferences for dierent kinds

Reviews

Write a Review

C/C++ Programming Questions & Answers

  Write a program for a college''s admissions office

Write a program for a college's admissions office. Create variables that store a numeric high school grade point average andan admission test score.Print the message "Accept" if the studenthas any of the following:

  Create a boolean function that computes

Create a Boolean function that computes the value of a factorial for values up to including 10. Include comments about how to use the function and describing how it works.

  Program calculates and displays salesperson''s commission

The sales manager at Tompkins Company wants a program that calculates and displays each salesperson's commission, which is 10% of his or her sales. It also should display the total commission. Use a value-returning function to get the amount sold by ..

  Define two derived classes of the abstract class

Define two derived classes of the abstract class ShapeBase below. Your two classes will be called RightArrow and LeftArrow. These classes will draw arrows that point right and left,

  How many integer values are in the array

Write a function named "location_of_largest" that takes as its arguments the following.

  Write c program to find smallest-largest values

Write a program c that will find the smallest, largest and average values in a collection of N numbers.Get the value of N before scanning each value in the collection of N numbers.

  Writing a program that creates a linked list

Writing a program that creates a linked list from 100 randomly generated numbers and sorts the using a heap. One heap in ascending order and one heap in descending order. Then displays the original linked list and both sorted lists.

  C++ pointer function synopsis

C++ pointer Function Synopsis: char *amonthLess(char *dateString) Description: Variable dataString is a valid date in a form of mm/dd/yyyy Given the above information, write a main() program that asks a user to enter a date in a form of mm/dd/yyyy..

  Write a c++ program that asks the user for a radius r

Write a c++ program that asks the user for a radius r. The program must include a function called calcwradius that accepts the value of the radius r, and calls the function pi shown below.

  Write application to calculate price of carpeting for room

The Westfield Carpet Company has asked you to write an application that calculates the price of carpeting for rectangular rooms.

  Write program to input series of hourly temperatures

Create and write a c++ program which inputs series of 24 hourly temperatures from file, and outputs bar chart (using stars) of temperatures for the day.

  Write a program using vectors and iterators that allows a

write a program using vectors and iterators that allows a user to maintain a personal list of dvd titles. the user

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd