Pagini recente » Cod sursa (job #408526) | Cod sursa (job #3267310) | Cod sursa (job #931709) | Cod sursa (job #2174797) | Cod sursa (job #997977)
Cod sursa(job #997977)
#include <fstream>
#include <string>
using namespace std;
int p;
string ex;
int eval();
int factor();
int termen();
int termen(){
int t = factor();
while(ex[p] == '/' || ex[p] == '*'){
++p;
if(ex[p - 1] == '/')
t /= factor();
else
t *= factor();
}
return t;
}
int eval(){
int t = termen();
while(ex[p] == '-' || ex[p] == '+'){
++p;
if(ex[p - 1] == '-')
t -= termen();
else
t += termen();
}
return t;
}
int factor(){
int t = 0;
if(ex[p] == '('){
++p;
t = eval();
++p;
return t;
}
while(ex[p] >= '0' && ex[p] <= '9'){
t = t * 10 + ex[p] - '0';
++p;
}
return t;
}
int main(){
ifstream in("evaluare.in");
ofstream out("evaluare.out");
in >> ex;
ex.push_back('$');
int ans = eval();
out << ans;
return 0;
}