B.cpp 619 B

12345678910111213141516171819202122232425262728293031
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4. stack<double> S;
  5. string x;
  6. while (cin >> x) {
  7. istringstream iss(x);
  8. double r;
  9. if (iss >> r) {
  10. S.push(r);
  11. } else {
  12. double second = S.top();
  13. S.pop();
  14. double first = S.top();
  15. S.pop();
  16. double res = first;
  17. if (x[0] == '+') {
  18. res += second;
  19. } else if (x[0] == '-') {
  20. res -= second;
  21. } else if (x[0] == '*') {
  22. res *= second;
  23. } else {
  24. res /= second;
  25. }
  26. S.push(res);
  27. }
  28. }
  29. cout << fixed << setprecision(2) << S.top() << endl;
  30. return 0;
  31. }