Salvens

B

Jul 27th, 2023
762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <array>
  2. #include <iostream>
  3. #include <vector>
  4. #include <stack>
  5.  
  6. using namespace std;
  7.  
  8. #define int long long
  9.  
  10. const long long INF = 1e18 + 7;
  11. const int MAXN = 2e5 + 10;
  12. const int N = 2e5;
  13.  
  14. int make_op(int a, int b, char op) {
  15.     if (op == '+') {
  16.         return a + b;
  17.     }
  18.     if (op == '-') {
  19.         return a - b;
  20.     }
  21.     if (op == '*') {
  22.         return a * b;
  23.     }
  24. }
  25.  
  26. int convert(string& s) {
  27.     int ans = 0;
  28.     for (auto& i: s) {
  29.         ans *= 10;
  30.         ans += (int)(i - '0');
  31.     }
  32.     return ans;
  33. }
  34.  
  35. signed main() {
  36.     ios_base::sync_with_stdio(false);
  37.     cin.tie(nullptr);
  38.     cout.tie(nullptr);
  39.     stack<int> st;
  40.     string s;
  41.     while (cin >> s) {
  42.         if (s[0] >= '0' && s[0] <= '9') {
  43.             st.push(convert(s));
  44.         } else {
  45.             int a = st.top();
  46.             st.pop();
  47.             int b = st.top();
  48.             st.pop();
  49.             swap(a, b);
  50.             st.push(make_op(a, b, s[0]));
  51.         }
  52.     }
  53.     cout << st.top() << '\n';
  54. }
Advertisement
Add Comment
Please, Sign In to add comment