For an assignment we are given an equation in reverse polish notation. For this example I will use: 2 3 8 * + $
The $
is for denoting the end of the expression. Using a stack, we output the answer.
I have been using:
getline(cin, input, '&');
input.c_str();
to read in the equation and then turn it into a c_string so I can look at the individual elements in the input.
After, I need to check for a few things. If the element is a digit I need to push it onto the stack. If it is white space I need to skip over it.
if (isdigit(input[i]))
{
push();
i++;
}
else if (isspace(input[i]))
{
i++;
}
Now is what has me stuck. If I hit an operator (in this case *
) I need to pop the top two elements off the stack and 'operate them' and push the result back to the stack. However, I don't know of anything that would allow me to recognize that they are operators. It is probably a silly question, but help would be greatly appreciated.