#include<iostream>
#include<vector>
#include<stack>
#include<string>
using namespace std;
void Lisp(string str)
{
vector<string> st;
stack<int>res;
bool flag = true;
int n = str.size();
for(int i = n - 1; i >= 0;)
{
if(str[i] == ')' || str[i] == ' ' || str[i] == '(')
{
i--;
}
else if(str[i] >= 'a' && str[i] <= 'z')
{
string op = str.substr(i - 2, 3);
st.push_back(op);
i -= 3;
}
else
{
int spaceIndex = str.rfind(' ', i);
string num = str.substr(spaceIndex + 1, i - spaceIndex);
st.push_back(num);
i = spaceIndex;
}
}
n = st.size();
for(int j = 0; j < n; j++)
{
string s = st[j];
if(s == "add" || s == "sub" || s == "mul" || s == "div")
{
if(res.size() < 2)
{
return;
}
int result = 0;
int num2 = res.top();
res.pop();
int num1 = res.top();
res.pop();
if(s == "add")
{
result = num1 + num2;
}
else if(s == "sub")
{
result = num2 - num1;
}
else if(s == "mul")
{
result = num1*num2;
}
else if(s == "div")
{
if(num1 == 0)
{
flag = false;
}
else
{
result = num2 / num1;
}
}
res.push(result);
}
else
{
res.push(atoi(s.c_str()));
}
}
if(!flag)
{
cout << "error" << endl;
}
else
{
cout << res.top() << endl;
}
}
int main()
{
string str;
cout << "please input the string:";
getline(cin, str);
Lisp(str);
return 0;
}