Pagini recente » Cod sursa (job #3188217) | Cod sursa (job #812821) | Cod sursa (job #2700815) | Cod sursa (job #959719) | Cod sursa (job #2691697)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
char a[100005];
int st[100005];
int main()
{
int i,top=0,x;
fin >> a;
for (i=0; a[i];)
{
if (a[i]=='+') i++;
else if (a[i]=='-') {st[++top]=-1; i++;}
else if (a[i]=='*') {st[++top]=-2; i++;}
else if (a[i]=='/') {st[++top]=-3; i++;}
else if (a[i]=='(') {st[++top]=-4; i++;}
else if (a[i]==')')
{
i++;
x=0;
while (st[top]!=-4)
{
x+=st[top];
top--;
}
top--;
if (top==0) st[++top]=x;
else
{
if (st[top]==-1)
{
st[top]=-x;
}
else if (st[top]==-2)
{
top--;
st[top]*=x;
}
else if (st[top]==-3)
{
top--;
st[top]/=x;
}
else st[++top]=x;
}
}
else if ('0'<=a[i] and a[i]<='9')
{
x=0;
while ('0'<=a[i] and a[i]<='9')
{
x=x*10+(a[i]-'0');
i++;
}
if (top==0) st[++top]=x;
else
{
if (st[top]==-1)
{
st[top]=-x;
}
else if (st[top]==-2)
{
top--;
st[top]*=x;
}
else if (st[top]==-3)
{
top--;
st[top]/=x;
}
else st[++top]=x;
}
}
}
x=0;
for (i=1; i<=top; i++)
x+=st[i];
fout << x;
return 0;
}