Updated (12 Feb 2023)

Postfix & Prefix Evaluator

This is a simple Prefix or Postfix Evaluator.


Enter the Postfix or Prefix expression below in box and press Evaluate
Note: Enter the number and operators seperated with space " "

Evaluated :

By Raj

Sr. no. Expression Stack

Algorithm used

Postfix Evaluation

  • Step 1: Add a ")" at the end of the postfix expression
  • Step 2: Scan every character of the postfix expression and repeat Step 3 and 4 until ")" is encountered.
  • Step 3: IF an operand is encountered, Push it on the stack
    IF an operator O is encountered, then
    • a: Pop the top two elements from the stack as A and B
    • b: Evaluate B O A, where A is the topmost element and B is the element below A.
    • c: Push the result of evaluation on the stack
      [END OF IF]
  • Step 4: Set result = stack's top elements
  • Step 5: Exit

Prefix Evaluation

  • Step 1: Get Prefix expression as it is
  • Step 2: Repeat untill all the characters in prefix
    expression have been scanned
    • a: Read the prefix expression from right to left one at a time
    • b: If the readed character is an operand, push it on the stack
    • c: If the readed character is an operator, then
      • i: pop two values from the stack.
      • ii: Apply the operation on the operands.
      • iii: Push the result onto the stack.
  • Step 3: Exit

Understanding the Postfix & Prefix Evaluator

Our Postfix & Prefix Evaluator is a powerful tool designed to calculate the result of mathematical expressions written in postfix (Reverse Polish Notation) or prefix (Polish Notation) formats. This evaluator is particularly useful for students, programmers, and anyone working with these alternative mathematical notations.

What are Postfix and Prefix Notations?

How to Use the Evaluator

  1. Enter your postfix or prefix expression in the input box.
  2. Ensure that numbers and operators are separated by spaces.
  3. Choose the appropriate evaluation mode (Postfix or Prefix) using the respective buttons.
  4. Click the "Evaluate" button to see the result.
  5. Review the evaluation process in the table below the result.

Features of Our Evaluator

Why Use Postfix and Prefix Notations?

While less intuitive for humans, postfix and prefix notations have several advantages:

Applications

This evaluator is valuable for:

Understanding the Evaluation Process

The evaluator uses a stack-based algorithm:

By using this tool and studying the evaluation process, you can gain a deeper understanding of how these notations work and how stack-based algorithms can efficiently evaluate complex expressions. This knowledge is fundamental in various areas of computer science, particularly in compiler design and expression parsing.

function sel(x) { return document.getElementById(x) } $("#fullresult").hide(); //evaluate button and expression $("button#evaluate").click(function () { var items, tab = ""; $("#fullresult").show(); let table; const input = document.getElementById("expression").value.trim(); switch ($('input[name=fixer]:checked').val()) { case '1': if (checker(input) == 0) { table = postfixEval(input); } else { alert("Humm! looks like this is a prefix expression please select prefix evaluate."); } break; case '2': if (checker(input) == 1) { table = prefixEval(input); } else { alert("Humm! looks like this is a postfix expression please select postfix evaluate."); } break; } $("span#eval").html(table.s[table.s.length - 1]); for (var i = 0; i < table.char.length; i++) { tab +="" + i + "" + table.char[i] + "" + table.s[i] + "" ; } sel("tabledata").innerHTML=tab; })