Pagini recente » Cod sursa (job #881492) | Cod sursa (job #1036817) | Cod sursa (job #1524084) | Cod sursa (job #3036703) | Cod sursa (job #1466624)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
#define nmax 100005
using namespace std;
char a[nmax],st1[nmax];
int c[nmax],st[nmax];
int n,vf,x,y,vf1;
int val(char c)
{
if(c=='(')
return 0;
if(c=='+'||c=='-')
return 1;
return 2;
}
int numar(char c)
{
if(c=='-')
return -1;
if(c=='+')
return -2;
if(c=='/')
return -3;
return -4;
}
void polonez()
{
int nr=strlen(a),num=0,ok=0;
for(int i=0;i<nr;i++)
{
if(isdigit(a[i]))
{
num=num*10+a[i]-'0';
ok=1;
}
else
{
if(ok)
c[++n]=num;
num=ok=0;
if(a[i]=='(')
{
st1[++vf1]='(';
}
else
if(a[i]==')')
{
while(st1[vf1]!='(')
c[++n]=numar(st1[vf1--]);
vf1--;
}
else
{
while(val(a[i])<=val(st1[vf1]))
c[++n]=numar(st1[vf1--]);
st1[++vf1]=a[i];
}
}
}
}
void eval()
{
vf=0;
for(int i=1;i<=n;i++)
{
if(c[i]>=0)
st[++vf]=c[i];
else
if(c[i]==-2)
{
x=st[vf--];
y=st[vf];
if(vf)
st[vf]=x+y;
else
st[++vf]=x+y;
}
else
if(c[i]==-1)
{
x=st[vf--];
y=st[vf];
if(vf>0)
st[vf]=x-y;
else
st[++vf]=y-x;
}
else
if(c[i]==-3)
{
x=st[vf--];
y=st[vf];
st[vf]=y/x;
}
else
if(c[i]==-4)
{
x=st[vf--];
y=st[vf];
st[vf]=x*y;
}
}
}
int main()
{
freopen("evaluare.in","r",stdin);
freopen("evaluare.out","w",stdout);
cin>>a+1;
a[0]='(';
a[strlen(a)]=')';
polonez();
eval();
cout<<st[vf];
return 0;
}