Pagini recente » Cod sursa (job #1748387) | Cod sursa (job #1926487) | Cod sursa (job #247736) | Cod sursa (job #449261) | Cod sursa (job #2033798)
#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;
}