Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

The Problem Reverse Polish Notation (RPN) is a way of representing mathematical expressions. In RPN a stack is used to store the operands. RPN is

The Problem

Reverse Polish Notation (RPN) is a way of representing mathematical expressions. In RPN a stack is used to store the operands. RPN is also known as postfix notation, since operators come after the operands. The expression 4 + 8 would be written as 4 8 + with the operands 4 and 8 being pushed on to the stack. Postfix notation removes the requirement for parenthesis. The name Reverse Polish comes from the the Polish nationality of the inventor of postfix notation. You will write an integer calculator using RPN. Your functions will be written in three files, calc.cpp, RPNStack.cpp, and RPNStack.h. Remember, there are many different expressions you will need to handle: 8 4 2 1 + / - is a valid expression as is 8 4 + 2 / 1 -. Results of an expression should be both displayed and pushed on to the stack. 8 4 / should evaluate to 2

Implement an RPN stack structure

Implement an RPN calculator

Handle error cases

Implementation

You have been given a file named main.cpp which contains your main. You will need to create two new files named RPNStack.cpp and RPNStack.h.

RPNStack.h

Define a struct named RPNStack

Include function prototypes for pop, push, duplicate, rotate and the output operator

RPNStack.cpp

void RPNStack::rotate() -- pops the top value n and rotates the next n elements

void RPNStack::duplicate() -- duplicate the top element of the stack

void RPNStack::push(int x) -- push x onto the stack

int RPNStack::pop() -- return and remove the top element of the stack

int RPNStack::size() -- return the number of elements on the stack

ostream& operator<<(ostream& os, const RPNStack& rhs)

calc.cpp

handles input

see -- prints the stack to the screen

dup -- duplicates the top of the stack

rot -- rotates the stack

pop -- pops the top number off the stack

end -- exit the calculator

+ - * / % ^ -- operations

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions