Pagini recente » Cod sursa (job #1194556) | Cod sursa (job #805110) | Cod sursa (job #3211640) | Cod sursa (job #2748098) | Cod sursa (job #1874879)
#include <bits/stdc++.h>
using namespace std;
int N, p;
string str;
int factor();
int termen();
int eval() {
int ans = termen();
while (p < N && (str[p] == '+' || str[p] == '-'))
if (str[p] == '+') {
++ p;
ans += termen();
}
else {
++ p;
ans -= termen();
}
return ans;
}
int termen() {
int ans = factor();
while (p < N && (str[p] == '*' || str[p] == '/'))
if (str[p] == '*') {
++ p;
ans *= termen();
}
else {
++ p;
ans /= termen();
}
return ans;
}
int factor() {
int ans = 0;
if (p < N && str[p] == '(') {
++ p; //Trecem peste (
ans = eval();
++ p; //Trecem peste )
}
else {
while (p < N && isdigit(str[p])) {
ans *= 10;
ans += (str[p] - '0');
p ++;
}
}
return ans;
}
int main() {
ifstream cin("evaluare.in");
ofstream cout("evaluare.out");
cin >> str;
N = str.size();
p = 0;
cout << eval() << '\n';
return 0;
}