fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <stack>
  4. #include <cctype>
  5.  
  6. using namespace std;
  7.  
  8. int main () {
  9. istringstream is ("4.5 7.89 + 45.2 2*");
  10. stack<double> operands;
  11. double acc = 0;
  12.  
  13. while (!is.eof()) {
  14. double operand;
  15. char oper;
  16.  
  17. if (is >> oper && !isdigit(oper)) {
  18. switch (oper) {
  19. case '+': {
  20. while (!operands.empty()) {
  21. acc += operands.top();
  22. operands.pop();
  23. }
  24. } break;
  25. case '-': {
  26. while (!operands.empty()) {
  27. acc -= operands.top();
  28. operands.pop();
  29. }
  30. } break;
  31. case '*': {
  32. if (acc == 0 && !operands.empty()) {
  33. acc = operands.top();
  34. operands.pop();
  35. }
  36. while (!operands.empty()) {
  37. acc *= operands.top();
  38. operands.pop();
  39. }
  40. } break;
  41. // etc
  42. }
  43. } else {
  44. is.unget();
  45. is >> operand;
  46. operands.push (operand);
  47. }
  48. }
  49.  
  50. cout << "Result: " << acc << endl;
  51. }
  52.  
  53.  
Success #stdin #stdout 0.02s 2864KB
stdin
Standard input is empty
stdout
Result: 1120.06