Calculator |
|
OK, you have two stacks as discussed on the previous page - how do you perform the calculations? Basically, all we need are two additional stacks (temp_operands, temp operators) and a way to compare precedence of operators. How you implement this is best done through examples but the basic idea takes advantage of something like Reverse Polish Notation... we take advantage of the fact that if you have any string of operator1 value1 operator2 value2, you can decide how to process them based on the precedence of the two operators - in one case you perform operator 2, in the other you postpone. We'll start with a simple example: 3 + 5 / 12
We always (except with parenthesis) push the Operand/Operator pair to the temp stacks... Next Page... |