Pagini recente » Cod sursa (job #2663629) | Cod sursa (job #2081506) | Cod sursa (job #322937) | Cod sursa (job #218563) | Cod sursa (job #2757654)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
using namespace std;
ifstream in("evaluare.in");
ofstream out("evaluare.out");
string e;
int p;
int evaluate();
int term();
int factor();
int evaluate() {
int x = term();
while (e[p] == '+' || e[p] == '-') {
if (e[p] == '+')
p++, x += term();
else
p++, x -= term();
}
return x;
}
int term() {
int x = factor();
while (e[p] == '*' || e[p] == '/') {
if (e[p] == '*')
p++, x *= factor();
else
p++, x /= factor();
}
return x;
}
int factor() {
int x = 0;
if (e[p] == '(') {
p++;
x = evaluate();
p++;
}
else {
x = 0;
while (e[p] >= '0' && e[p] <= '9') {
x = x * 10 + e[p] - '0';
p++;
}
}
return x;
}
int main() {
ios_base::sync_with_stdio(false);
in.tie(NULL), out.tie(NULL);
in >> e;
out << evaluate();
return 0;
}