Created
March 12, 2009 10:11
-
-
Save anshumanatri/77990 to your computer and use it in GitHub Desktop.
This file contains hidden or 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> | |
#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