Pagini recente » Monitorul de evaluare | Cod sursa (job #284996) | Profil Djok | Autentificare | Cod sursa (job #205770)
Cod sursa(job #205770)
#include<stdio.h>
#include<string.h>
#define N 100100
#define marc 2000000
inline bool cifra(char c){
return c>='0' && c<='9';
}
inline bool op(char c){
return c=='+' || c== '-' || c=='*' || c=='/';
}
inline int priori(char c){
if(c=='+' || c=='-')
return 1;
if(c=='*' || c=='/')
return 2;
return 0;
}
inline int oper(char op,int x,int y){
if(op=='+')
return x+y;
if(op=='-')
return x-y;
if(op=='*')
return x*y;
return x/y;
}
int numar(char *&s){
int val=0;
while(cifra(*s)){
val=val*10+(int)(*s-'0');
++s;
}
return val;
}
void final(int &vfi,int *sti,int &vfc,char *stc){
int rez;
while(vfc && stc[vfc]!='('){
rez=oper(stc[vfc],sti[vfi-1],sti[vfi]);
sti[--vfi]=rez;
--vfc;
}
--vfc;
}
void calc(char *&s,int &vfi,int *sti,int &vfc,char *stc){
if(vfc==0 || priori(*s)>priori(stc[vfc])){
stc[++vfc]=*s;
++s;
return;
}
char c=stc[vfc];
int rez=oper(c,sti[vfi-1],sti[vfi]);
sti[--vfi]=rez;
stc[vfc]=*s;
++s;
}
int calcul(char *s){
int vfi=0,vfc=0,sti[N];
char stc[N];
while(*s!='\n'){
if(cifra(*s))
sti[++vfi]=numar(s);
else{
if(*s=='('){
stc[++vfc]=*s;
++s;
}
else
if(*s==')'){
final(vfi,sti,vfc,stc);
++s;
}
else
if(op(*s))
calc(s,vfi,sti,vfc,stc);
}
}
final(vfi,sti,vfc,stc);
return sti[1];
}
int main(){
freopen("evaluare.in","r",stdin);
freopen("evaluare.out","w",stdout);
char s[N];
fgets(s,N,stdin);
printf("%d\n",calcul(s));
return 0;
}