#include #include using namespace std; int iss; char is[200000][20]; int x[200000]; int st[2000]; int ss; void ps1(int i){ printf("przed operacja %d %s\n",i,is[i]); for(int i=0;i0){ ss--; return true; }else{ return false; } } bool inv(){ if(ss>0){ st[ss-1]*=-1; return true; }else{ return false; } } bool dup(){ if(ss!=1000 && ss>0){ st[ss]=st[ss-1]; ss++; return true; }else{ return false; } } bool swp(){ if(ss>1){ st[ss-1]^=st[ss-2]^=st[ss-1]^=st[ss-1]; return true; }else{ return false; } } bool add(){ if(ss>1){ st[ss-2]=st[ss-2]+st[ss-1]; if(st[ss-2]>1000000000 || st[ss-2]<-1000000000){ return false; } ss--; return true; }else{ return false; } } bool sub(){ if(ss>1){ st[ss-2]=st[ss-1]-st[ss-2]; if(st[ss-2]>1000000000 || st[ss-2]<-1000000000){ return false; } ss--; return true; }else{ return false; } } bool mul(){ if(ss>1){ long long m = (long long)st[ss-1]*(long long)st[ss-2]; if(m>1000000000 || m<-1000000000){ return false; } st[ss-2]=m; ss--; return true; }else{ return false; } } bool div(){ if(ss>1 && st[ss-1]!=0){ st[ss-2]=st[ss-2]/st[ss-1]; ss--; return true; }else{ return false; } } bool mod(){ if(ss>1 && st[ss-1]!=0){ st[ss-2]=st[ss-2]%st[ss-1]; ss--; return true; }else{ return false; } } void run(int v){ num(v); for(int i=0;i=0;i++){ bool czy=true; //ps1(i); if(strcmp(is[i],"NUM")==0){ czy=num(x[i]); }else if(strcmp(is[i],"POP")==0){ czy=pop(); }else if(strcmp(is[i],"INV")==0){ czy=inv(); }else if(strcmp(is[i],"DUP")==0){ czy=dup(); }else if(strcmp(is[i],"SWP")==0){ czy=swp(); }else if(strcmp(is[i],"ADD")==0){ czy=add(); }else if(strcmp(is[i],"SUB")==0){ czy=sub(); }else if(strcmp(is[i],"MUL")==0){ czy=mul(); }else if(strcmp(is[i],"DIV")==0){ czy=div(); }else if(strcmp(is[i],"MOD")==0){ czy=mod(); }else{ ss=-1; } if(czy==false){ ss=-1; } //ps2(i); } } int main(){ while(true){ scanf("%s",is[0]); //printf("wczytano: %s\n",is[0]); if(strcmp(is[0],"QUIT")==0){ return 0; } if(strcmp(is[0],"NUM")==0){ int tmp; scanf("%d",&tmp); x[0]=tmp; //printf("jest liczba %d\n",tmp); } int i=0; while(strcmp(is[i],"END")!=0){ //printf("---dla i=%d wczytano %s\n",i,is[i]); i++; scanf("%s",is[i]); //printf("dla i=%d wczytano %s\n",i,is[i]); if(strcmp(is[i],"NUM")==0){ int tmp; scanf("%d",&tmp); x[i]=tmp; //printf("jest liczba %d\n",tmp); } } iss=i; int n; scanf("%d",&n); //printf("bedzie %d zestawow\n",n); for(int z =0;z