fork(3) download
  1. #include <iostream>
  2. #include <stack>
  3.  
  4. using namespace std;
  5.  
  6. bool CheckNumber(string str)
  7. {
  8. int i = 0;
  9. bool flag = true;
  10. bool dot = false;
  11. while (str[i])
  12. {
  13. if (str[i] < 48 || str[i] > 57)
  14. {
  15. if (str[i] != '.')
  16. return false;
  17. if (dot && str[i] == '.')
  18. return false;
  19. if (i!=0 && str[i] == '-')
  20. return false;
  21. }
  22. if (str[i] == '.')
  23. dot = true;
  24. ++i;
  25. }
  26. return true;
  27. }
  28.  
  29. int main()
  30. {
  31. string c;
  32. float a, b;
  33. stack<float> st;
  34. bool flag = false;
  35. while (cin >> c)
  36. {
  37. if (c == "+" || c == "-" || c == "*" || c == "/")
  38. {
  39. if (!st.empty())
  40. {
  41. a = st.top();
  42. st.pop();
  43. if (!st.empty())
  44. {
  45. b = st.top();
  46. st.pop();
  47. if (c == "+")
  48. st.push(b + a);
  49. else if (c == "-")
  50. st.push(b - a);
  51. else if (c == "*")
  52. st.push(b * a);
  53. else
  54. {
  55. if (a != 0)
  56. st.push(b / a);
  57. else
  58. flag = true;
  59. }
  60. }
  61. else
  62. flag = true;
  63. }
  64. else
  65. flag = true;
  66. }
  67. else
  68. {
  69. if (CheckNumber(c))
  70. st.push(stof(c));
  71. else
  72. flag = true;
  73. }
  74. if (cin.peek() == 10 )
  75. {
  76. if (st.size() == 1 && !flag)
  77. printf("%.4f\n", st.top());
  78. else
  79. cout << "ERROR" << endl;
  80. while (!st.empty())
  81. st.pop();
  82. flag = false;
  83. }
  84. }
  85. if (st.size() == 1 && !flag)
  86. printf("%.4f\n", st.top());
  87. else
  88. cout << "ERROR" << endl;
  89. while (!st.empty())
  90. st.pop();
  91. return 0;
  92. }
Success #stdin #stdout 0s 15240KB
stdin
1 2 3 /
2 3 /
3 4 * /
1 2 4 + - 5 * 7 /
stdout
ERROR
0.6667
ERROR
-3.5714