#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#define MAX 50
struct stack{ int top; char stkel[MAX];};
void postfix(char *,char *);
int prec(char c);
char stacktop(struct stack*s);
void push(struct stack *s, char c);
int isempty(struct stack *s);
char pop(struct stack *s);

void main()
{
 char inex[MAX],post[MAX];
 clrscr();
 printf("\nEnter infix expression:\n\t");
 gets(inex);
 postfix(inex,post);
 printf("\nPostfix expression is %s", post);
 getch();
}
void postfix(char inex[],char post[])
{
 int i=0,j=0;
 char ch,topsymb;
 struct stack *s;
 s->top=-1;
 while((ch=inex[i++])!='\0')
 {
  if(isalnum(ch)) post[j++]=ch;
  else
  { while(isempty(s)&&(prec(stacktop(s))<prec(ch))&&(ch=='('))
    push(s,ch);
  while(!isempty(s)&&(prec(stacktop(s))>=prec(ch))&&(ch!='('))
  {
   topsymb=pop(s);
   if(topsymb!='(') post[j++]=topsymb;
  } /*End of while*/
  if(ch!=')') push(s,ch);
 }  /*End of else*/
}  /*End of while*/
 while(!isempty(s)) post[j++]=pop(s);
 post[j++]='\0';
}   /*End of postfix function*/

void push(struct stack * s,char c)
{
 if (s->top==(MAX-1)) {printf("stack overflow"); }
 else
  { s->stkel[++s->top]=c;
    return;
 }
}
char pop(struct stack *s)
{
 if(s->top==-1) {printf("Stack underflow"); return(0);};
 return s->stkel[s->top--];
}

char stacktop(struct stack *s)
{ return s->stkel[s->top]; }
int prec(char c)
{ int k;
 switch(c)
 {
  case'(' :
  case ')': k=0; break;
  case'+' :
  case'-' : k=1; break;
  case'*' :
  case'/' : k=2; break;
  case'$' : k=3; break;
 }
 return k;
}
int isempty(struct stack *s)
{ return(s->top==-1); }



