Cod sursa(job #2033798)
Utilizator | Robert Trifan trifangrobert | Data | 7 octombrie 2017 10:47:19 |
---|---|---|---|
Problema | A+B | Scor | 0 |
Compilator | cpp | Status | done |
Runda | Arhiva de probleme | Marime | 1.36 kb |
#include <iostream>
#include <stack>
#define DIM 110
using namespace std;
int EvalExp(char s[])
{
stack<int> st;
int e = 0, i,v;
for (i = 0;s[i];++i)
{
if (s[i] == '(')
st.push(-1);
if (s[i] == '*')
st.push(-2);
if (isdigit(s[i]))
{
if (s.top() == -2)
{
s.pop();
v = st.top() * (s[i] - '0');
st.pop();
st.push(v);
}
else
st.push(s[i] - '0');
}
if (s[i] == ')')
{
v = 0;
while(!st.empty())
{
int x = st.top();
st.pop();
if (x == -1)
break;
else
v +=x;
}
if (st.empty())
st.push(v);
else if (st.top() == -2)
{
st.pop();
int x = st.top() * v;
st.pop();
st.push(x);
}
else st.push(v);
}
}
while(!st.empty())
{
e += st.top();
st.pop();
}
return e;
}
int main()
{
ifstream f("test.in");
f.getline(s, DIM);
cout << EvalExp(s);
f.close();
return 0;
}