Advertisement
SorahISA

A. 顛倒國 (reverse)

Oct 5th, 2022
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5.     string formula;
  6.     getline(cin, formula);
  7.     reverse(formula.begin(), formula.end());
  8.    
  9.     stringstream ss;
  10.     string formula_add_sub, L, op, R;
  11.    
  12.     ss << formula;
  13.     ss >> L;
  14.     while (ss >> op >> R) {
  15.         if (op == "+" or op == "-") formula_add_sub += L + " " + op + " ", L = R;
  16.         else if (op == "*")  L = to_string( stoi(L) * stoi(R) );
  17.         else if (op == "\\") L = to_string( stoi(L) / stoi(R) );
  18.     }
  19.     formula_add_sub += L;
  20.    
  21.     ss.clear();
  22.     ss << formula_add_sub;
  23.     ss >> L;
  24.     while (ss >> op >> R) {
  25.              if (op == "+") L = to_string( stoi(L) + stoi(R) );
  26.         else if (op == "-") L = to_string( stoi(L) - stoi(R) );
  27.     }
  28.    
  29.     cout << L << "\n";
  30.    
  31.     return 0;
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement