Pagini recente » Cod sursa (job #1436538) | Cod sursa (job #1367880) | Cod sursa (job #693986) | Cod sursa (job #1090497) | Cod sursa (job #194411)
Cod sursa(job #194411)
#include <cstdio>
#include <cstring>
const char FILEIN[] = "evaluare.in";
const char FILEOUT[] = "evaluare.out";
const int MAXN = 100005;
int N;
char s[MAXN];
int p;
int Expresie();
int Numar() {
int ret = 0;
while (p < N && (s[p] >= '0' && s[p] <= '9'))
ret = ret*10 + s[p++]-'0';
return ret;
}
int Factor() {
int ret;
if (s[p] == '(') ++p, ret = Expresie(), ++p;
else ret = Numar();
if (p < N && s[p] == '*')
++p, ret *= Factor();
if (p < N && s[p] == '/')
++p, ret /= Factor();
return ret;
}
int Expresie() {
int ret = Factor();
if (p < N && s[p] == '+')
++p, ret += Expresie();
if (p < N && s[p] == '-')
++p, ret -= Expresie();
return ret;
}
int main() {
freopen(FILEIN, "r", stdin);
freopen(FILEOUT, "w", stdout);
scanf("%s", s);
N = strlen(s);
printf("%d\n", Expresie());
}