Infix to Postfix Conversion and Evaluation in C

Description

 This program is used to Convert Infix Expression (eg. a+b) to Postfix Expression (eg. ab+) and Evaluation of the Expression 

Note : This program works only for one digit operands.

#include<stdio.h>
void postfix();
char stack[50];
int s[50];
char post[50];
int p = -1;
int top = -1;
int rtop = -1;
int xtop=-1;      
 
xpush(int elem)
{     
 s[++xtop]=elem;                
}

int xpop()
{                      
 return(s[xtop--]);
}

void push(char x)
{
 stack[++top] = x;
}
 
char pop()
{
    if(top == -1)
        return -1;
    else
     return stack[top--];
}
 
int priority(char x)
{
    if(x == '(')
        return 0;
    if(x == '+' || x == '-')
        return 1;
    if(x == '*' || x == '/')
        return 2;
}

main()
{
 int choice;
 do
 {
  printf("\n\n----MENU----\n1.INFIX - POSTFIX\n2.EXIT\n");
  printf("Enter your choice\n");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1 : postfix();break;
   case 2 : printf("~~~~~ Thank You ~~~~~\n");break;
   default: printf("INVALID INPUT !!!!\n");
  }
 }while(choice!=2);
}

void postfix()
{
 p=-1;
    char exp[20];
    char *e, x;
    printf("Enter the expression :: ");
    scanf("%s",exp);
    printf("\nPostfix expression :: ");
    e = exp;
    while(*e != '\0')
    {
        if(isalnum(*e))
        {
   printf("%c",*e);
            post[++p]=*e;
        }
        else if(*e == '(')
      push(*e);
      
  else if(*e == ')')
        {
            while((x = pop()) != '(')
         {
    printf("%c", x);
          post[++p]=x;
         }
        }
     else
     {
         while(priority(stack[top]) >= priority(*e))
         {
          char a;
          a= pop();
       printf("%c",a);
             post[++p]=a;
         }
         push(*e);
     }
        e++;
    }
 while(top != -1)
 {
  char b;
  b=pop();
     printf("%c",b);
     post[++p]=b;
 }
    
///////////evaluation//////////
 int i=0,op1,op2;
 char ch;
 while(i != (p+1))
 {
  ch=post[i];
  i++;
  if(isdigit(ch))
   xpush(ch-'0'); 
  else
  {        
   op2=xpop();
   op1=xpop();
   switch(ch)
   {
   case '+':xpush(op1+op2);break;
   case '-':xpush(op1-op2);break;
   case '*':xpush(op1*op2);break;
   case '/':xpush(op1/op2);break;
   }
  } 
 }
   printf("\n\nResult after Evaluation: %d\n",s[xtop]);
}

OUTPUT

Infix to Postfix Output

Labels: