Pagini recente » Cod sursa (job #2856073) | Cod sursa (job #628055) | Cod sursa (job #1878107) | Cod sursa (job #2173560) | Cod sursa (job #585553)
Cod sursa(job #585553)
#include <stdio.h>
#include <string.h>
using namespace std;
// + -2 - -3 * -4 / -5 ( -6
int i,k,l,nr,st[100001],post[100001];
char sir[100001];
int main(void)
{
freopen("evaluare.in","r",stdin);
freopen("evaluare.out","w",stdout);
scanf("%s",sir);
l=strlen(sir)-1;
for (i=0;i<=l;)
{
nr=0;
if (sir[i]>='0' && sir[i]<='9')
{
while (sir[i]>='0' && sir[i]<='9' && i<=l)
{
nr=nr*10+sir[i]-'0';
i++;
}
post[++post[0]]=nr;
continue;
}
if (sir[i]==')')
{
while (st[k]!=-6)
{
post[++post[0]]=st[k];
k--;
}
k--;
i++;
continue;
}
if (sir[i]=='+' || sir[i]=='-')
{
while (st[k]/2<=-1 && st[k]!=-6) {post[++post[0]]=st[k]; k--;}
if (sir[i]=='+') st[++k]=-2; else st[++k]=-3;
i++;
continue;
}
if (sir[i]=='(') {st[++k]=-6; i++; continue;}
if (sir[i]=='*' || sir[i]=='/')
{
while (st[k]/2==-2) {post[++post[0]]=st[k]; k--;}
if (sir[i]=='*') st[++k]=-4; else st[++k]=-5;
i++;
continue;
}
i++;
}
while (k) {post[++post[0]]=st[k]; k--;}
k=0;
for (i=1;i<=post[0];i++)
{
if (post[i]>0)
{
st[++k]=post[i];
continue;
}
if (post[i]==-2)
{
st[k-1]+=st[k];
k--;
continue;
}
if (post[i]==-3)
{
st[k-1]-=st[k];
k--;
continue;
}
if (post[i]==-4)
{
st[k-1]*=st[k];
k--;
continue;
}
if (post[i]==-5)
{
st[k-1]/=st[k];
k--;
continue;
}
}
printf("%d\n",st[1]);
return 0;
}