Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. #include "Stack.h"
  2.  
  3. #include <list>
  4. using std::list;
  5. #include <vector>
  6. #include <iostream>
  7. #include <string>
  8. #include <sstream>
  9. #include <algorithm>
  10. #include <iterator>
  11. #include <iostream>
  12. using std::cout;
  13. using std::endl;
  14.  
  15. bool isSymbol(const string& token){
  16. string symbols[] = {"+","-","*","/"};
  17. for(int i = 0; i<4; i++){
  18. if (token == symbols[i]) {
  19. return true;
  20. }
  21. }
  22. return false;
  23. };
  24.  
  25. bool is_number(const std::string& s)
  26. {
  27. try
  28. {
  29. std::stod(s);
  30. }
  31. catch(...)
  32. {
  33. return false;
  34. }
  35. return true;
  36. }
  37.  
  38. double evaluate(string polish){
  39.  
  40. Stack s;
  41.  
  42. istringstream iss(polish);
  43. vector<string> tokens{istream_iterator<string>{iss},
  44. istream_iterator<string>{}};
  45.  
  46. for(int i=2; i < tokens.size(); i++){
  47. if(isSymbol(tokens[i])){
  48. string op = tokens[i];
  49. double l;
  50. if (is_number(tokens[i-2])) {
  51. l = stod(tokens[i-2]);
  52. } else {
  53. l = s.pop();
  54. }
  55. double r = stod(tokens[i-1]);
  56. double result;
  57. if (s.empty()){
  58. if (op == "+") {
  59. result = l + r;
  60. s.push(result);
  61. } else if (op == "-") {
  62. result = l - r;
  63. s.push(result);
  64. } else if (op == "/") {
  65. result = l / r;
  66. s.push(result);
  67. } else if (op == "*") {
  68. result = l * r;
  69. s.push(result);
  70. }
  71. } else {
  72. if (op == "+") {
  73. result = s.pop() + r;
  74. s.push(result);
  75. } else if (op == "-") {
  76. result = s.pop() - r;
  77. s.push(result);
  78. } else if (op == "/") {
  79. result = s.pop() / r;
  80. s.push(result);
  81. } else if (op == "*") {
  82. result = s.pop() * r;
  83. s.push(result);
  84. }
  85. }
  86. }
  87.  
  88.  
  89. }
  90. return s.pop();
  91. };
  92.  
  93.  
  94. int main() {
  95.  
  96. int retval = 0;
  97.  
  98. {
  99. list<double> pushThese{3.0, 7.0};
  100.  
  101. Stack s;
  102.  
  103.  
  104.  
  105. for (const double d : pushThese) {
  106. s.push(d);
  107. }
  108.  
  109. if (s.empty()) {
  110. cout << "0) Fail: stack is empty after pushing things onto it\n";
  111. ++retval;
  112. } else {
  113. cout << "0) Pass: stack is not empty after pushing things onto it\n";
  114. }
  115.  
  116. list<double> popped;
  117.  
  118. while (!s.empty()) {
  119. popped.push_front(s.pop());
  120. }
  121.  
  122. if (popped == pushThese) {
  123. cout << "1) Pass: pushed {3.0, 7.0}, popped {7.0, 3.0}\n";
  124. } else {
  125. cout << "1) Fail: pushed {3.0, 7.0}, popped {";
  126.  
  127.  
  128. for (bool comma = false; !popped.empty(); comma = true) {
  129. if (comma) {
  130. cout << ", ";
  131. }
  132. cout << popped.back();
  133. popped.pop_back();
  134. }
  135. cout << "}\n";
  136. ++retval;
  137. }
  138.  
  139.  
  140.  
  141. {
  142. double answer = evaluate("3.0 4.0 +");
  143. if (answer == 7.0) {
  144. cout << "2) Pass: evaluated '3.0 4.0 +', got 7.0\n";
  145. } else {
  146. cout << "2) Fail: evaluated '3.0 4.0 +' expecting 7.0, but got " << answer << std::endl;
  147. ++retval;
  148. }
  149. }
  150.  
  151. {
  152. double answer = evaluate("3.0 4.0 - 4.0 +");
  153. if (answer == 3.0) {
  154. cout << "3) Pass: evaluated '3.0 4.0 - 4.0 +', got 3.0\n";
  155. } else {
  156. cout << "3) Fail: evaluated '3.0 4.0 - 4.0 +' expecting 3.0, but got " << answer << std::endl;
  157. ++retval;
  158. }
  159. }
  160.  
  161. {
  162. double answer = evaluate("3.0 4.0 - 2.0 *");
  163. if (answer == -2.0) {
  164. cout << "4) Pass: evaluated '3.0 4.0 - 2.0 *', got -2.0\n";
  165. } else {
  166. cout << "4) Fail: evaluated '3.0 4.0 - 2.0 *' expecting -2.0, but got " << answer << std::endl;
  167. ++retval;
  168. }
  169. }
  170.  
  171. return retval;
  172. }};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement