Let\'s try to develop a C++ Reverse Polish Notation (RPN) calculator! Create a b
ID: 3751898 • Letter: L
Question
Let's try to develop a C++ Reverse Polish Notation (RPN) calculator!
Create a base class called Operand
Derive a class called Number from Operand
- Maintain a double variable in class Number
- If you like, you can make the variable public for simplicity
Derive a class called Operator from Operand
- Derive a class called Add from Operator
- Derive a class called Subtract from Operator
- Derive a class called Multiply from Operator
- Derive a class called Divide from Operator
Almost all of the above classes are going to be empty
- Only class Number will have a true constructor and member variables
- If you wish you can put all of these classes into a file called Operands.h
Develop some C++ code that does the following:
- Create a queue of Operand pointers
- Use push_back operations to build the stack just like you would using RPN
- Develop a function called Calculate which takes in a stack and returns the double result
The Calculate() function must do the following:
- For each element of the queue starting at the front:
+ If the operand is a Number, add it to a stack
+ If the operand is an Operator, pop two values off the stack and use that operator to do the correct math, then put the new value back on the stack
= You'll need to use dynamic_cast to make this work
- When things complete, the stack should hold the final result. Return it.
Example:
std::queue a;
a.push_back(new Number(1));
a.push_back(new Number(2));
a.push_back(new Add());
std::cout << Calculate(a); // Prints 3
Explanation / Answer
In this code we are calculating rpn ,
#include<iostream>
#include<iterator>
#include <vector>
#include <string>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <cstdlib>
double rpncalculator(const std::string &expr){
std::istringstream iss(expr);
std::vector<double> stack;
std::cout << "Input Operation Stack after" << std::endl;
std::string token;
while (iss >> token) {
std::cout << token << " ";
double tokenNum;
if (std::istringstream(token) >> tokenNum) {
std::cout << "Push ";
stack.push_back(tokenNum);
}
else {
std::cout << "Operate ";
double secondOperand = stack.back();
stack.pop_back();
double firstOperand = stack.back();
stack.pop_back();
if (token == "*")
stack.push_back(firstOperand * secondOperand);
else if (token == "/")
stack.push_back(firstOperand / secondOperand);
else if (token == "-")
stack.push_back(firstOperand - secondOperand);
else if (token == "+")
stack.push_back(firstOperand + secondOperand);
else if (token == "^")
stack.push_back(std::pow(firstOperand, secondOperand));
else { //just in case
std::cerr << "Error" << std::endl;
std::exit(1);
}
}
std::copy(stack.begin(), stack.end(), std::ostream_iterator<double>(std::cout, " "));
std::cout << std::endl;
}
return stack.back();
}
int main() {
std::string s = " 3 4 2 * 1 5 - 2 3 ^ ^ / + ";
std::cout << "Final answer will be : " << rpncalculator(s) << std::endl;
return 0;
}
Output should be like
Input Operation Stack after
3 Push 3
4 Push 3 4
2 Push 3 4 2
* Operate 3 8
1 Push 3 8 1
5 Push 3 8 1 5
- Operate 3 8 -4
2 Push 3 8 -4 2
3 Push 3 8 -4 2 3
^ Operate 3 8 -4 8
^ Operate 3 8 65536
/ Operate 3 0.00012207
+ Operate 3.00012
Final answer will be : 3.00012
Thank you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.