Pagini recente » Cod sursa (job #890291) | Cod sursa (job #364708) | Cod sursa (job #1235609) | Cod sursa (job #761624) | Cod sursa (job #792724)
Cod sursa(job #792724)
#include <cstdio>
#include <cstring>
#include <cmath>
char expr[100010];
char *p = expr;
int lmax = 3;
char op[4][4] = {"+-",
"*/",
"^",
""};
long operatie(long x, long y, char o) {
switch (o) {
case '+': return x+y;
case '-': return x-y;
case '*': return x*y;
case '/': return x/y;
case '^': return pow(x,y);
}
return 0;
}
long eval(int l) {
int r = 0;
if (l == lmax) {
if (*p == '(') ++p, r=eval(0), ++p;
else
while (strchr("1234567890", *p))
r = r*10 + *(++p-1) - '0';
} else r = eval(l+1);
while (strchr(op[l], *p))
r = operatie(r, eval(l+1), *(++p-1));
return r;
}
int main () {
freopen("evaluare.in","rt",stdin);
freopen("evaluare.out","wt",stdout);
fgets(expr, 100010, stdin);
printf("%ld", eval(0));
return 0;
}