Question

In: Computer Science

Let's try to develop a C++ Reverse Polish Notation (RPN) calculator! Create a base class called...

Let's try to develop a C++ Reverse Polish Notation (RPN) calculator!

Create a base class called Operand

  • Give it a virtual destructor to avoid any weird problems later on!

Derive a class called Number from Operand

  • Maintain a double member variable in class Number
  • For simplicity, you may make the member variable public if you would like

Derive a class called Operator from Operand

Derive a class called Add from Operator (2 + 3 = 5)

Derive a class called Subtract from Operator (5 - 2 = 3)

Derive a class called Multiply from Operator (5 * 3 = 15)

Derive a class called Divide from Operator (18 / 6 = 3)

Derive a class called Square from Operator (3 ^ 2 = 9)

Almost all of the above classes are going to be "empty" classes

  • As described above, only class Number will have a true constructor with a member variable and member functions
  • Everything else will actually be empty! Like seriously empty!
  • If you wish you can put all of the classes described above into a single file called Operands.h

In a separate file, create a function called Calculate() that must do the following:

  • The only input parameter is a std::queue of Operand pointers (the series of number values and operations that will be performed by the calculator)
  • Use an std::stack of double values internally to maintain the numeric value stack throughout the execution of the function
  • For each element of the queue, starting from the front:
    • If the operand is a Number, simply add its value to the stack
    • If the operand is an Operator, pop one or two values off the stack (depends on which of the five operators) and use that operator to do the correct math, then put the new value back on the stack
      • You'll need to use the dynamic_cast keyword to make this work correctly
      • If you don't correctly set up the calculation code, the stack might become empty too soon. Throw an exception of some kind if this happens.
  • When things complete, the stack should only hold the final result value. Return it from the function.

Develop some C++ code in the main() function that uses your Calculate() function by doing the following:

  • Create a queue of Operand pointers
  • Use push_back operations to build the stack just like you would using RPN
  • Call Calculate() with the queue
  • In this case, print 11 by using the video as your template (by using code like you see below)

Simpler Example Code:

std::queue<Operand *> a;
a.push_back(new Number(1));
a.push_back(new Number(2));
a.push_back(new Add());
std::cout << Calculate(a);    // Prints 3

Solutions

Expert Solution

Here i am providing the answer, Hope it helps, please give me a like, it helps me a lot.

Here is the C++ Reverse Polish Notation calculator....

#include<iostream.h>

#include<math.h>

#include<cstdlib.h>

#include<sstream>

#include<vector>

#include<iterator>

#include<algorithm>

#include<string>

double rpn(const std ::string &expr){

std :: istringstream iss(expr);

std :: vector<double> stack;

std :: cout <<"input\toperation\tstack after"<<std ::end1;

std :: string token;

while( iss >> token) {

std :: cout << token << "\t";

double tokenNum;

if ( std :: istringstream(token) >> tokenNum ) {

std :: cout << "push\t\t";

stack.push_back(tokenNum);

} else {

std :: cout <<"operator\t\t";

double secondOperator = 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 {

std : : cerr << "error" << std :: end1;

std :: exit(1);

}

}

std : : copy (stack.begin(),stack.end(),std :: ostream_iterator<double>(std :: cout, " "));

std : :cout<<std : : end1;

}

return stack.back();

}

int main() {

std : : string s = " " ;

std : : cout << "final answer : " << rpn(s) << std :: end1;

return 0;

}

thank you. please like.


Related Solutions

REVERSE POLISH CALCULATOR C++ ONLY. For this assignment, you are to write a program, which will...
REVERSE POLISH CALCULATOR C++ ONLY. For this assignment, you are to write a program, which will calculate the results of Reverse Polish expressions that are provided by the user. You must use a linked list to maintain the stack for this program (NO array implementations of the stack). You must handle the following situations (errors): Too many operators (+ - / *) Too many operands (doubles) Division by zero The program will take in a Polish expression that separates the...
Write a Reverse Polish Calculator, RPN We discussed in class a general implementation of RPN using...
Write a Reverse Polish Calculator, RPN We discussed in class a general implementation of RPN using a stack to keep track of the numbers. Each time an operand is encountered, two values are popped from this stack, the given operation is performed, and the result is pushed back onto the stack. Utilize Java generic Stack Class to implement this algorithm with four arithmetic operations: +, *, -, /. It is suggested that you implement a class RPN with a single...
Reverse Polish notation is a notation where every operator follows all of its operands. For example,...
Reverse Polish notation is a notation where every operator follows all of its operands. For example, an expression (1+2)*(5+4) in the conventional Polish notation can be represented as 1 2 + 5 4 + * in the Reverse Polish notation. One of advantages of the Reverse Polish notation is that it is parenthesis-free. Write a program which reads an expression in the Reverse Polish notation and prints the computational result. An expression in the Reverse Polish notation is calculated using...
Question: Reverse Polish notation is a notation where every operator follows all of its operands. For...
Question: Reverse Polish notation is a notation where every operator follows all of its operands. For example, an expression (1+2)*(5+4) in the conventional Polish notation can be represented as 1 2 + 5 4 + * in the Reverse Polish notation. One of advantages of the Reverse Polish notation is that it is parenthesis-free. Write a program which reads an expression in the Reverse Polish notation and prints the computational result. An expression in the Reverse Polish notation is calculated...
Reverse Polish (HP) Style Calculator - Part 2 The purpose of this assignment is to incorporate...
Reverse Polish (HP) Style Calculator - Part 2 The purpose of this assignment is to incorporate an abstract class and inheritance and add it to the interface of the business calculator created in Topic 4. • Implement a pure abstract stack class named AbstractStack that has no implementation. It will not have a private array of double, because that is an implementation detail of ArrayStack that would not be found in other implementations such as LinkedStack. Nor will it have...
Python Create a move function that is only defined in the base class called Objects. The...
Python Create a move function that is only defined in the base class called Objects. The move function will take two parameters x,y and will also return the updated x,y parameters.
1) Consider the following infix expressions. What is the equivalent postfix (reverse Polish notation) expression? 16/(5+3)b)...
1) Consider the following infix expressions. What is the equivalent postfix (reverse Polish notation) expression? 16/(5+3)b) A*B+C*Dc) X × Y + W × Z + V × U 2) Consider the postfix (reverse Polish notation) 10 5 + 6 3 - /. What is the equivalent infix expression?
In C++ Define a base class called Person. The class should have two data members to...
In C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and...
C++ Define a base class called Person. The class should have two data members to hold...
C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and !=...
Problem Description: SavingsAccount Develop a C++ class declaration called SavingsAccount class that allows user to input...
Problem Description: SavingsAccount Develop a C++ class declaration called SavingsAccount class that allows user to input initial values of dollars and cents and then asks for deposits and withdrawals. The class should include the following information: Operations (Member Functions) 1. Default constructor that sets both dollars and cents to 0. 2. The constructor has 2 parameters that set dollars and cents to the indicated values. 3. Open account (with an initial deposit). This is called to put initial values in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT