Here's a simple RPN evaluator, without any error handling. You only need a stack to store the operands, not the operators, which makes it pretty easy to implement.
Note that this version assumes the operands are single digit numbers on the input expression. I've done this to simplify parsing the RPN expression. In real life you'd want to handle multi-digit operands.
std::stack<int> stack;
const char *expression="12+4*3+";
for(char c=*expression; c!=0; c=*expression++)
{
switch(c)
{
case '+':
{
int rhs=stack.top(); stack.pop();
int lhs=stack.top(); stack.pop();
int result=lhs+rhs;
stack.push(result);
break;
}
case '-':
{
int rhs=stack.top(); stack.pop();
int lhs=stack.top(); stack.pop();
int result=lhs-rhs;
stack.push(result);
break;
}
case '*':
{
int rhs=stack.top(); stack.pop();
int lhs=stack.top(); stack.pop();
int result=lhs*rhs;
stack.push(result);
break;
}
case '/':
{
int rhs=stack.top(); stack.pop();
int lhs=stack.top(); stack.pop();
int result=lhs/rhs;
stack.push(result);
break;
}
default:
int number=(c-'0');
stack.push(number);
break;
}
}
int final_result=stack.top();
std::cout << "result is " << final_result << std::endl;