Professional Documents
Culture Documents
h>
#include<stdlib.h>
#include<string.h>
#define TRUE 1
#define FALSE 0
#define STACKSIZE 20
struct Stack
{
int top;
int item[STACKSIZE];
};
int main()
{
struct Stack s;
char infix[100];
char postfix[100];
char ch,topsymb;
printf("enter the infix expression\n");
gets(infix);
int i,j=0;
s.top=-1;
for(i=0;infix[i]!='\0';i++)
{
ch=infix[i];
if(isdigit(ch)||isalpha(ch)){
postfix[j++]=ch;}
else
{
while(!empty(&s) && prcd(peek(&s),ch))
{
topsymb=pop(&s);
postfix[j++]=topsymb;
}
if(ch!=')')
push(&s,ch);
else
topsymb=pop(&s);
}
}
while(!empty(&s))
{
topsymb=pop(&s);
postfix[j++]=topsymb;
}
postfix[j]='\0';
int k;
for(k=0;k<j;k++)
{
if(postfix[k]=='(' || postfix[k]==')')
{
printf("unequal brackets\n");
exit(0);
}
}
printf("the postfix expression:%s\n",postfix);
}