浏览代码

添加 'B.cpp'

webturing 5 年之前
父节点
当前提交
b29bc579fe
共有 1 个文件被更改,包括 31 次插入0 次删除
  1. 31 0
      B.cpp

+ 31 - 0
B.cpp

@@ -0,0 +1,31 @@
+#include <bits/stdc++.h>
+using namespace std;
+int main() {
+  stack<double> S;
+  string x;
+  while (cin >> x) {
+    istringstream iss(x);
+    double r;
+    if (iss >> r) {
+      S.push(r);
+    } else {
+      double second = S.top();
+      S.pop();
+      double first = S.top();
+      S.pop();
+      double res = first;
+      if (x[0] == '+') {
+        res += second;
+      } else if (x[0] == '-') {
+        res -= second;
+      } else if (x[0] == '*') {
+        res *= second;
+      } else {
+        res /= second;
+      }
+      S.push(res);
+    }
+  }
+  cout << fixed << setprecision(2) << S.top() << endl;
+  return 0;
+}