Program to convert infix expression to postfix expression c programing

Program to convert infix expression to postfix expression. 


#include<stdio.h>
#include<conio.h>
#define max 100
#include<ctype.h>
#include<string.h>
#include<stdlib.h>

int isoper(char ch);
int precedence(char);
void main()
{clrscr();
      int i,j=0,len;
  char infix[max],postfix[max], stack[max];
  int top=-1;
   printf(" Enter infix");
   gets(infix);
   len=strlen(infix);
   for (i=0; i<len; i++)
   {
 if(isalpha(infix[i]))
 postfix[j++]=infix[i];
 else if(isoper(infix[i]))
 {
  while(top>-1 && stack[top]!='(')
  {
     if(precedence(infix[i])<=precedence(stack[top]))
   {
    postfix[j++]=stack[top--];
   }
   else break;
  }

   stack[++top]=infix[i];
 }
   else if(infix[i]=='(')
   stack[++top]=infix[i];

   else if(infix[i]==')')
         {       while (stack[top]!='(')
         { postfix[j++]=stack[top--];
         }
    top--;

   }
     }       while (top>-1)
       postfix[j++]=stack[top--];
       postfix[j++]='\0';
       printf("Infix expression %s", infix);
       printf("\n postfix expression %s", postfix);

       getch();
}

int precedence(char ch)
{
 int prcd;
 if(ch=='+'|| ch == '-')
 prcd=0;
 else if(ch=='*'|| ch=='/')
 prcd=1;
 else if(ch=='$')
 prcd=2;
 else
 {
  printf("Invalid operator");
  exit(0);
 }
 return prcd;
}
int isoper(char ch)
{
 if(ch=='+' || ch=='-'|| ch=='*' || ch== '/'|| ch=='$')
 return 1;
 else
 return 0;







}


OUTPUT


Powered by Blogger.