Pagini recente » Cod sursa (job #2878322) | Cod sursa (job #327947) | Cod sursa (job #227333) | Cod sursa (job #1398327) | Cod sursa (job #3237528)
#include <bits/stdc++.h>
using namespace std;
ifstream fin( "evaluare.in" );
ofstream fout( "evaluare.out" );
const int DIM = 1e5 + 1;
string s;
int pos, n;
int eval();
int term();
int fact();
int eval() {
int res = term();
while ( pos < n && (s[pos] == '+' || s[pos] == '-') ) {
if ( s[pos] == '+' ) {
++pos;
res += term();
} else {
++pos;
res -= term();
}
}
return res;
}
int term() {
int res = fact();
while ( pos < n && (s[pos] == '*' || s[pos] == '/') ) {
if ( s[pos] == '*' ) {
++pos;
res *= fact();
} else {
++pos;
res /= fact();
}
}
return res;
}
int fact() {
int res = 0;
if ( s[pos] == '(' ) {
++pos;
res = eval();
++pos;
} else {
while ( pos < n && isdigit(s[pos]) ) {
res = res * 10 + s[pos++] - '0';
}
}
return res;
}
int main() {
ios_base::sync_with_stdio(0);
fin.tie(0);
fin >> s;
n = s.length();
fout << eval();
fin.close();
fout.close();
return 0;
}