Pagini recente » Cod sursa (job #386735) | Cod sursa (job #797115) | Cod sursa (job #1276818) | Cod sursa (job #1845468) | Cod sursa (job #373595)
Cod sursa(job #373595)
#include<fstream>
using namespace std;
ifstream f("evaluare.in");
ofstream g("evaluare.out");
char c;
int a[100001],p,u,nr,ok;
char b[100001],d[256];
void calc(){
if(b[u]=='+')
{ a[p-1]=a[p]+a[p-1];
p--;u--;
return;
}
if(b[u]=='-')
{ a[p-1]=a[p-1]-a[p];
p--;u--;
return;
}
if(b[u]=='/')
{ a[p-1]=a[p-1]/a[p];
p--;u--;
return;
}
if(b[u]=='*')
{ a[p-1]=a[p-1]*a[p];
p--;u--;
return;
}
}
int main(){
d['(']=0;
d['+']=d['-']=1;
d['*']=d['/']=2;
p=u=0;
nr=0;ok=0;
while(!f.eof()){
c=f.get();
if(!f.eof()&&c!='\n'){
if(c>='0'&&c<='9')
{ ok=1; nr=nr*10+c-'0';}
else{ if(ok==1){
p++;
a[p]=nr;
nr=0; ok=0;}
if(c==')'){
while(b[u]!='(')
calc();
u--;
}
else if(d[c]>d[b[u]]||u==0||c=='('){
u++;
b[u]=c;
}
else {while(d[c]<=d[b[u]]&&u>0)
calc();
u++;
b[u]=c;
}
}
}
}
if(ok==1)
{ p++;
a[p]=nr;
}
while(u>0)
calc();
g<<a[1];
return 0;
}