#include<stdio.h>
#include<conio.h>
#include<ctype.h>
struct stack
{
int top;
char operator[40];
};
int main (void)
{
struct stack s;
char input[40],optop,output[40];
int i,j=0,result,op1,op2,value;
s.top =-1;
clrscr();
printf("Enter in INFIX form: ");
i=0;
while((input[i]=getchar())!='\n')
{
if(isalpha(input[i]))
{
output[j]=input[i];
j++;
}
else
{
while(!empty(&s) && prcd(s.operator[s.top],input[i]))
{
optop=s.operator[s.top];
pop(&s);
output[j]=optop;
j++;
}
if(empty(&s)||input[i]!= ')')
push(&s,input[i]);
else
pop(&s);
}
i++;
}
while(!empty(&s))
{
optop=s.operator[s.top];
pop(&s);
output[j]=optop;
j++;
}
printf("Postfix form is:\n");
output[j]='\0';
j=0;
while(output[j]!='\0')
{
printf("%c",output[j]);
j++;
}
getch();
}
push(ps,n)
struct stack *ps;
int n;
{
ps->top++;
ps->operator[ps->top]=n;
}
pop(ps)
struct stack *ps;
{
ps->top--;
}
empty(ps)
struct stack *ps;
{
if(ps->top==-1)
return 1;
else
return 0;
}
prcd(stktop,op)
char stktop,op;
{
if((stktop=='+'||stktop=='-')&&(stktop=='*'||stktop=='/'))
return 0;
if(stktop=='(')
return 0;
if(stktop!=')'&& op=='(')
return 0;
if(stktop!='('&& op==')')
return 1;
}
Related posts: