Pagini recente » Cod sursa (job #2069643) | Cod sursa (job #2922302) | Cod sursa (job #162476) | Cod sursa (job #200413) | Cod sursa (job #145587)
Cod sursa(job #145587)
#include <iostream>
#define FIN "evaluare.in"
#define FOUT "evaluare.out"
using namespace std;
int lop=0;
int drez=0,rez[10000];
char op[100000];
void add_op(int op){
if (op=='+'){
--drez;
rez[drez]+=rez[drez+1];
} else if (op=='-'){
--drez;
rez[drez]-=rez[drez+1];
} else if (op=='*'){
--drez;
rez[drez]*=rez[drez+1];
} else {
--drez;
rez[drez]/=rez[drez+1];
}
}
void add_val(int vl){
rez[++drez]=vl;
}
void erase(void){
while (lop>=1 && op[lop]!='('){
add_op(op[lop]);
lop--;}
}
void iofile(void){
lop=0;
freopen(FIN,"rt",stdin);
freopen(FOUT,"wt",stdout);
int c=11,x=0;
for (c=getchar();c!='\n';c=getchar()){
if (c>='0' && c<='9'){x=x*10+c-'0';} else{
if (x){add_val(x);}
x=0;
if (c=='('){op[++lop]=c;} else
if (c=='*'){op[++lop]=c;} else
if (c=='/'){op[++lop]=c;} else
if (c=='-' || c=='+'){
if (lop>0 && (op[lop]=='*' || op[lop]=='/')){
erase();}
op[++lop]=c;
} else if (c==')'){
erase();
lop--;}
}
}
if (x){add_val(x);}
if (lop){erase();}
printf("%d\n",rez[1]);
fclose(stdin);
fclose(stdout);
}
int main(void){
iofile();
return 0;
}