Code Scrappers


Infix To Postfix - C Program


                    #include<stdio.h>

                    #include<stdlib.h>

                    #include<ctype.h>

                    int prec(char);

                    char pop();

                    int push(char);

                    char stack[40];

                    int top=-1;

                      void main()
                      {
                          char infx[30], pfix[30];

                          char c , ele;

                          int i=0, k=0;

                          printf("\n\nEnter infix expression : ");

                          scanf("%s",infx);

                          push('#');

                          while( (c = infx[i++]) != '\0')
                          {
                              if( c == '(')
                              {
                                  push(c);
                              }

                              else
                              {
                                  if(isalnum(c))
                                  {
                                      pfix[k++] = c;
                                  }

                                  else
                                  {
                                      if( c == ')')
                                      {
                                          while( stack[top] != '(')
                                          {
                                              pfix[k++] = pop();
                                          }
                                          ele = pop();
                                      }
                                      else
                                      {
                                          while( prec(stack[top]) >= prec(c) )
                                          {
                                              pfix[k++] = pop();
                                          }

                                          push(c);
                              }     }   }
                          }
                          while( stack[top] != '#')
                          {
                              pfix[k++] = pop();
                          }

                          pfix[k]='\0';

                          printf("\npostfix expression =  %s\n",pfix);
                      }



                    int push(char ele)
                    {
                        stack[++top] = ele;

                        return 0;

                    }

                    char pop()
                    {
                        return(stack[top--]);
                    }

                    int prec(char ele)
                    {
                        switch(ele)
                        {

                        case '#': return 0;

                        case '(': return 1;

                        case '+': return 2;

                        case '-': return 2;

                        case '*': return 3;

                        case '/': return 3;
                        }
                    }


                



Output

          Read the Infix Expression : 5+2-6-7

          Postfix Expn: 52+6-7-