stacker
JavaScript
React
Infix to postfix/prefix convertor and evaluator.
InFix Notation: A mathematical notation in which operators are placed between operands.
PreFix Notation: Also known as Polish Notation, is a mathematical notation in which operators precede their operands.
PostFix Notation: Also known as Reverse Polish Notation, is a mathematical notation in which operators follow their operands.
Practically, these are used in calculators, as they increase the speed of calculations.
Algorithms for conversion
InFix to PostFix conversion:
- Scan each element of the expression (X) from left to right and repeat steps 2 to 5 until the stack is empty.
- If an operand is encountered add it to the resultant string (Y).
- If a left parenthesis ( "(" ) is encountered then push it to the stack.
- If an operator is encountered then:
- Repeatedly pop from stack and add it to Y which has the same or higher precedence than the next operator.
- Push operator to the stack.
- If a right parenthesis is encountered then:
- Repeatedly pop from the stack and to Y until a left parenthesis is encountered.
- Pop the left parenthesis.
- END
InFix to PreFix conversion:
- Scan the expression (X) and reverse it, also replace "(" to ")" and vice versa.
- Perform InFix to PostFix conversion on the reversed expression.
- Finally, reverse the obtained PostFix expression, resulting in the PreFix expression.
- END
Algorithms for evaluations
PostFix evaluation:
- Scan the expression (X) from left to right and repeat steps 2 to 4.
- If an operand is encountered then push the element to stack.
- If an operator is encountered then pop two operands and evaluate based on the operator.
- Push the answer to the stack.
- Finally, pop the element which is the answer.
- END
PreFix evaluation:
- Scan the expression (X) and reverse it.
- Perform PostFix evaluation on the expression.
- The resulting value is the answer.
- END
README.md