The std::stack
is an adapter class which lets the user push objects onto it like on a real stack of objects, and pop objects down from it again. In this section, we construct a reverse polish notation (RPN) calculator around that data structure, in order to show how to use it.
The RPN is a notation that can be used to express mathematical expressions in a way that is really simple to parse. In RPN, 1 + 2
is 1 2 +
. Operands first, then the operation. Another example: (1 + 2) * 3
would be 1 2 + 3 *
in RPN and that already shows why it is easier to parse, as we do not need parentheses to define subexpressions.
In this section, we will read a mathematical expression in RPN from the standard input, and then feed it into a function that evaluates it. In the end, we print the numeric result back to the user.
- We will use a lot of helpers from the STL, so there are a few includes:
#include <iostream> #include <stack...