Pagini recente » Cod sursa (job #2630876) | Cod sursa (job #622951) | Cod sursa (job #1803046) | Cod sursa (job #2227204) | Cod sursa (job #2848009)
#include <fstream>
#include <stack>
using namespace std;
stack <int> st;
stack <char> polo;
int pr(char op)
{
switch (op)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
}
}
int op(int x, char oper, int y)
{
switch (oper)
{
case '+':
return x + y;
case '-':
return x - y;
case '/':
return x / y;
case '*':
return x * y;
}
}
const int NMAX = 100003;
char s[NMAX];
int main()
{
return 0;
ifstream cin("evaluare.in");
ofstream cout("evaluare.out");
cin >> s;
int nr = 0;
for (int i = 0; s[i]; i++)
{
if (s[i] >= '0' and s[i] <= '9')
{
nr = nr * 10 + s[i] - '0';
continue;
}
if (nr)
st.push(nr);
nr = 0;
if (s[i] == '(')
{
polo.push('(');
continue;
}
if (s[i] == ')')
{
if (st.size() >= 2 and !polo.empty()
and polo.top() != '('
)
{
int val = st.top();
st.pop();
st.top() = op(st.top(), polo.top(), val);
polo.pop();
}
polo.pop();
continue;
}
while (!polo.empty() and pr(s[i]) <= pr(polo.top())
and polo.top() != '(')
{
int val = st.top();
st.pop();
st.top() = op(st.top(), polo.top(), val);
polo.pop();
}
polo.push(s[i]);
}
if (nr)
st.push(nr);
while (!polo.empty())
{
int val = st.top();
st.pop();
st.top() = op(st.top(), polo.top(), val);
polo.pop();
}
cout << st.top();
}