Pagini recente » Cod sursa (job #1156498) | Cod sursa (job #2716614) | Cod sursa (job #2334053) | Cod sursa (job #1260271) | Cod sursa (job #2303531)
#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];
int pr(char x){
if(x == '(')
return 2;
if(x == '-' || x== '+')
return 1;
return 0;
}
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();
}
void backt(int k){
int i, s;
if(k > n){
s = pol();
if(s > mx)
mx = s;
}
else{
for(i = 1;i <= 3;i++){
sk[k] = i;
backt(k + 1);
}
}
}
int main()
{
freopen("evaluare.in", "r", stdin);
freopen("evaluare.out", "w", stdout);
int i;
scanf("%s", s);
printf("%d", pol());
return 0;
}