#include<stdio.h>
#include<math.h>
char inf[30],postf[30];
int st[30],i=0,j=0,top=-1;
void push(int);
char pop();
main()
{
 clrscr();
 st[top]=0;
 printf("Enter infix expression : ");
 gets(inf);
 while(inf[i]!=NULL)
 {
 switch(inf[i])
 {
  case '(':
   push(0);
   break;
  case '+':
   while(st[top]>=1)
   {
    postf[j++]=pop();
   }
   push(1);
   break;
  case '-':
   while(st[top]>=1)
   {
    postf[j++]=pop();
   }
   push(2);
   break;
  case '*':
   while(st[top]>=3)
   {
    postf[j++]=pop();
   }
   push(3);
   break;
  case '/':
   while(st[top]>=3)
   {
    postf[j++]=pop();
   }
   push(4);
   break;
  case '^':
   while(st[top]>=5)
   {
    postf[j++]=pop();
   }
   push(5);
   break;
  case ')':
   while(st[top]!=0)
    postf[j++]=pop();
   top--;
   break;
  default:
  postf[j++]=inf[i];
 }
 i++;
 }
 while(top>-1)
 {
  postf[j++]=pop();
 }
 postf[j]=NULL;
 printf("Postfix expression : ");
 puts(postf);
 getch();
}
void push(int p)
{
 st[++top]=p;
}
char pop()
{
 int op;
 op=st[top--];
 switch(op)
 {
  case 1:
 return '+';
  case 2:
 return '-';
  case 3:
 return '*';
  case 4:
 return '/';
  case 5:
 return '^';
 }
}

OUTPUT


Enter infix expression : A + (B * C)
Postfix expression : B C * A +

Leave a Reply

Subscribe to Posts | Subscribe to Comments

All Notes on BCA

All Notes  on BCA
BCA all subjects notes

Total Pageviews

Translate

Powered by Blogger.

Copyright © All Notes on BCA