Pagini recente » Cod sursa (job #2543811) | Cod sursa (job #2576829) | Cod sursa (job #2516155) | Cod sursa (job #3144715) | Cod sursa (job #2324505)
#include <cstdio>
#include <cstring>
using namespace std;
const int MAX = 100001,
HMAX = 2;
char S[MAX], *p = S,
operand[4][4] = {"+-", "*/", "^", ""};
int termen();
int factor();
int op(int x, int y, char c) {
switch ( c ) {
case '+': return x+y;
case '-': return x-y;
case '*': return x*y;
case '/': return x/y;
}
return 0;
}
int eval(int);
int elem() {
int r = 0;
if(*p == '(')
++p, r = eval(0), ++p;
else
while(*p >= '0' && *p <= '9')
r = r * 10 + *p - '0', ++p;
return r;
}
int eval(int h) {
int r = (h == HMAX) ? elem() : eval(h+1);
while(strchr(operand[h], *p))
r = op(r, eval(h+1), *(++p-1));
return r;
}
int main()
{
freopen("evaluare.in", "r", stdin);
freopen("evaluare.out", "w", stdout);
fgets(S, MAX, stdin);
printf("%i", eval(0));
return 0;
}