C Program for Infix to Postfix Conversion. Here we covert the infix expression to postfix expression by using stack. for example a*b-c/d is the infix expression, and equivalent postfix expression is: ab*cd/-. Read more about C Programming Language .
/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/
#define SIZE 50 /* Size of Stack */
#include <ctype.h>
char s[SIZE];
int top = -1; /* Global declarations */
push(char elem) { /* Function for PUSH operation */
s[++top] = elem;
}
char pop() { /* Function for POP operation */
return (s[top--]);
}
int pr(char elem) { /* Function for precedence */
switch (elem) {
case '#':
return 0;
case '(':
return 1;
case '+':
case '-':
return 2;
case '*':
case '/':
return 3;
}
}
main() { /* Main Program */
char infx[50], pofx[50], ch, elem;
int i = 0, k = 0;
printf("nnRead the Infix Expression ? ");
scanf("%s", infx);
push('#');
while ((ch = infx[i++]) != ' ') {
if (ch == '(')
push(ch);
else if (isalnum(ch))
pofx[k++] = ch;
else if (ch == ')') {
while (s[top] != '(')
pofx[k++] = pop();
elem = pop(); /* Remove ( */
} else { /* Operator */
while (pr(s[top]) >= pr(ch))
pofx[k++] = pop();
push(ch);
}
}
while (s[top] != '#') /* Pop from stack till empty */
pofx[k++] = pop();
pofx[k] = ' '; /* Make pofx as valid string */
printf("nnGiven Infix Expn: %s Postfix Expn: %sn", infx, pofx);
}
Read more Similar C Programs
Data Structures
Learn C Programming
You can easily select the code by double clicking on the code area above.
To get regular updates on new C programs, you can Follow @c_program
You can discuss these programs on our Facebook Page. Start a discussion right now,
our page!
Share this program with your Facebook friends now! by liking it
(you can send this program to your friend using this button)
Like to get updates right inside your feed reader? Grab our feed!
(c) www.c-program-example.com
8 comments on “C Program for Infix to Postfix Conversion.”
program for postfix evaluation with output
Here’s link for C Program for postfix expression evaluation
I will add the sample output in few days. Meanwhile, if it’s urgent drop an email to [email protected] or get it touch with us using our Facebook Page.
not understandable for diploma level students please design another web for diploma level students please
Hi,
I am sorry if some of the program seem complex for you. I am happy to help you. Please get in touch over email or Facebook. Thanks.
Email: [email protected]
Facebook Page: https://www.facebook.com/cprogramexample
can you please combine both postfix evaluation and infix to postfix programs and upload?
Hi Kushboo,
Thanks for asking. I’d be really happy to do that. I will create a new post with what you have asked and share link here.
I appreciate your patience.
Hi Khushboo,
I have posted the program you requested here. C Program For Infix To Postfix Conversion and Evaluation of postfix expression
You can also check it on our Facebook Page and share it. You can also start a discussion there.