Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#define _GLIBCXX_DEBUG
- #include <bits/stdc++.h>
- #pragma GCC optimize("O3")
- #define count masloriy1489
- #define y1 jhgfds
- #define prev maAslo
- #define ll long long
- #define ld long double
- #define pb push_back
- #define eb emplace_back
- #define all(v) v.begin(), v.end()
- #define rep(i, n) for(int i = 0; i < n; i++)
- using namespace std;
- //freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- //cout << clock()*1000/CLOCKS_PER_SEC << '\n';
- stack<int> num;
- stack<char> op;
- inline void make(){
- int a = num.top();
- num.pop();
- int b = num.top();
- num.pop();
- char c = op.top();
- op.pop();
- if(c == '+')
- num.push(a + b);
- else
- num.push(b - a);
- }
- int32_t main(){
- ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
- //freopen("evalpm.in", "r", stdin);
- //freopen("evalpm.out", "w", stdout);
- int curr = 0;
- string s;
- cin >> s;
- bool f = false;
- op.push('(');
- s += ')';
- for(char c : s){
- if(c >= '0' && c <= '9'){
- curr = curr*10 + (c - '0');
- f = true;
- }else{
- if(f){
- num.push(curr);
- f = false;
- curr = 0;
- }
- if(c == ')'){
- while(!op.empty() && op.top() != '('){
- make();
- }
- if(!op.empty())
- op.pop();
- if(!op.empty()){
- if(op.top() == '-'){
- op.pop();
- op.push('+');
- int g = num.top();
- num.pop();
- num.push(-g);
- }
- }
- }else{
- op.push(c);
- }
- }
- }
- cout << num.top();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement