Pagini recente » Cod sursa (job #1381701) | Cod sursa (job #555256) | Cod sursa (job #2104245) | Cod sursa (job #2493817) | Cod sursa (job #2713022)
#include <bits/stdc++.h>
using namespace std;
ifstream f("evaluare.in");
ofstream g("evaluare.out");
vector<int> p;
stack<char> st;
stack<int> stiva;
int cod[305];
bool scoate(char a, char b)
{
if((a=='+' || a=='-') && (b!='('))
{
return true;
}
if((a=='*' || a=='/') && (b=='*' || b=='/'))
{
return true;
}
return false;
}
int main()
{
string aux,s;
getline(f,aux);
s = "("+aux+")";
int nr = -1;
cod['+']=-1;
cod['-']=-2;
cod['*']=-3;
cod['/']=-4;
for(auto it : s)
{
if(isdigit(it))
{
if(nr==-1)
{
nr=0;
}
nr = nr*10+it-'0';
continue;
}
if(nr!=-1)
{
p.push_back(nr);
nr = -1;
}
if(it==')')
{
while(st.top()!='(')
{
p.push_back(cod[st.top()]);
st.pop();
}
st.pop();
continue;
}
while(!st.empty() && scoate(it,st.top()))
{
p.push_back(cod[st.top()]);
st.pop();
}
st.push(it);
}
if(nr!=-1)
{
p.push_back(nr);
}
for(auto it : p)
{
if(it<0)
{
int x = stiva.top();
stiva.pop();
int y = stiva.top();
stiva.pop();
int rez = 0;
if(it==-1)
{
rez = x+y;
}
else if(it==-2)
{
rez = y-x;
}
else if(it==-3)
{
rez = x*y;
}
else if(it==-4)
{
rez = y/x;
}
stiva.push(rez);
}
else
{
stiva.push(it);
}
}
g<<stiva.top()<<'\n';
return 0;
}