0
1.7kviews
Write a C program that use stack operations to convert infix expression to postfix expression.

Test the program for the following Infix Expressions:

1] A + B

2] A + B * C - D

1 Answer
0
170views

Infix to Postfix Conversion using the Stack Operations

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
char pop();
void push(char);
int pri(char);
char st[10];
int top=-1;
void main()
{
char s[25];
int i;
printf("Enter Infix Expression: ");
gets(s);
push('(');
strcat(s,")");
for(i=0;i<s[i];i++)
{
if(isalnum(s[i]))
printf("%c",s[i]);
else if(s[i]=='(')
push(s[i]);
else if(s[i]==')')
{
 while(st[top]!='(')
 printf("%c",pop());
 pop();
 }
 else
 {
 while(pri(s[i])<=pri(st[top]))
 printf("%c",pop());
 push(s[i]);
 }
 }
 getch();
 }
 void push(char e)
 {
 st[++top]=e;
 }
 char pop()
 {
 char ch;
 ch=st[top];
 top--;
 st[top+1]='\0';
 return(ch);
 }
 int pri(char p)
 {
 switch(p)
 {
 case '(':return 0;
 case '+':
 case '-':return 1;
 case '/':
 case '%':
 case '*':return 2;
 }
 }

OUTPUT -

Output

Please log in to add an answer.