Skip to content

Instantly share code, notes, and snippets.

@anshumanatri
Created March 12, 2009 10:11
Show Gist options
  • Save anshumanatri/77990 to your computer and use it in GitHub Desktop.
Save anshumanatri/77990 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
int i,j=-1,top=-1;
clrscr();
char in[30], po[30],stack[20];
printf("\n Enter the infix expression ");
gets(in);
for(i=0;in[i]!='\0';i++)
{
if(isalpha(in[i]))
{
po[++j]=in[i];
po[j+1]='\0';
}
else
switch(in[i])
{
case '(' :
stack[++top] = in[i];stack[top+1]='\0';
break;
case '+':
case '-':
while(stack[top]!='('&&top>-1)
po[++j]=stack[top--];
stack[++top]=in[i];stack[top+1]='\0';
break;
case '*' :
case '/' :
while((stack[top]!='('&&top>-1)&&((stack[top]=='*')||(stack[top]=='/')))
po[++j]=stack[top--];po[j+1]='\0';
stack[++top]=in[i];stack[top+1]='\0';
break;
case ')' :
while(stack[top]!='(')
{ po[++j] = stack[top--];
po[j+1]='\0'; }
top=top-1;
} }
while(top!=-1&&stack[top]!='(')
po[++j]=stack[top--];
po[j+1]='\0';
printf("\n postfix expression is:");
puts(po);
getch();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment