Pagini recente » Cod sursa (job #284154) | Cod sursa (job #868197) | Cod sursa (job #143874) | Cod sursa (job #1584166) | Cod sursa (job #2334684)
#include <bits/stdc++.h>
using namespace std;
ifstream f("evaluare.in");
ofstream g("evaluare.out");
int n,i;
string s;
vector<pair<int,int> > pst;
stack<int> St;
map<char,int> Map;
int termen()
{
int ret=0;
for(;i<n&&'0'<=s[i]&&s[i]<='9';i++)
ret=ret*10+(s[i]-'0');
return ret;
}
int main()
{
f>>s;n=s.size();
Map['-']=1;
Map['+']=2;
Map['*']=3;
Map['/']=4;
for(i=0;i<n;)
{
if('0'<=s[i]&&s[i]<='9')
{
pst.push_back({termen(),0});
continue;
}
if(s[i]=='('){St.push(5);i++;continue;}
if(s[i]==')')
{
while(St.top()!=5)
{
pst.push_back({0,St.top()});
St.pop();
}St.pop();
i++;continue;
}
while(St.size()&&(St.top()>=Map[s[i]])&&(St.top()!=5))
{
pst.push_back({0,St.top()});
St.pop();
}
St.push(Map[s[i]]);
i++;
}
while(St.size())
{
pst.push_back({0,St.top()});
St.pop();
}
// for(auto it:pst)
// g<<it.first<<' '<<it.second<<'\n';
for(auto it:pst)
if(!it.second)
St.push(it.first);
else
{
int op1=St.top();St.pop();
int op2=St.top();St.pop();
if(it.second==2)St.push(op2+op1);
if(it.second==1)St.push(op2-op1);
if(it.second==3)St.push(op2*op1);
if(it.second==4)St.push(op2/op1);
}
g<<St.top();
return 0;
}