The BOTTOM LINE Quote Of The Day

The BOTTOM LINE Quote Of The Day

Don't Ever Tell GOD How BIG Your Problems are.
Just Tell Your Problems How BIG your GOD is ;)

Wednesday, January 26, 2011

Infix-Postfix Conversion and Expression Evalution



#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
#define max 50

char infx[max];
char pstfx[max];
char stack[max];
int nstore[max];
int nstack[max];

int top;
int cnt = 0;

int set_prcdnce(char ch)
{
 switch(ch)
 {
 case '^' : return 3;
 case '*' : return 2;
 case '/' : return 2;
 case '+' : return 1;
 case '-' : return 1;
 default  : return 0;
 }
}

int evaluate(char opt)
{
 int a,b;
 b = nstack[top]; top-=1;
 a = nstack[top];
 switch(opt)
 {
 case '^' : return pow(a,b);
 case '*' : return a*b;
 case '/' : return a/b;
 case '+' : return a+b;
 case '-' : return a-b;
 }
}

void stck_stat(char ch,int cho)
{
 int i;
 if(isalpha(ch) && cho == 2)
  {
  printf("  %d \t",nstore[cnt]);  cnt++;
  }
 else
  printf("  %c \t",ch);
 for(i=0;i<=top;i++)
  {
  if(cho==1)
   printf(" %c",stack[i]);
  else
   printf(" %d",nstack[i]);
  }
}

void main()
{
clrscr();
int flag,i,tb=0,j=0;
int cpd,ppd;
 do
 {
  flag = 0;
  printf("\n Enter the infx expression : ");
  gets(infx);
  if(strlen(infx) > max )
  {
   printf("\n\n WARNING : Exceeding the limits !! \n\n");
   flag = 1;
  }
  for(i=0;infx[i]!='\0';i++)
   {
    if(infx[i]=='(')
     tb++;
    if(infx[i]==')')
     tb--;
   }
  if(tb != 0)
  {
   printf("\n\n WARNING : Unequal Right n Left Brackets in the expression !! \n\n");
   flag = 1;
  }
 }while(flag == 1);
printf("\n\n The expression in its infix form : ");
puts(infx);
int x = strlen(infx);
infx[x] = ')';   infx[x+1] = '\0';
x+=1;
top = 0;
stack[top] = '(';
printf("\n\n     Element\t  Stack Status\t  Postfix Expression");
printf("\n     -------- \t ---------------  -------------------- \n\n");
for(i=0;i<strlen(infx);i++)
   {
    switch(infx[i])
    {
    case '(' :  top += 1;
stack[top] = infx[i];
break;
    case ')' :  while(stack[top] != '(')
{
pstfx[j] = stack[top];
top -= 1; j++;
}
top -= 1;
break;
    case '^' :
    case '/' :
    case '*' :
    case '+' :
    case '-' :  cpd = set_prcdnce(infx[i]);
ppd = set_prcdnce(stack[top]);
if(cpd < ppd)
{
while(cpd<=ppd)
{
if(top == -1) break;
pstfx[j] = stack[top];
top -= 1; j++;
ppd = set_prcdnce(stack[top]);
}
}
top += 1; stack[top] = infx[i];
break;
    default :   pstfx[j] = infx[i]; j++;
break;
    }
   printf("  %-2d. ",i+1);
   stck_stat(infx[i],1);
   gotoxy(40,11+i);
   puts(pstfx);
   }
pstfx[j] = '\0';
printf("\n\n The expression in postfix form : ");
puts(pstfx);
j=0;
for(i=0;i<strlen(pstfx);i++)
 {
  if(isalpha(pstfx[i]))
   {
     printf("\n Enter value for %c :",pstfx[i]);
     scanf("%d",&nstore[j]);  j++;
   }
 }

j=0;
printf("\n\n Now, The postfix expression becomes as : ");
for(i=0;i<strlen(pstfx);i++)
 {
  if(isalpha(pstfx[i]))
   {
     printf("%d,",nstore[j]);  j++;
   }
  else
   printf("%c,",pstfx[i]);
 }
printf("\n\n Evaluating the stack as follows : ");
printf("\n\n Element Check \t Stack Status");
printf("\n ----------- \t ------------ \n");
j = 0;   top = -1;
for(i=0;i<strlen(pstfx);i++)
 {
 printf("\n %-2d ",i+1);
 switch(pstfx[i])
    {
    case '^' :
    case '/' :
    case '*' :
    case '+' :
    case '-' :  nstack[top] = evaluate(pstfx[i]);
break;
    default :   top +=1; nstack[top] = nstore[j]; j+=1;
break;
    }
 stck_stat(pstfx[i],2);
 }
printf("\n\n After, Evaluating the expression the result is : %d",nstack[top]);
getch();
}

No comments:

Post a Comment