Search code examples
cstackpostfix-notationinfix-notation

Converting from Infix to Postfix and evaluating Postfix notation


I'm writing a program that reads an Infix notation, converts it to Postfix and then evaluate that Postfix. Here's my program:

#include<stdio.h> 
#include <ctype.h>
#define SIZE 50            /* Size of Stack */

char s[SIZE];
int top = -1; /* Global declarations */

push(char elem) { /* Function for PUSH operation */
 s[++top] = elem;
}

char pop() { /* Function for POP operation */
 return (s[top--]);
}

int pr(char elem) { /* Function for precedence */
 switch (elem) {
 case '#':
  return 0;
 case '(':
   return 1;
 case '+':
 case '-':
  return 2;
 case '*':
 case '/':
  return 3;
 }
}
pushit(int ele){                       /* Function for PUSH operation */
 s[++top]=ele;
}

int popit(){                      /* Function for POP operation */
 return(s[top--]);
}

 main() { /* Main Program */
  char infx[50], pofx[50], ch, elem;
 int i = 0, k = 0, op1, op2,ele;
 printf("\n\nRead the Infix Expression   ");
 scanf("%s", infx);
 push('#');
 while ((ch = infx[i++]) != '\0') {
  if (ch == '(')
   push(ch);
  else if (isalnum(ch))
   pofx[k++] = ch;
  else if (ch == ')') {
   while (s[top] != '(')
    pofx[k++] = pop();
   elem = pop(); /* Remove ( */
  } else { /* Operator */
   while (pr(s[top]) >= pr(ch))
    pofx[k++] = pop();
   push(ch);
  }
 }
  while (s[top] != '#') /* Pop from stack till empty */
  pofx[k++] = pop();
 pofx[k] = '\0'; /* Make pofx as valid string */
 printf("\n\nGiven Infix Expn: %s  Postfix Expn: %s\n", infx, pofx);

 while( (ch=pofx[i++]) != '\0')
 {
  if(isdigit(ch)) pushit(ch-'0'); /* Push the operand */
  else
  {        /* Operator,pop two  operands */
   op2=popit();
   op1=popit();
   switch(ch)
   {
   case '+':pushit(op1+op2);break;
   case '-':pushit(op1-op2);break;
   case '*':pushit(op1*op2);break;
   case '/':pushit(op1/op2);break;
   }
  }
 }
 printf("\n Given Postfix Expn: %s\n",pofx);
 printf("\n Result after Evaluation: %d\n",s[top]);
}

The program converts my Infix to a Postfix notation correctly. However, for the evaluation part, it always returns 0 as a result.

Also, when converting from Infix to Postfix , I would like to print the result in each step, how can I do that?


Solution

  • One problem is your are storing values in s as a char with storage of 1 byte per element and then attempt to push integers into s with:

    pushit (int ele) {      /* Function for PUSH operation */
        s[++top] = ele;
    }
    

    After mixing int/char in s, you attempt to read:

    op2=popit();
    op1=popit();
    

    which attempts to create an int from popit(). popit() is simply a 1 byte char. So op1 and op2 are not getting the values you want:

    int popit(){                      /* Function for POP operation */
    return(s[top--]);
    }
    

    You need to look at how your are storing integers if you expect to get integers back. Lastly, look at your warnings. At a minimum, build with the -Wall option. It reveals:

    popit.c:8:1: warning: return type defaults to ‘int’
    popit.c:32:1: warning: return type defaults to ‘int’
    popit.c:41:1: warning: return type defaults to ‘int’
    

    Which may be what you intended. However, your code should build without warning to help insure it is doing what you think it is doing.