Pagini recente » Cod sursa (job #1781694) | Cod sursa (job #2742351) | Cod sursa (job #2764457) | Cod sursa (job #2176049) | Cod sursa (job #951272)
Cod sursa(job #951272)
#include<cstdio>
#define IsNum(c) ((int)(c-'0') < 10) && (((int)(c-'0')) >= 0)
int getNumber(char &c)
{
int k = (int)(c-'0');
do {
scanf("%c",&c);
if (IsNum(c)) k = k*10 + (int)(c-'0');
} while ( IsNum(c));
return k;
}
int addOperator(char op)
{
if (op == '+') return 1000000001;
else if (op == '-') return 1000000002;
else if (op == '*') return 1000000003;
else if (op == '/') return 1000000004;
else return 1000000005;
}
int priority(char op)
{
if ( op=='*' || op=='/')
return 2;
else if ( op=='+' || op=='-')
return 1;
else return 0;
}
int main()
{
freopen("evaluare.in","r",stdin);
freopen("evaluare.out","w",stdout);
char c;
int pf[50000];
int solution[50000];
int soli = -1;
char st[50000];
int sti = -1,pfi = -1;
scanf("%c",&c);
do
{
if ( IsNum(c) )
pf[++pfi] = getNumber(c);
else if (c=='(')
{
st[++sti] = c;
scanf("%c",&c);
}
else if (c==')')
{
while (st[sti]!='(')
pf[++pfi] = addOperator(st[sti--]);
scanf("%c",&c);
}
else
{
if (sti == -1)
st[++sti] = c;
else
{
while ( (sti!=-1) && (priority(st[sti]) >= priority(c)) )
pf[++pfi] = addOperator(st[sti]),--sti;
st[++sti] = c;
}
scanf("%c",&c);
}
}
while (!feof(stdin));
while (sti!=-1)
pf[++pfi] = addOperator(st[sti--]);
for (int i=0;i<=pfi;++i)
{
if (pf[i]<=1000000000)
solution[++soli] = pf[i];
else switch (pf[i])
{
case 1000000001: solution[soli-1]+=solution[soli],--soli; break;
case 1000000002: solution[soli-1]-=solution[soli],--soli; break;
case 1000000003: solution[soli-1]*=solution[soli],--soli; break;
case 1000000004: solution[soli-1]/=solution[soli],--soli; break;
}
}
printf("%d",solution[0]);
return 0;
}