Pagini recente » Cod sursa (job #2337091) | Profil andrici_cezar | Cod sursa (job #3214849) | Cod sursa (job #2215872) | Cod sursa (job #2303552)
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;
char s[200005];
stack <int> nr;
stack <int> st;
int n, sk[100005], mx = -2000000;
char f[1000005];
inline int pr(char x){
if(x == '(')
return 2;
if(x == '-' || x== '+')
return 1;
return 0;
}
inline int calc(int a, int b, char c){
if(c == '+'){
return a + b;
}
if(c == '-'){
return a - b;
}
if(c == '*'){
return a * b;
}
if(c == '/'){
if(b != 0)
return a / b;
return 0;
}
}
int pol(){
int i, k, a, sum;
char sm;
for(i = 0;i < strlen(s);++i){
if(s[i] >= '0' && s[i] <= '9'){
sum = 0;
while(s[i] >= '0' && s[i] <= '9'){
sum = sum * 10 + s[i] - '0';
i++;
}
i--;
nr.push(sum);
}
else
if(s[i] == '('){
st.push('(');
}
else
if(s[i] == ')'){
while(st.top() != '('){
sum = nr.top();
sm = st.top();
st.pop();
nr.pop();
if(sm == '/' && sum == 0)
return 0;
sum = calc(nr.top(), sum, sm);
nr.pop();
nr.push(sum);
}
st.pop();
}
else{
while(!st.empty() && pr(s[i]) >= pr(st.top())){
sum = nr.top();
sm = st.top();
st.pop();
nr.pop();
if(sm == '/' && sum == 0)
return 0;
sum = calc(nr.top(), sum, sm);
nr.pop();
nr.push(sum);
}
st.push(s[i]);
}
}
while(!st.empty()){
sum = nr.top();
sm = st.top();
st.pop();
nr.pop();
if(sm == '/' && sum == 0)
return 0;
sum = calc(nr.top(), sum, sm);
nr.pop();
nr.push(sum);
}
return nr.top();
}
int main()
{
freopen("evaluare.in", "r", stdin);
freopen("evaluare.out", "w", stdout);
int i;
scanf("%s", s);
printf("%d", pol());
return 0;
}