Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. while(! queue.empty()) {
  2. std::string op;
  3.  
  4. const auto token = queue.front();
  5. queue.pop_front();
  6. switch(token.type) {
  7. case Token::Type::Number:
  8. stack.push_back(std::stoi(token.str));
  9. op = "Push " + token.str;
  10. break;
  11.  
  12. case Token::Type::Operator:
  13. {
  14. const auto rhs = stack.back();
  15. stack.pop_back();
  16. const auto lhs = stack.back();
  17. stack.pop_back();
  18.  
  19. switch(token.str[0]) {
  20. default:
  21. printf("Operator error [%s]\n", token.str.c_str());
  22. exit(0);
  23. break;
  24. case '^':
  25. stack.push_back(static_cast<char>(pow(lhs, rhs)));
  26. //stack.push_back(static_cast<int>(pow(lhs, rhs)));
  27. break;
  28. case 'r':
  29. //stack.push_back(static_cast<int>(sqrt(rhs))); //square root
  30. //stack.push_back(static_cast<int>(pow(rhs, 1/lhs))); //nth root
  31. //stack.push_back(pow(rhs, 1.0 / lhs));
  32. //stack.push_back(static_cast<signed char>(pow(rhs, 1.0/lhs))); //nth root
  33. stack.push_back(char(pow(rhs, 1.0/lhs))); //nth root
  34. break;
  35. case '*':
  36. //stack.push_back(lhs * rhs);
  37. stack.push_back(static_cast<char>(lhs * rhs));
  38. break;
  39. case '/':
  40. //stack.push_back(lhs / rhs);
  41. //stack.push_back(rational(lhs, rhs)); // TODO - fix rationalizing fractions
  42. //stack.push_back(divide(lhs, rhs));
  43. if(lhs%rhs == 0)
  44. stack.push_back(static_cast<char>(lhs / rhs));
  45. //stack.push_back(lhs / rhs);
  46. else
  47. stack.push_back(static_cast<char>(lhs / rhs));
  48. //stack.push_back(lhs / rhs);
  49.  
  50. //evenly_divisible = false;
  51. //return lhs, rhs;
  52. break;
  53. case '+':
  54. stack.push_back(static_cast<char>(lhs + rhs));
  55. //stack.push_back(lhs + rhs);
  56. break;
  57. case '-':
  58. stack.push_back(static_cast<char>(lhs - rhs));
  59. //stack.push_back(lhs - rhs);
  60. break;
  61. case 'u':
  62. //stack.push_back(static_cast<char>((-1) * rhs));
  63. stack.push_back(static_cast<char>(-rhs));
  64. //stack.push_back(static_cast<signed int>((-1) * rhs));
  65. // TODO add negative values possibility
  66. break;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement