Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. //#define _GLIBCXX_DEBUG
  2. #include <bits/stdc++.h>
  3. #pragma GCC optimize("O3")
  4. #define count masloriy1489
  5. #define y1 jhgfds
  6. #define prev maAslo
  7. #define ll long long
  8. #define ld long double
  9. #define pb push_back
  10. #define eb emplace_back
  11. #define all(v) v.begin(), v.end()
  12. #define rep(i, n) for(int i = 0; i < n; i++)
  13.  
  14. using namespace std;
  15. //freopen("input.txt", "r", stdin);
  16. //freopen("output.txt", "w", stdout);
  17. //cout << clock()*1000/CLOCKS_PER_SEC << '\n';
  18.  
  19. stack<int> num;
  20. stack<char> op;
  21.  
  22. inline void make(){
  23. int a = num.top();
  24. num.pop();
  25. int b = num.top();
  26. num.pop();
  27. char c = op.top();
  28. op.pop();
  29. if(c == '+')
  30. num.push(a + b);
  31. else
  32. num.push(b - a);
  33. }
  34.  
  35. int32_t main(){
  36. ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  37. //freopen("evalpm.in", "r", stdin);
  38. //freopen("evalpm.out", "w", stdout);
  39. int curr = 0;
  40. string s;
  41. cin >> s;
  42. bool f = false;
  43. op.push('(');
  44. s += ')';
  45. for(char c : s){
  46. if(c >= '0' && c <= '9'){
  47. curr = curr*10 + (c - '0');
  48. f = true;
  49. }else{
  50. if(f){
  51. num.push(curr);
  52. f = false;
  53. curr = 0;
  54. }
  55. if(c == ')'){
  56. while(!op.empty() && op.top() != '('){
  57. make();
  58. }
  59. if(!op.empty())
  60. op.pop();
  61. if(!op.empty()){
  62. if(op.top() == '-'){
  63. op.pop();
  64. op.push('+');
  65. int g = num.top();
  66. num.pop();
  67. num.push(-g);
  68. }
  69. }
  70. }else{
  71. op.push(c);
  72. }
  73. }
  74. }
  75. cout << num.top();
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement