Cod sursa(job #1029120)
Utilizator | Tudor Buzu buzu.tudor67 | Data | 15 noiembrie 2013 00:30:00 |
---|---|---|---|
Problema | Evaluarea unei expresii | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 1.53 kb |
#include<fstream>
#include<cctype>
using namespace std;
ifstream fi("evaluare.in");
ofstream fo("evaluare.out");
char s[100005];
int i=0;
int expresie();
int factor(){
int x=0;
if (s[i]=='('){
i++;
x=expresie();
i++;
}
else while (isdigit(s[i])) {
x=x*10+s[i]-48;
i++;
}
return x;
}
int termen(){
int y;
y=factor();
while (s[i]=='*' || s[i]=='/') if (s[i]=='*') {
i++;
y*=factor();
}
else {
i++;
y/=factor();
}
return y;
}
int expresie(){
int z;
z=termen();
while (s[i]=='+' || s[i]=='-') if (s[i]=='+') {
i++;
z+=termen();
}
else {
i++;
z-=termen();
}
return z;
}
int main(){
fi>>s;
fo<<expresie();
fi.close();
fo.close();
return 0;
}