Skip to content

Instantly share code, notes, and snippets.

@priyadarshitathagat
Created September 23, 2016 19:58
Show Gist options
  • Save priyadarshitathagat/0f3f1b282ce21185db602e4ec826e321 to your computer and use it in GitHub Desktop.
Save priyadarshitathagat/0f3f1b282ce21185db602e4ec826e321 to your computer and use it in GitHub Desktop.
Program to convert Infix expression to Postfix expression in c
#include<stdio.h>
#define max 100
int top=-1, a[max];
void push(char x)
{
a[++top]=x;
}
char pop()
{ if(top==-1)
return -1;
else
return a[top--];
}
int prcd(char c)
{ if(c=='(')
return 0;
else if(c=='+'||c=='-')
return 1;
else if(c=='*'||c=='/')
return 2;
}
int infixtopostfix(char infix[max],char postfix[max])
{
char temp,x;
int i=0,j=0;
while(infix[i]!='\0')
{
temp=infix[i];
if(isalnum(temp))
{
postfix[j++]=temp;
}
else if(temp=='(')
push(temp);
else if(temp==')')
{
while((x=pop())!='(')
{
postfix[j++]=x;
}
}
else
{ while(prcd(a[top])>=prcd(temp))
{postfix[j++]=pop();}
push(temp);
}
i++;
}
while(top!= -1)
postfix[j++]=pop();
postfix[j]='\0';
}
main()
{
char infix[max],postfix[max];
printf("Enter the infix expression\n");
gets(infix);
printf("The infix expression is %s\n",infix);
infixtopostfix(infix, postfix);
printf("The postfix expression is %s\n",postfix);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment