Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. Let's try to develop a C++ Reverse Polish Notation (RPN) calculator!
  2. • https://en.wikipedia.org/wiki/Reverse_Polish_notation
  3. • https://www.youtube.com/watch?v=7edoVNuwGvc
  4. Create a base class called Operand
  5. Derive a class called Number from Operand
  6. • Maintain a double member variable in class Number
  7. • For simplicity, you may make the member variable public if you would like
  8. Derive a class called Operator from Operand
  9. Derive a class called Add from Operator (2 + 3 = 5)
  10. Derive a class called Subtract from Operator (5 - 2 = 3)
  11. Derive a class called Multiply from Operator (5 * 3 = 15)
  12. Derive a class called Divide from Operator (18 / 6 = 3)
  13. Derive a class called Square from Operator (3^2 = 9)
  14. Almost all of the above classes are going to be "empty" classes
  15. • As described above, only class Number will have a true constructor with a member variable and member functions
  16. • Everything else will actually be empty! Like seriously empty!
  17. • If you wish you can put all of the classes described above into a single file called Operands.h
  18. In a separate file, create a function called Calculate() that must do the following:
  19. • 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)
  20. • Use an std::stack of double values internally to maintain the numeric value stack throughout the execution of the function
  21. • For each element of the queue, starting from the front:
  22. o If the operand is a Number, simply add its value to the stack
  23. o 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
  24.  You'll need to use the dynamic_cast keyword to make this work correctly
  25.  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.
  26. • When things complete, the stack should only hold the final result value. Return it from the function.
  27. Develop some C++ code in the main() function that uses your Calculate() function by doing the following:
  28. • Watch this video for the calculation I want to see computed by your program
  29. • Create a queue of Operand pointers
  30. • Use push_back operations to build the stack just like you would using RPN
  31. • Call Calculate() with the queue
  32. • In this case, print 11 by using the video as your template (by using code like you see below)
  33. Simpler Example Code:
  34. std::queue<Operand *> a;
  35. a.push_back(new Number(1));
  36. a.push_back(new Number(2));
  37. a.push_back(new Add());
  38. std::cout << Calculate(a); // Prints 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement