Cod sursa(job #664783)

Utilizator timeiutzaTimea Balan timeiutza Data 20 ianuarie 2012 20:00:57
Problema Evaluarea unei expresii Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.73 kb
#include <cstdio>
#include <cstring>
char s[100005];
char *p = s , ope[4][3] = {"+-","*/","r"} , *cifre = "0123456789";
int eval(int h);
int operatie(int x,int y,char s)
{
if(s == '+') return x + y;
if(s == '-') return x - y;
if(s == '/') return x/y;
if(s == '*') return x*y;
return  0;
}
int elem()
{
int ans = 0;
if(*p == '(') p++ , ans = eval(0) , p++;
else while(strchr(cifre,*p)) ans = ans * 10 + *(++p - 1) - '0';
return ans;
}
int eval(int h)
{
int t = h == 2 ? elem() : eval(h + 1);
while(strchr(ope[h],*p)) t = operatie(t,eval(h + 1),*(++p - 1));
return t;
}
int main()
{
freopen("evaluare.in","r",stdin);
freopen("evaluare.out","w",stdout);
fgets(s,100005,stdin);
printf("%d\n",eval(0));
return 0;
}