Pagini recente » Cod sursa (job #1932834) | Cod sursa (job #1351527) | Cod sursa (job #1529794) | Cod sursa (job #729390) | Cod sursa (job #326689)
Cod sursa(job #326689)
#include<stdio.h>
#include<string.h>
#include<ctype.h>
char s[100010],polo[100010][12],op[100010];
double ps[100010];
long i,j,l,t;
int prior(char a)
{
if(a=='+' || a=='-') return 1;
return 2;
}
int main()
{
freopen("evaluare.in","r",stdin);
freopen("evaluare.out","w",stdout);
gets(s);
l=strlen(s);
for(i=0;i<l;++i)
{
if(isdigit(s[i]))
{
polo[0][1]++;
polo[polo[0][1]][++t]=s[i];
while(isdigit(s[i+1]))
{
i++;
polo[polo[0][1]][++t]=s[i];
}
polo[polo[0][1]][0]=t;
t=0;
}
else
{
if(!op[0] || s[i]=='(')
{
++op[0];
op[op[0]]=s[i];
}
else
{
if(s[i]==')')
{
while(op[op[0]]!='(')
{
polo[++polo[0][1]][1]=op[op[0]];
polo[polo[0][1]][0]=1;
op[0]--;
}
op[0]--;
}
else
{
if(prior(op[op[0]])<prior(s[i]) || op[op[0]]=='(')
{
op[++op[0]]=s[i];
}
else
{
while(op[0] && op[op[0]]!='(')
{
polo[++polo[0][1]][1]=op[op[0]];
polo[polo[0][1]][0]=1;
op[0]--;
}
op[++op[0]]=s[i];
}
}
}
}
}
while(op[0])
{
polo[++polo[0][1]][1]=op[op[0]];
polo[polo[0][1]][0]=1;
op[0]--;
}
/* for(i=1;i<=polo[0][1];++i)
{
for(j=1;j<=polo[i][0];++j)
{
printf("%c",polo[i][j]);
}
printf("\n");
}*/
t=1;
long a,b,c,temp[12],max=polo[0][1];
for(i=1;i<=max;++i)
{
if(isdigit(polo[i][1]))
{
for(j=1;j<=polo[i][0];++j)
ps[i]=ps[i]*10+(polo[i][j]-'0');
}
else
{
if(polo[i][1]=='+') ps[i]=1000000001;
if(polo[i][1]=='-') ps[i]=1000000002;
if(polo[i][1]=='*') ps[i]=1000000003;
if(polo[i][1]=='/') ps[i]=1000000004;
}
}
while(t)
{
t=0;
for(i=1;i<=max;++i)
{
if(ps[i]>=1000000001)
{
t=1;
a=ps[i-2];b=ps[i-1];
if(ps[i]==1000000001) c=a+b;
else
if(ps[i]==1000000002) c=a-b;
else
if(ps[i]==1000000003) c=a*b;
else
if(ps[i]==1000000004) c=a/b;
memset(temp,0,sizeof(temp));
ps[i-2]=c;
for(j=i-1;j<=max-2;++j)
{
ps[j]=ps[j+2];
}
max-=2;
break;
}
}
}
printf("%.0lf",ps[1]);
printf("\n");
return 0;
}