Cod sursa(job #1359459)

Utilizator viuscenkoViktor Iuscenko viuscenko Data 24 februarie 2015 22:47:18
Problema Evaluarea unei expresii Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.44 kb
#include <bits/stdc++.h>

using namespace std;

#define     mp              make_pair
#define     fs              first
#define     sc              second
#define     pob             pop_back
#define     pub             push_back
#define     eps             1E-7
#define     sz(a)           a.size()
#define     count_one       __builtin_popcount;
#define     count_onell     __builtin_popcountll;
#define     fastIO          ios_base::sync_with_stdio(false)
#define     PI              (acos(-1.0))
#define     linf            (1LL<<62)//>4e18
#define     inf             (0x7f7f7f7f)//>2e9

#define DEBUG 1
#ifdef DEBUG
#define D(x) x
#else
#define D(x)
#endif

char in[100010], *p;

ifstream f("evaluare.in");
ofstream g("evaluare.out");

char pr[4][4] = { "+-", "*/", "", "" };

int eval(int a, int b, int op) {
    switch(op) {
        case '+': return a + b;
        case '-': return a - b;
        case '*': return a * b;
        case '/': return a / b;
    }
}

int solve(int prior) {
    int x, y;

    if( prior == 2 )
        if(*p == '(') {
            p++; x = solve(0), p++;
        } else
            for(x = 0; isdigit(*p); p++) {
                x = x * 10 + *p - '0';
            }
    else
        for(x = solve(prior + 1); strchr(pr[prior], *p); x = y)
            y = eval(x, solve(prior+1), *p++);
    return x;
}

int main() {
    f >> in;
    p = in;

    g << solve(0);

    return 0;
}