Tuesday, July 21, 2020

Infix to Postfix/Prefix converter

https://scanftree.com/Data_Structure/prefix-postfix-infix-online-converter

https://www.web4college.com/converters/infix-to-postfix-prefix.php

https://raj457036.github.io/Simple-Tools/prefixAndPostfixConvertor.html (wrong)


Infix to Postfix/Prefix converter

We have two converters. The first converter converts infix to postfix expression. And the second one converts infix to prefix expression. It avoids the problem of operator precedence and association while making calculations in programming languages.
You will get step by step conversion for your infix expression to postfix or prefix form.

Infix to Postfix converter

The following converter converts an infix expression to a postfix expression.
Change the expression and converter will convert infix to postfix step by step.

Infix: 
Postfix: 

Step by step output for "" expression

Input StringOutput StackOperator Stack
(A/B)+CD+E(F/G)(
(A/B)+CD+E(F/G)A(
(A/B)+CD+E(F/G)A(/
(A/B)+CD+E(F/G)AB(/
(A/B)+CD+E(F/G)AB/
(A/B)+CD+E(F/G)AB/+
(A/B)+CD+E(F/G)AB/C+
(A/B)+CD+E(F/G)AB/CD+
(A/B)+CD+E(F/G)AB/CD++
(A/B)+CD+E(F/G)AB/CD+E+
(A/B)+CD+E(F/G)AB/CD+E+(
(A/B)+CD+E(F/G)AB/CD+EF+(
(A/B)+CD+E(F/G)AB/CD+EF+(/
(A/B)+CD+E(F/G)AB/CD+EFG+(/
(A/B)+CD+E(F/G)AB/CD+EFG/+
(A/B)+CD+E(F/G)AB/CD+EFG/+

Infix to Prefix converter

The following converter converts an infix expression to a prefix expression.
Change the expression and converter will convert infix to prefix step by step.
Infix: 
Prefix: 

Step by step output for "" expression

  • 1. Reversed string:   
  • 2. Postfix of :     (see table)
  • 3. Reversed string of :   

Infix, Postfix, Prefix

Infix
Infix expression is the normal expression that consists of operands and operators. For example, A+B
Postfix
Postfix expression consists of operands followed by operators. For example, AB+
Prefix
Prefix expression consists of operators followed by operands. For example, +AB

Algorithm to convert infix to postfix expression

In this case, We use the stacks to convert infix to postfix. We have operator's stack, output's stack and one input string. Operator's stack works as FILO(First In Last Out). Output's stack works as FIFO (First In First Out).
The following algorithm converts infix to postfix.
  • Scan input string from left to right character by character.
  • If the character is an operand, put it into output stack.
  • If the character is an operator and operator's stack is empty, push operator into operators' stack.
  • If the operator's stack is not empty, there may be following possibilites.
    • If the precedence of scanned operator is greater than the top most operator of operator's stack, push this operator into operand's stack.
    • If the precedence of scanned operator is less than or equal to the top most operator of operator's stack, pop the operators from operand's stack untill we find a low precedence operator than the scanned character. Never pop out ( '(' ) or ( ')' ) whatever may be the precedence level of scanned character.
    • If the character is opening round bracket ( '(' ), push it into operator's stack.
    • If the character is closing round bracket ( ')' ), pop out operators from operator's stack untill we find an opening bracket ('(' ).
    • Now pop out all the remaining operators from the operator's stack and push into output stack.

Algorithm to convert infix to prefix expression

In this case, we have operator's stack, output's stack and one input string. Operator's stack works as FILO(First In Last Out). Output's stack works as FIFO (First In First Out).
The following algorithm must be followed for infix to prefix conversion.
  • Reverse the input string.
  • Convert the reversed string into infix expression.
  • Now reverse the resulting infix expression obtained from the previous step. The resulting expression is prefix expression

No comments:

Post a Comment