fork download
  1. #include<bits/stdc++.h>
  2. #define int long long
  3. using namespace std;
  4. int solve(string s) {
  5. stack<int> st;
  6. int num = 0;
  7. char ops = '+';
  8. for(int i = 0; i < s.size(); i ++) {
  9. char c = s[i];
  10. if (isdigit(c)) num = num * 10 + (c - '0');
  11. if ((!isdigit(c) and c != ' ') or i == s.size() - 1){
  12. if (ops == '+') st.push(num);
  13. else if (ops == '-') st.push(-num);
  14. else if (ops == '*') {
  15. int top = st.top();
  16. st.pop();
  17. st.push(top * num);
  18. }
  19. else if (ops == '/') {
  20. int top = st.top();
  21. st.pop();
  22. st.push(top / num);
  23. }
  24. ops = c;
  25. num = 0;
  26. }
  27. }
  28. int ans = 0;
  29. while (!st.empty()) {
  30. ans += st.top();
  31. st.pop();
  32. }
  33. return ans;
  34. }
  35.  
  36. signed main() {
  37. string s;
  38. getline(cin, s);
  39. cout << solve(s);
  40. return 0;
  41. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty