Created
September 23, 2016 19:58
-
-
Save priyadarshitathagat/0f3f1b282ce21185db602e4ec826e321 to your computer and use it in GitHub Desktop.
Program to convert Infix expression to Postfix expression in c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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