Postfix Evaluator

Evaluate a postfix expression using stack, and see the step-by-step process used to achieve the result.

Learn More

Selected Data Record:

A Data Record is a set of calculator entries that are stored in your web browser's Local Storage. If a Data Record is currently selected in the "Data" tab, this line will list the name you gave to that data record. If no data record is selected, or you have no entries stored for this calculator, the line will display "None".

DataData recordData recordSelected data record: None
Example notations:

Example postfix expressions:

To see an example of how the Postfix Evaluator works, and what types of expressions the calculator is set up to handle, select a postfix expression from the drop-down menu. To clear the expression field to enter your own postfix expression, select "Example Problems" or click the "Reset" button.

Postfix expression:

Postfix expression:

Enter a postfix expression that fits within the following guidelines:

  • Include a single space between numbers or operators.
  • Numbers and operators only (no letters or variables).
  • Contains only numbers, decimal points, and these valid characters: ^ * / + - .
  • Numbers with a leading decimal point must be preceded by a zero (enter .5 as 0.5).
#
Result:

Result:

This line will display the result of the postfix evaluation.

If you would like to save the current entries to the secure online database, tap or click on the Data tab, select "New Data Record", give the data record a name, then tap or click the Save button. To save changes to previously saved entries, simply tap the Save button. Please select and "Clear" any data records you no longer need.

Help and Tools

Learn

How to evaluate postfix expression using stack.

How to Evaluate a Postfix Expression Using Stack

In case you're not familiar, a stack is a collection or list wherein the last element added to the stack is always the first element to be removed.

When evaluating postfix expressions, using a stack to temporarily store operands is necessary because as we are evaluating each character of the postfix expression from left to right, we can't instantly know an operator's right-hand operand. Therefore we need to temporarily add (push) operands to the stack and only remove (pop) them from the stack once we know an operator's right operand.

So now that you know what a stack is and why it is used, here is the process for evaluating a postfix expression using stack.

Moving from left to right, one character at a time, if a character is an operand (number), push it to the top of the stack.

Otherwise, if a character is an operator (^ * / + -), pop (remove) the top element from the stack to form the operator's right operand, and then pop the next top element from the stack to form the operator's left operand. Finally, solve the expression formed by the operator and its operands, and push the result to the top of the stack.

Repeat the above until all characters have been processed, at which point the last element remaining in the stack becomes the result.

Postfix Evaluation Examples

Here are a couple of examples of how to evaluate postfix expressions using the stack method.

Example #1: 4 5 + 7 2 - *

4 5 + 7 2 - *

The first character scanned is "4", which is an operand, so push it to the stack.

4
  
StackExpression

4 5 + 7 2 - *

The next character scanned is "5", which is an operand, so push it to the stack.

5
4
  
StackExpression

4 5 + 7 2 - *

The next character scanned is "+", which is an operator, so pop its two operands from the stack. Pop 5 from the stack for the right operand and then pop 4 from the stack to make the left operand.

 
 4 + 5 = 9 
StackExpression

Next, push the result of 4 + 5 (9) to the stack.

9
  
StackExpression

4 5 + 7 2 - *

The next character scanned is "7", which is an operand, so push it to the stack.

7
9
  
StackExpression

4 5 + 7 2 - *

The next character scanned is "2", which is an operand, so push it to the stack.

2
7
9
  
StackExpression

4 5 + 7 2 - *

The next character scanned is "-", which is an operator, so pop its two operands from the stack. Pop 2 from the stack for the right operand and then pop 7 from the stack to make the left operand.

9
 7 - 2 = 5 
StackExpression

Next, push the result of 7 - 2 (5) to the stack.

5
9
  
StackExpression

4 5 + 7 2 - *

The next character scanned is "*", which is an operator, so pop its two operands from the stack. Pop 5 from the stack for the right operand and then pop 9 from the stack to make the left operand.

 
 9 * 5 = 45 
StackExpression

Next, push the result of 9 * 5 (45) to the stack.

45
  
StackExpression

Since we are done scanning characters, the remaining element in the stack (45) becomes the result of the postfix evaluation.

Postfix notation: 4 5 + 7 2 - *
Result: 45
Example #2: 4 2 3 5 1 - + * +

4 2 3 5 1 - + * +

The first character scanned is "4", which is an operand, so push it to the stack.

4
  
StackExpression

4 2 3 5 1 - + * +

The next character scanned is "2", which is an operand, so push it to the stack.

2
4
  
StackExpression

4 2 3 5 1 - + * +

The next character scanned is "3", which is an operand, so push it to the stack.

3
2
4
  
StackExpression

4 2 3 5 1 - + * +

The next character scanned is "5", which is an operand, so push it to the stack.

5
3
2
4
  
StackExpression

4 2 3 5 1 - + * +

The next character scanned is "1", which is an operand, so push it to the stack.

1
5
3
2
4
  
StackExpression

4 2 3 5 1 - + * +

The next character scanned is "-", which is an operator, so pop its two operands from the stack. Pop 1 from the stack for the right operand and then pop 5 from the stack to make the left operand.

3
2
4
 5 - 1 = 4 
StackExpression

Next, push the result of 5 - 1 (4) to the stack.

4
3
2
4
  
StackExpression

4 2 3 5 1 - + * +

The next character scanned is "+", which is an operator, so pop its two operands from the stack. Pop 4 from the stack for the right operand and then pop 3 from the stack to make the left operand.

2
4
 3 + 4 = 7 
StackExpression

Next, push the result of 3 + 4 (7) to the stack.

7
2
4
  
StackExpression

4 2 3 5 1 - + * +

The next character scanned is "*", which is an operator, so pop its two operands from the stack. Pop 7 from the stack for the right operand and then pop 2 from the stack to make the left operand.

4
 2 * 7 = 14 
StackExpression

Next, push the result of 2 * 7 (14) to the stack.

14
4
  
StackExpression

4 2 3 5 1 - + * +

The next character scanned is "+", which is an operator, so pop its two operands from the stack. Pop 14 from the stack for the right operand and then pop 4 from the stack to make the left operand.

 
 4 + 14 = 18 
StackExpression

Next, push the result of 4 + 14 (18) to the stack.

18
  
StackExpression

Since we are done scanning characters, the remaining element in the stack (18) becomes the result of the postfix evaluation.

Postfix notation: 4 2 3 5 1 - + * +
Result: 18

Adjust Calculator Width:

Move the slider to left and right to adjust the calculator width. Note that the Help and Tools panel will be hidden when the calculator is too wide to fit both on the screen. Moving the slider to the left will bring the instructions and tools panel back into view.

Also note that some calculators will reformat to accommodate the screen size as you make the calculator wider or narrower. If the calculator is narrow, columns of entry rows will be converted to a vertical entry form, whereas a wider calculator will display columns of entry rows, and the entry fields will be smaller in size ... since they will not need to be "thumb friendly".

Show/Hide Popup Keypads:

Select Show or Hide to show or hide the popup keypad icons located next to numeric entry fields. These are generally only needed for mobile devices that don't have decimal points in their numeric keypads. So if you are on a desktop, you may find the calculator to be more user-friendly and less cluttered without them.

Stick/Unstick Tools:

Select Stick or Unstick to stick or unstick the help and tools panel. Selecting "Stick" will keep the panel in view while scrolling the calculator vertically. If you find that annoying, select "Unstick" to keep the panel in a stationary position.

If the tools panel becomes "Unstuck" on its own, try clicking "Unstick" and then "Stick" to re-stick the panel.