The rpn calculator program

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

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: EM13161797

Questions Cloud

Explain the relationships and related to each other : Interpret and explain the use case diagram in the Figure 1: book new edition p. 149. Explain the various roles of those using the system and what functions each role requires. Explain the relationships and how the use cases are related to each other.
Entrepreneurial intensity in the revolutionary sector : Companies should strive to position its EI (entrepreneurial intensity) in the revolutionary sector of the frequency-degree entrepreneurial grid in order to achieve best fit with its internal and external environments.
Design your application according to the considerations : Design your application according to the considerations described above.  For example, you must use functions that have the specified signatures, and arrays that have the specified declarations. They are
Write a program that asks a user for a file name and prints : Write a program that asks a user for a file name and prints the number of characters, words, and lines in that file.
The rpn calculator program : 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 th..
Function which correctly sorts three : Write an x8086 HLA Assembly language program that implements a function which correctly sorts three parameters and returns a boolean value in AL which should be set to true if any swaps were performed to sort the sequence in increasing order.
Cashregister class that can be used with the retailitem clas : Write a CashRegister class that can be used with the RetailItem class that you wrote in Part 1. The CashRegister class should simulate the sale of a retail item. It should have a constructor that accepts a RetailItem object as an argument.
Shows the users name and program name : Java program, the program has a page that shows the users name and program name. a second jpanel that shows 4 buttons (circle square rectangle and oval) the problem i am having is that my program is not dropping where i click and the shapes are not s..
The program should not accept quantities : Input Validation: The program should not accept quantities, or wholesale or retail costs, less than 0. The program should not accept dates that the programmer deter- mines are unreasonable.

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