Pagini recente » Cod sursa (job #112166) | Cod sursa (job #856692) | Cod sursa (job #302620) | Cod sursa (job #1594804) | Cod sursa (job #663698)
Cod sursa(job #663698)
#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;
}