Pagini recente » Cod sursa (job #2360328) | Cod sursa (job #658955) | Cod sursa (job #2642754) | Cod sursa (job #3211987) | Cod sursa (job #2282673)
#include <bits/stdc++.h>
using namespace std;
const int mins=(1e9)+10;
const int pls=(1e9)+11;
const int ori=(1e9)+12;
const int impa=(1e9)+13;
ifstream f("evaluare.in");
ofstream g("evaluare.out");
int i,n;
string s;
stack<char> St;
stack<int> st;
vector<int> ans;
map<char,int> Map;
int main()
{
f>>s;n=s.size();
Map['-']=mins;
Map['+']=pls;
Map['*']=ori;
Map['/']=impa;
for(i=n;i>=1;i--)
s[i]=s[i-1];
s[0]='(';n++;s[n]=')';
for(i=0;i<=n;)
if(s[i]>='0'&&s[i]<='9')
{
int ret=0;
while(s[i]>='0'&&s[i]<='9')
ret=ret*10+s[i]-'0',i++;
ans.push_back(ret);
}
else
{
if(s[i]=='(')
{
St.push(s[i]);i++;
continue;
}
if(s[i]==')')
{
while(St.top()!='(')
{
ans.push_back(Map[St.top()]);
St.pop();
}
St.pop();i++;
continue;
}
if((s[i]=='*')||(s[i]=='/'))
{
St.push(s[i]);
i++;continue;
}
while((St.top()=='*')||(St.top()=='/'))
{
ans.push_back(Map[St.top()]);
St.pop();
}
St.push(s[i]);i++;
}
// for(auto it:ans)
// g<<it<<' ';g<<'\n';
for(auto it:ans)
if(it>=(1e9))
{
int y=st.top();
st.pop();
int x=st.top();
st.pop();
// g<<x<<' '<<y<<' '<<it<<'\n';
if(it==mins)
st.push(x-y);
if(it==pls)
st.push(x+y);
if(it==ori)
st.push(x*y);
if(it==impa)
st.push(x/y);
}
else
st.push(it);
g<<st.top();
return 0;
}