#include<stdio.h>
#include<conio.h>
#include
<ctype.h>
#define SIZE 50
char s[SIZE];
int top=-1;
push(char c)
{
s[++top]=c;
}
char pop()
{
return(s[top--]);
}
int priority(char
c)
{
switch(c){
case '$':
return 0;
case '(':
return 1;
case '+':
case '-':
return 2;
case '*':
case '/':
return 3;
case '^':
return 4;}
}
main()
{
char
infix[50],postfix[50],ch,c;
int i=0,j=0;
printf("\nEnter the infix
expression=\t");
scanf("%s",infix);
push('$');
while((ch=infix[i++])!='\0')
{
if(ch =='(')
push(ch);
else if(isalnum(ch))
postfix[j++]=ch;
else
if( ch ==')'){
while(s[top]
!='(')
postfix[j++]=pop();
c=pop();
}
else{
while(priority(s[top])>=priority(ch))
postfix[j++]=pop();
push(ch);
}
}
while( s[top] != '$')
postfix[j++]=pop();
postfix[j]='\0';
printf("\n\nThe Postfix Expn:
%s\n",postfix);
getch();
}
No comments:
Post a Comment