Thursday, April 21, 2016

C program to convert an infix expression into postfix and calculate result using postfix notations

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<process.h>
#include<math.h>
#define maxsize 50
int top=-1;
int stack[maxsize];
int y;
char postfix[50][50];
void push(int digit)
 {
  if(top==maxsize-1)
   {
    printf("\nOverflow condition");
    exit(0);
   }
  top++;
  stack[top]=digit;
 }
int pop()
{
 if(top==-1)
  {
   printf("\nUnderflow Condition");
   exit(0);
  }
 return(stack[top--]);
}
char operator_stack[50];
int operator_top=-1;
int isoperator(char s)
 {
  if(s=='^'||s=='*'||s=='/'||s=='%'||s=='+'||s=='-')
   {
    return 1;
   }
  else
   {
    return 0;
   }
 }
int isparenthesis(char pa)
 {
  if(pa=='(')
   return (1);
  else if(pa==')')
   return(2);
  else
   return(0);
 }
void operator_push(char n)
{
 if(operator_top==49)
  {
   printf("\Overflow condition");
   exit(0);
  }
 operator_stack[++operator_top]=n;
}
int priority(char p)
 {
  if(p=='^')
   return 3;
  else if(p=='*'||p=='/'||p=='%')
   return 2;
  else if(p=='+'||p=='-')
   return 1;
  else
   return 0;
 }
char operator_pop()
 {
  if(operator_top==-1)
   {
    printf("\nUnderflow condition");
    exit(0);
   }
  return(operator_stack[operator_top--]);
 }
void calculate_postfix()
{
 int i,j,k;
 int res;
 for(i=0;i<y;i++)
  {
   if(isdigit(postfix[i][0]))
    {
     int digit;
     digit=0;
     for(int c=0;postfix[i][c]!='\0';c++)
      {
       digit=digit*10+(postfix[i][c]-48);
      }
     push(digit);
    }
   else if(isoperator(postfix[i][0]))
    {
     k=pop();
     j=pop();
     switch(postfix[i][0])
     {
      case '^':
       res=pow(j,k);
       break;
      case '*':
       res=j*k;
       break;
      case '/':
       res=j/k;
       break;
      case '%':
       res=j%k;
       break;
      case '+':
       res=j+k;
       break;
      case '-':
       res=j-k;
       break;
     }
     push(res);
     }
  }
 printf("\nResult=%d",stack[top]);
}

void infix_to_postfix1()
{
 int x;
 char *exp_infix;
 //char postfix[50][50];
 int digit;
 char dig[50];
 printf("\nEnter the infix notation");
 scanf("%[^\n]s",exp_infix);
 for(x=0,y=0;exp_infix[x]!='\0';)
  {
   if(isdigit(exp_infix[x]))
    {
     digit=0;
     while((exp_infix[x]>=48)&&(exp_infix[x]<=57))
      {
       dig[digit]=exp_infix[x];
       x++;
       digit=digit+1;
      }
     dig[digit]='\0';
     strcpy(postfix[y],dig);
     y++;
    }
    else if(isoperator(exp_infix[x])||isparenthesis(exp_infix[x]))
    {
     if(operator_top==-1||isparenthesis(exp_infix[x])==1)
      {
       operator_push(exp_infix[x]);
       x++;
      }
     else
      {
       if(isparenthesis(exp_infix[x])==2)
{
while(operator_stack[operator_top]!='(')
 {
  digit=0;
  dig[digit]=operator_pop();
  digit=digit+1;
  dig[digit]='\0';
  strcpy(postfix[y],dig);
  y++;
 }
operator_pop();
x++;
continue;
}
       if(priority(exp_infix[x])<priority(operator_stack[operator_top]))
{
while((priority(exp_infix[x])<priority(operator_stack[operator_top])&&(operator_top>-1)))
 {
  digit=0;
  dig[digit]=operator_pop();
  digit=digit+1;
  dig[digit]='\0';
  strcpy(postfix[y],dig);
  y++;
 }
operator_push(exp_infix[x]);
x++;
}
else if(priority(exp_infix[x])==priority(operator_stack[operator_top]))
{
  digit=0;
  dig[digit]=operator_pop();
  digit=digit+1;
  dig[digit]='\0';
  strcpy(postfix[y],dig);
  y++;
  operator_push(exp_infix[x]);
  x++;
}
       else if(priority(exp_infix[x])>priority(operator_stack[operator_top]))
{
operator_push(exp_infix[x]);
x++;
}
      }
    }
  }
while(operator_top!=-1)
   {
    digit=0;
    dig[digit]=operator_pop();
    digit++;
    dig[digit]='\0';
    strcpy(postfix[y],dig);
    y++;
   }
  for(x=0;x<y;x++)
   {
      printf("%s",postfix[x]);
   }
  calculate_postfix();
}
void main()
{
 infix_to_postfix1();
 getch();
}
output
Enter the infix notation 5+7
infix notation 57+
result 12

No comments:

Post a Comment