C Program: Infix Expression to a Postfix Conversion

0
251

This program show how to convert a in-ix expression to a post-fix expression, to understand the program you should read the rules for converting a normal infix expression to postfix expression. For example

We begin the code by including the header files “stdio.h”, “conio.h” , after including the header files we create 4 functions namely:-

  • int r(char z)

It is used to find the presidency of the operators and in c operators cannot be directly compared to find out who has higher presidency and to convert a infix expression to post fix we need to find the presidency of the operators.

  • void pushout(char a)

This function basically creates a main stack (n[]) and pushes the final output which will be displayed on the output screen

  • void pushs(char b)

This function creates a secondary stack (n1[]) which stores the operators which are then later pushed to the main stack to be displayed on the output screen.

  • void pop()

This function displays the elements stored in main stack (n[]) on the output screen.

Now we enter the main program,

C Program: Infix Expression to a Postfix Conversion

User is asked to input the string, that is, the expression, after it a for loop is initialized which executes until the end of the string.

Then it start checking from the first element of the string if it is an open braces ‘(‘ it calls pushs() function, that is, is stores it in the secondary stack , but if the element is a character then it pushes it to the main stack by calling the pushout() function and if the element scanned is a operator then variable ‘f’ is incremented and later it is checked if ‘f’ is greater than one which means the secondary stack (n[]) already has an operator and then they both are checked as to find who has a higher presidency by calling  r() function and if the operator scanned has higher presidency then it gets stored in the secondary stack else if it has lower presidency than the operator already present in the secondary stack then the operators are shifted to main stack (n[]) until an operator in secondary stack is found whose presidency is less than that of the scanned element from the string.

C Program: Infix Expression to a Postfix Conversion

If an close brace is found ‘)’ then all the elements of the secondary stack stored are transferred to main stack and the for loop ends and later all the elements of the main stack are displayed on the output screen by calling the pop() function.

C Program: Infix Expression to a Postfix Conversion

You have to add open and close brace ‘(‘ ‘)’  at the start and end of the string and I have just used the code which contains single braces and a limited set of operators. The program is simple to understand if you understand the logic and rules of converting infix to postfix.

C Program: Infix Expression to a Postfix Conversion C Program: Infix Expression to a Postfix Conversion

Download C Program: Infix Expression to a Postfix Conversion

LEAVE A REPLY

Please enter your comment!
Please enter your name here