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

  Create program that uses functions and reference parameters

Create program that uses functions and reference parameters, and asks user for the outside temperature.

  Write a program using vectors and iterators

Write a program using vectors and iterators that allows a user to maintain a personal list of DVD titles

  Write the code required to analyse and display the data

Calculate and store the average for each row and column. Determine and store the values for the Average Map.

  Write a webservices application

Write a webservices application that does a simple four function calculator

  Iimplement a client-server of the game

Iimplement a client-server version of the rock-paper-scissors-lizard-Spock game.

  Model-view-controller

Explain Model-View-Controller paradigm

  Design a nested program

How many levels of nesting are there in this design?

  Convert celsius temperatures to fahrenheit temperatures

Write a C++ program that converts Celsius Temperatures to Fahrenheit Temperatures.

  Evaluate and output the value in the given base

Write C program that will input two values from the user that are a Value and a Base with which you will evaluate and output the Value in the given Base.

  Design a base class shape with virtual functions

Design a base class shape with virtual functions

  Implementation of classes

Implementation of classes Chart and BarChart. Class barChart chould display a simple textual representation of the data

  Technical paper: memory management

Technical Paper: Memory Management, The intent of this paper is to provide you with an in depth knowledge of how memory is used in executing, your programs and its critical support for applications.

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