Cod sursa(job #1825961)

Utilizator andru47Stefanescu Andru andru47 Data 9 decembrie 2016 21:50:06
Problema Evaluarea unei expresii Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.03 kb
#include <bits/stdc++.h>
using namespace std;
char s[100005];
int where=1,len;
inline int solve();
inline int op();
inline int numar()
{
    int ret = 0;
    if (s[where] == '(')
    {
        ++where;
        ret = solve();
        ++where;
        return ret;
    }
    while(isdigit(s[where]))
        ret = ret*10 + (s[where] -'0'),++where;
    return ret;

}
inline int op()
{
    int ret = numar();
    while(s[where]=='*'||s[where]=='/')
    {
        if (s[where]=='*')
            ++where,
            ret *= numar();
        else ++where,ret/=numar();

    }
    return ret;
}
inline int solve()
{
    int ret = op();
    while(s[where]=='+'||s[where]=='-')
    {
        if (s[where]=='+')
            ++where,ret += op();
        else if (s[where]=='-')
            ++where,ret-= op();
    }
    return ret;
}
int main()
{
    freopen("evaluare.in","r",stdin);
    freopen("evaluare.out","w",stdout);
    gets(s+1);
    len = strlen(s+1);
    printf("%d\n", solve());

    return 0;
}