Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define f first
- #define s second
- #define pb push_back
- #define ll long long
- #define mp make_pair
- int n;
- vector<char> oper;
- vector<int> val;
- inline void operation() {
- char c = oper.back();
- oper.pop_back();
- int b = val.back();
- val.pop_back();
- int a = val.back();
- val.pop_back();
- if (c == '*') {
- val.pb(a * b);
- }
- if (c == '/') {
- val.pb(a / b);
- }
- if (c == '+') {
- val.pb(a + b);
- }
- if (c == '-') {
- val.pb(a - b);
- }
- //cerr << c << ' ' << val.back() << endl;
- }
- int main() {
- freopen("evalhard.in", "r", stdin);
- freopen("evalhard.out", "w", stdout);
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- string s;
- cin >> s;
- s = s + ')';
- int n = s.size();
- oper.pb('(');
- for (int i = 0; i < n; ++i) {
- if ('0' <= s[i] && s[i] <= '9') {
- int v = s[i] - '0';
- while ('0' <= s[i + 1] && s[i + 1] <= '9') {
- ++i;
- v *= 10;
- v += s[i] - '0';
- }
- val.pb(v);
- }
- else {
- if (s[i] != ')') {
- if (s[i] != '(') {
- while (oper.back() != '(' && (oper.back() == '/' || oper.back() == '*' || s[i] == '+' || s[i] == '-')) {
- operation();
- }
- }
- oper.pb(s[i]);
- }
- else {
- while (oper.back() != '(') {
- operation();
- }
- oper.pop_back();
- }
- }
- }
- assert(val.size() == 1);
- assert(oper.size() == 0);
- cout << val.back();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement