top of page

C Program to convert infix expression to post-fix expression

  • Writer: Jino Shaji
    Jino Shaji
  • Jan 2, 2015
  • 1 min read

Updated: Jun 9, 2020



#include
#include
#include
#include


int priority(char c);

void main()
{ int top=-1,i=0;
  char a[50],s[50];
  clrscr();
  printf(“enter the infix expression with a ‘)’ as ending\n”);
  gets(a);
  top+=1;
  s[top]='(‘;
  printf(“postfix expression is \n”);
  while(a[i]!=’\0′)
  { if(isdigit(a[i]))
      printf(“%c”,a[i]);
    else
    { if(a[i]=='(‘)
      { top+=1;
       s[top]=a[i];
      }
      else
      { if(a[i]==’)’)
 { while(s[top]!='(‘)
   { printf(“%c”,s[top]);
     top-=1;
   }
   top-=1;
 }
 else
 { while(priority(a[i])<=priority(s[top]))
   { printf(“%c”,s[top]);
     top-=1;
   }
   top+=1;
   s[top]=a[i];
 }
      }
    }
    i=i+1;
  }
  getch();


}
int priority(char c)
{ switch(c)
  { case ‘^’:return 3;
    case ‘/’:return 2;
    case ‘*’:return 2;
    case ‘+’:return 1;
    case ‘-‘:return 1;
  }
  return(0);
}


Output








 
 
 

Comments


bottom of page