Cod sursa(job #1376293)

Utilizator Al3ks1002Alex Cociorva Al3ks1002 Data 5 martie 2015 16:57:28
Problema Evaluarea unei expresii Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.3 kb
#include<cstdio>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<bitset>
#include<deque>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<unordered_map>

#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second

using namespace std;

const int nmax = 100005;

int e(), t(), f();
char s[nmax], *p;

int main()
{
    freopen("evaluare.in", "r", stdin);
    freopen("evaluare.out", "w", stdout);

    scanf("%s", s);
    p = s;
    printf("%d\n", e());

    return 0;
}

int e()
{
    int r = t();

    while(*p == '+' || *p == '-')
    {
        if(*p == '+') p++, r += t();
        else p++, r -= t();
    }

    return r;
}

int t()
{
    int r = f();

    while(*p == '*' || *p == '/')
    {
        if(*p == '*') p++, r *= f();
        else p++, r /= f();
    }

    return r;
}

int f()
{
    int r = 0;

    if(*p == '(')
    {
        p++;
        r = e();
        p++;
        return r;
    }

    while(*p >= '0' && *p <= '9')
        r = r * 10 + *p - '0', p++;

    return r;
}