In infix expressions, the operator is sandwiched between operands like A + B
. Postfix, on the other hand, is like a straight-up queue at the movie theater—operands first, then the operator, like A B +
. ?
So, how do we make this magical conversion happen in C? Well, darling, you’re gonna need a stack to keep track of operators and some serious loop action to read through the expression.
This program show how to convert a Infix Expression 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
Infix -----> (A+B/C*D)
postfix -----> ABC*D/+
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,
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.
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.
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
Download C Program: Infix Expression to a Postfix Conversion
[sociallocker]
Download C Program: Infix Expression to a Postfix Conversion
Password:codewithc.com
[/sociallocker]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function to check precedence
int precedence(char ch) {
switch(ch) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return 0;
}
// Function to convert infix to postfix
void infixToPostfix(char *exp) {
int i, k = 0;
char stack[100], postfix[100];
for(i = 0; i < strlen(exp); i++) {
if(exp[i] >= 'a' && exp[i] <= 'z') {
postfix[k++] = exp[i];
} else {
while(precedence(stack[strlen(stack) - 1]) >= precedence(exp[i])) {
postfix[k++] = stack[strlen(stack) - 1];
stack[strlen(stack) - 1] = '\0';
}
stack[strlen(stack)] = exp[i];
stack[strlen(stack) + 1] = '\0';
}
}
while(strlen(stack) > 0) {
postfix[k++] = stack[strlen(stack) - 1];
stack[strlen(stack) - 1] = '\0';
}
postfix[k] = '\0';
printf("Postfix expression: %s\n", postfix);
}
// Main function
int main() {
char exp[] = "a+b*c-d/e";
printf("Infix expression: %s\n", exp);
infixToPostfix(exp);
return 0;
}
How’s This Work? ?♀️
- Precedence Function: Determines the priority of operators. The bigger the number, the higher the priority.
- infixToPostfix Function: The magic potion that converts your infix to postfix. We loop through the expression, pushing and popping operators onto a stack based on their precedence.
- Main Function: This is where your program starts. We define an infix expression and call
infixToPostfix
to do the heavy lifting.
In Closing ?
And there you go, sweethearts! You’ve just dipped your toes into the fascinating world of expression conversion. It might seem a bit overwhelming at first, but once you get the hang of it, it’s as satisfying as nailing a tough yoga pose. ?♀️