Pagini recente » Cod sursa (job #1188895) | Cod sursa (job #1408894) | Cod sursa (job #883563) | Cod sursa (job #1333585) | Cod sursa (job #754681)
Cod sursa(job #754681)
#include <fstream>
#include <cstring>
using namespace std;
#define dim 100001
char sir[dim];
int n;
int vf;
pair <int,char> stiva[dim];
void solve()
{
int i,j,k;
for(i=vf;stiva[i].second!='(';--i);
for(j=i,k=i;j<=vf;++j)
{
int val=1,ok=0;
if(stiva[j].second=='*' || stiva[j].second=='/')
{
val=stiva[j-1].first;
ok=1;
--k;
}
while(stiva[j].second=='*' || stiva[j].second=='/')
{
if(stiva[j].second=='*')
val*=stiva[j+1].first;
else
val/=stiva[j+1].first;
stiva[j-1].first=stiva[j].second=stiva[j+1].first=0;
j+=2;
}
if(ok==1)
stiva[k++].first=val;
if(stiva[j].first!=0 || stiva[j].second=='+' || stiva[j].second=='-')
stiva[k++]=stiva[j];
}
int q=stiva[i].first;
for(j=i;j<=k;++j)
{
if(stiva[j].second=='+')
q+=stiva[j+1].first;
if(stiva[j].second=='-')
q-=stiva[j+1].first;
stiva[j].first=stiva[j].second=0;
}
vf=i;
stiva[vf].first=q;
stiva[vf].second=0;
}
int eval()
{
int s=0;
for(int i=0;i<strlen(sir);++i)
{
int nr=0,ok=0;
while(isdigit(sir[i]))
{
nr=nr*10+sir[i]-'0';
++i;
ok=1;
}
if(ok==1)
stiva[++vf]=make_pair(nr,0);
if(sir[i]==')')
solve();
else
stiva[++vf]=make_pair(0,sir[i]);
}
stiva[0].second='(';
solve();
return stiva[0].first;
}
int main()
{
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
fin>>sir;
fout<<eval();
return 0;
}