When you write down an operator expression – for example, 2+3*4 – you don’t perform the operations in the order they are written.
That is, 2+3*4 is not 2+3 multiplied by 4 it is 3*4 plus 2 which is 14 not 20.
Given that this is another “alter the order of things” problem you should again be thinking “stack”!
In this case the method is slightly more complicated than the shunting algorithm but not much. The simplest way of doing it is to have two stacks – one for the values and one for the operators, but it can be done using just one stack quite easily,
All you do is assign each operator a priority. For example, + is priority 1 and * is priority 2.
You then scan the expression from left to right stacking each item as you go on its appropriate stack.
Before you stack an operator however you compare its priority with the operator on the top of the stack. If the current operator has a higher priority then you pop it off the stack and make it operate on the top two items on the value stack – pushing the answer back on the value stack.
When the scan is completed the final answer is evaluated by popping each operator off the operator stack and using it on the top two items on the value stack, pushing the result back on until all the operators are used up.
The answer is then the single value on the stack.
In the case of the expression 2+3*4 this would result in the following steps –
Try the same method on 2*3+4 and you will discover that when you reach the + operator the 2*3 is automatically calculated before the + is pushed onto the operator stack (because the * has a higher priority and we have to evaluate the top two items on the stack using it and push the result back on the stack)..
Reverse Polish -RPN
This stack operator algorithm is well known and there are lots of variations on it. It also gives rise to the once very well-known Reverse Polish (RP) notation.
If you write operator expressions so that each operator acts on the two items to its left then the expression can be evaluated using an even simpler stack algorithm.
For example, in RP the expression 2+3*4 would be written 2, 3,4*,+ and it can be evaluated simply by scanning from left to right and pushing values on the stack.
Each time you hit an operator you pop the top two items from the stack, apply the operator and push the result back on the stack.
When you get to the end of the RP expression the result is on the top of the stack.
Try it and you will discover it works. In fact you can use a stack to convert standard operator, known as infix, expressions to RP.
Back in the days when hardware was more expensive it was thought to be a good idea to get people to work directly in RP. You could and still can buy calculators that work in RP notation.
To add 2 and 3 you enter 2, then 3 and press the plus button.
Burroughs even built a mainframe computer that didn’t have any other type of working memory than a stack. Everything it did was effectively in RP notation applied to the central stack. The stack machine architecture was simple but not fast enough to compete with more general architectures.
However the stack approach to computing hasn't completely died a death. For example, the difference between the usual Java Virtual Machine - JVM and the one used in Android - Dalvik is the use of a stack. The standard JVM is register based machine but this is too expensive and power hungry for a mobile device hence the Dalvik VM is a stack oriented VM.
Perhaps the longest standing overuse of the stack approach was, and is, the language Forth and its many derivatives.
It turns out that with a little effort you can build an entire programming language that can be written down as operators in RP notation.
Of course making it work is just a matter of using a stack to push operators and operands onto.This makes it easy to implement but arguably difficult to use.
However the fun that people had thinking in RP is such that they became really good at it!
The fascination for this sort of language is so great that you still find it in use in niche environments particularly where hardware is limited and it has its enthusiast following.
The LIFO stack is a wonderful invention but it isn’t the only thing computer science has to offer us as a practical tool!
If all you know is the LIFO stack then everything looks like a pop or push.
What has modular arithmetic got to do with the real world? The answer any experienced programmer should give you is "a lot". Not only is it the basis for many an algorithm, it is part of the hardware. [ ... ]