Pagini recente » Cod sursa (job #1278444) | Cod sursa (job #1188181) | Cod sursa (job #1776079) | Cod sursa (job #75837) | Cod sursa (job #3196443)
#include <fstream>
using namespace std;
ifstream cin("evaluare.in");
ofstream cout("evaluare.out");
/// evaluarea unei expresii folosind recursivitate indirecta
int i;
char s[100001];
int expresie();
int termen();
int factor();
int constanta();
expresie(){
int r=termen();
while(s[i]=='+'||s[i]=='-'){
if(s[i]=='+'){
i++;
r+=termen();
}else{
i++;
r-=termen();
}
}
return r;
}
termen(){
int r=factor();
while(s[i]=='*'||s[i]=='/'){
if(s[i]=='*'){
i++;
r*=factor();
}else{
i++;
r/=factor();
}
}
return r;
}
factor(){
int r;
if(s[i]=='('){
i++;
r=expresie();
i++; /// sar peste paranteza inchisa
}else
r=constanta();
return r;
}
constanta(){
int r=0;
while(s[i]>='0'&&s[i]<='9'){
r=r*10+s[i]-'0';
i++;
}
return r;
}
int main()
{
cin>>s;
cout<<expresie();
return 0;
}