Pagini recente » Cod sursa (job #2622941) | Cod sursa (job #2402651) | Cod sursa (job #2623838) | Cod sursa (job #1395693) | Cod sursa (job #1154455)
#include<cstdio>
#include<cstring>
#include<algorithm>
#define M -2000000000
#include<queue>
using namespace std;
FILE *f=fopen("evaluare.in","r");
FILE *g=fopen("evaluare.out","w");
char aux[100002],c;
struct kkt
{
int pr;
int pozz;
}auxx;
struct cmp
{
bool operator() (kkt a,kkt b)
{
if(a.pr<b.pr) return true;
else if(a.pr==b.pr) return a.pozz>b.pozz;
else return false;
}
};
priority_queue<kkt,vector<kkt>,cmp>v;
int p,val[100002],k,nr,kk;
int valoare(int a,char c,int b)
{
if(c=='+') return a+b;
if(c=='/') return a/b;
if(c=='*') return a*b;
if(c=='-') return a-b;
return 0;
}
int construire()
{
int poz,j,i,sol;
while(!v.empty())
{
poz=v.top().pozz;
i=poz;
while(val[i]==M)i--;
j=poz;
while(val[j]==M)j++;
val[poz]=valoare(val[i],aux[poz],val[j]);
sol=val[poz];
v.pop();
val[i]=M;
val[j]=M;
}
return sol;
}
int main()
{
unsigned int i;
for(i=1;i<=100001;i++)val[i]=M;
fscanf(f,"%c",&c);
while(!feof(f))
{
if('0'<=c&&c<='9')
{
nr=0;
while('0'<=c&&c<='9'&&!feof(f))
{
nr=nr*10+c-'0';fscanf(f,"%c",&c);
}
++k;
val[k]=nr;
}
else fscanf(f,"%c",&c);
if(c=='-'||c=='+')
{k++;
auxx.pozz=k;
auxx.pr=p+1;
v.push(auxx);
aux[k]=c;
}
if(c=='*'||c=='/')
{
k++;
auxx.pozz=k;
auxx.pr=p+2;
v.push(auxx);
aux[k]=c;
}
if(c=='('){p+=3;++k;}
if(c==')'){p-=3;++k;}
}
fprintf(g,"%d",construire());
return 0;
}