Pagini recente » Infoarena Monthly 2012 - Runda 9, Clasament | Cod sursa (job #3174452) | Cod sursa (job #1930123) | Cod sursa (job #1935173) | Cod sursa (job #2771426)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
string s;
int pos;
int fact();
int term();
int eval();
int fact() {
int ret;
if(s[pos] == '(') {
pos++;
ret = eval();
pos++;
} else {
ret = 0;
for(; '0' <= s[pos] && s[pos] <= '9'; ) {
ret = ret * 10 + (s[pos++] - '0');
}
}
return ret;
}
int term() {
int ret = fact();
for(; s[pos] == '*' || s[pos] == '/'; ) {
if(s[pos++] == '*') {
ret *= fact();
} else {
ret /= fact();
}
}
return ret;
}
int eval() {
int ret = term();
for(; s[pos] == '+' || s[pos] == '-'; ) {
if(s[pos++] == '+') {
ret += term();
} else {
ret -= term();
}
}
return ret;
}
int main() {
fin >> s;
fout << eval() << '\n';
return 0;
}