Infix to Postfix Conversion in C. For example, let the infix expression be A+B.C. In this infix expression. will be converted first and + will be converted last, this is usual thing. Let us do this step by step, A+ B. C. A + BC. B, C operands are combined and. is written on right side. Now A and BC. are two operands.
In this article, we discuss about writing a C program to convert infix expression to prefix expression. The infix expression is of form
operand operator operand
and it is converted to operator operand operand
form.Consider the following example.
Problem Definition
The user enters an infix expression and the C program converts the infix expression to prefix equivalent expression.
The program uses a character array to receive the
input expression
and another character array to output the final prefix expression
. The program use a stack S
to keep track of operators and parenthesis of the expression.- The Postfix (Postorder) form of the above expression is '23.45/-'. Infix to Postfix Conversion: In normal algebra we use the infix notation like a+b.c. The corresponding postfix notation is abc.+. The algorithm for the conversion is as follows: Scan the Infix string from left to right. Initialise an empty stack.
- The Postfix (Postorder) form of the above expression is '23.45/-'. Infix to Postfix Conversion: In normal algebra we use the infix notation like a+b.c. The corresponding postfix notation is abc.+. The algorithm for the conversion is as follows: Scan the Infix string from left to right. Initialise an empty stack.
- Algorithm to Convert Infix to Postfix Expression Using Stack. Following is the algorithm to convert infix expression into Reverse Polish notation. Initialize the Stack. Scan the operator from left to right in the infix expression. If the leftmost character is an operand, set it as the current output to the Postfix string.
Here is the algorithm to convert infix expression to prefix expression.
- Scan the infix expression right to left.
- If the character is right parenthesis “)”, push to stack S.
- If the character is left parenthesis “(“, then start popping stack one by one and put the characters into prefix array.
- If the scanned character is operator , compare with the operator on stack and continuously pop operators from stack if scanned operator has less precedence than the operator on the top of the stack. If scanned operator has higher precedence than top of the stack. Put the operator on the stack.
- If scanned character is an operand, directly put it in the prefix array.
- If all scanned character is finished , start popping stack and put the operators in prefix array one by one.
For example, suppose we want to convert the infix expression ((A + B) * C) into prefix expression.
Functions Used In the Program
The program uses functions to perform scan, pop from stack, push to stack, check the precedence of operators, etc. Each of these function is discussed in the following sections.
Stack Functions
Identify priority of scanned character
You need to identify the priority level of scanned character so that we can compare it with the operator on the top of the stack . The function will return a number for each scanned character.
Identify priority of character on the top of the stack
This function will return the priority of a character on the top of the stack. We must compare it with the priority of the scanned character. If scanned character has higher priority , push it into the stack.
Else start popping characters from stack and push them into prefix array until scanned character priority become higher.
Converting Infix To Postfix
Infix expression to prefix expression converter
![Lua And C Infix To Postfix Lua And C Infix To Postfix](https://i.ytimg.com/vi/fI2fHKMnoks/maxresdefault.jpg)
This the main function that does character by character scan of infix expression. Let’s look at the variable declarations.