Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. Operand::pointer_type RPNEvaluator::evaluate(TokenList const& rpnExpression) {
  2. Operand::pointer_type result;
  3.  
  4. stack <Operand::pointer_type> operand_stack;
  5.  
  6. if (rpnExpression.empty())
  7. throw exception("Error: insufficient operands");
  8.  
  9. for (auto x : rpnExpression)
  10. {
  11. if (is<Operand>(x))
  12. {
  13.  
  14. operand_stack.push(convert<Operand>(x));
  15.  
  16.  
  17. }
  18. else
  19. {
  20.  
  21. auto opera = convert<Operation>(x);
  22. if (dynamic_pointer_cast<Operation>(x)->number_of_args() > operand_stack.size())
  23. {
  24. throw exception("Insufficient # operands for operation");
  25. }
  26.  
  27. OperandList opList;
  28. unsigned size = opera->number_of_args();
  29.  
  30. while (size != 0)
  31. {
  32. opList.push_front(operand_stack.top());
  33. operand_stack.pop();
  34. --size;
  35. }
  36. //operand_stack.push(convert<Operand>(x));
  37.  
  38. if (is<UnaryOperator>(opera))
  39. {
  40. if (is<Negation>(opera))
  41. {
  42. if (opList.size() == 1) {
  43. if (is<Integer>(opList[0])) {
  44. if (is<Integer>(opList[0])) {
  45. Integer::value_type temp = (dynamic_pointer_cast<Integer>(opList[0])->get_value()) * -1;
  46. operand_stack.push(dynamic_pointer_cast<Operand>(make<Integer>(temp)));
  47. }
  48. }
  49. else if (is<Real>(opList[0])) {
  50. Real::value_type temp = dynamic_pointer_cast<Real>(opList[0])->get_value() * -1;
  51. operand_stack.push(dynamic_pointer_cast<Operand>(make<Real>(temp)));
  52. }
  53. }
  54. else
  55. {
  56. throw invalid_argument("Operands Not Valid For Operation");
  57. operand_stack.push(dynamic_pointer_cast<Operand>(make<Integer>(0)));
  58. }
  59. }
  60.  
  61. if (is<Identity>(opera))
  62. {
  63. if (opList.size() == 1)
  64. {
  65. Integer::value_type temp = dynamic_pointer_cast<Integer>(opList[0])->get_value();
  66. operand_stack.push(dynamic_pointer_cast<Operand>(make<Integer>(temp)));
  67. }
  68.  
  69.  
  70. }
  71. if (is<Factorial>(opera))
  72. {
  73.  
  74. Integer::value_type temp = dynamic_pointer_cast<Integer>(opList[0])->get_value();
  75. temp = factorial(temp);
  76. operand_stack.push(dynamic_pointer_cast<Operand>(make<Integer>(temp)));
  77. }
  78.  
  79.  
  80. }
  81.  
  82.  
  83. }
  84.  
  85. }
  86.  
  87.  
  88.  
  89. if (operand_stack.size() > 1)
  90. throw exception("Error: too many operands");
  91. if (operand_stack.empty())
  92. throw exception("Too many operations for the number of operands");
  93.  
  94. result = operand_stack.top();
  95.  
  96.  
  97. operand_stack.pop();
  98.  
  99.  
  100. //result = rpnExpression;
  101.  
  102. return result; // replace this with the result.
  103.  
  104.  
  105.  
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement