Stack Program #3

17. Write a program to convert in-fix notation to post-fix notation using Stack.

#include<stdio.h>
#define N 50
char s[N];
int top=-1;
void push(char);
char pop();
int f(char);
int g(char);
int r(char);
void main()
{
     int i=0,j=0,rank;
     char infix[50],next,temp,x,polish[50];
     top=0;
     s[top]='(';
     polish[50]='\0';
     rank=0;
     printf("Enter a infix expression with extra closing bracket:\n");
     scanf("%s",infix);
     next=infix[i];
     while(next!='\0')
     {
          if(top<0)
          {
               printf("Invalid\n");
               return;
          }
          while(g(s[top])>f(next))
          {
               temp=pop();
               polish[j]=temp;
               j++;
               rank=rank+r(temp);
               if(rank<1)
               {
                    printf("Invalid\n");
                    return;
               }
          }
          if(g(s[top])!=f(next))
          {
               push(next);
          }
          else
          {
               pop();
          }
          i++;
          next=infix[i];
     }
     if(top!=-1 || rank!=1)
     {
           printf("Invalid\n");
     }
     else
     {
           printf("Valid\n");
     }
     polish[j]='\0';
     printf("Postfix of %s is %s \n",infix,polish);
}
void push(char x)
{
     if(top>=N-1)
     {

          printf("STACK OVERFLOW\n");
     }
     else
     {
          top++;
          s[top]=x;
     }
}
char pop()
{
     if(top==-1)
     {
          printf("STACK UNDERFLOW\n");
          return('\0');
     }
     else
     {
          top=top-1;
          return(s[top+1]);
     }
}
int f(char t)
{
     if(t=='+' || t=='-')
     {
          return 1;
     }
     if(t=='*' || t=='/')
     {
          return 3;
     }
     if(t=='^' || t=='$')
     {
          return 6;
     }
     if(t>='a' && t<='z' || t>='A' && t<='Z')
     {
          return 7;
     }
     if(t=='(')
     {
          return 9;
     }
     if(t==')')
     {
          return 0;
     }
}
int g(char t)
{
     if(t=='+' || t=='-')
     {
          return 2;
     }
     if(t=='*' || t=='/')
     {
          return 4;
     }
     if(t=='^' || t=='$')
     {
          return 5;
     }
     if(t>='a' && t<='z' || t>='A' && t<='Z')
     {
          return 8;
     }
     if(t=='(')
     {
          return 0;
     }
}
int r(char t)
{
     if(t=='+' || t=='-')
     {
          return -1;
     }
     if(t=='*' || t=='/')
     {
          return -1;
     }
     if(t=='^'|| t=='$')
     {
          return -1;
     }
     if(t>='a' && t<='z' || t>='A' && t<='Z')
     {
          return 1;
     }
}

OR


#include<stdio.h>
#define N 50
char s[N];
int top=-1;
void push(char);
char pop();
int f(char);
int g(char);
int r(char);
void main()
{
     int i=0,j=0,rank;
     char infix[50],next,temp,x,polish[50]={0};
     top=0;
     s[top]='(';
     polish[50]='\0';
     rank=0;
     printf("Enter a infix expression with extra closing bracket:\n");
     scanf("%s",infix);
     printf("%c\t%s\t%s\t%d\n",infix[i],s,polish,rank);
     i=0;
     next=infix[i];
     while(next!='\0')
     {
          if(top<0)
          {
               printf("Invalid\n");
               return;
          }
          while(g(s[top])>f(next))
          {
               temp=pop();
               polish[j]=temp;
               j++;
               rank=rank+r(temp);
               if(rank<1)
               {
                    printf("Invalid\n");
                    return;
               }
          }
          if(g(s[top])!=f(next))
          {
               push(next);
          }
          else
          {
               pop();
          }
          polish[j]='\0';
          printf("%c\t%s\t%s\t%d\n",next,s,polish,rank);
          i++;
          next=infix[i];
     }
     if(top!=-1 || rank!=1)
     {
           printf("Invalid\n");
     }
     else
     {
           printf("Valid\n");
     }
     polish[j]='\0';
     printf("Postfix of %s is %s \n",infix,polish);
}
void push(char x)
{
     if(top>=N-1)
     {
          printf("STACK OVERFLOW\n");
     }
     else
     {
          top++;
          s[top]=x;
     }
}
char pop()
{
     if(top==-1)
     {
          printf("STACK UNDERFLOW\n");
          return('\0');
     }
     else
     {
          top=top-1;
          return(s[top+1]);
     }
}
int f(char t)
{
     if(t=='+' || t=='-')
     {
          return 1;
     }
     if(t=='*' || t=='/')
     {
          return 3;
     }
     if(t=='^' || t=='$')
     {
          return 6;
     }
     if(t>='a' && t<='z' || t>='A' && t<='Z')
     {
          return 7;
     }
     if(t=='(')
     {
          return 9;
     }
     if(t==')')
     {
          return 0;
     }
}
int g(char t)
{
     if(t=='+' || t=='-')
     {
          return 2;
     }
     if(t=='*' || t=='/')
     {
          return 4;
     }
     if(t=='^' || t=='$')
     {
          return 5;
     }
     if(t>='a' && t<='z' || t>='A' && t<='Z')
     {
          return 8;
     }
     if(t=='(')
     {
          return 0;
     }
}
int r(char t)
{
     if(t=='+' || t=='-')
     {
          return -1;
     }
     if(t=='*' || t=='/')
     {
          return -1;
     }
     if(t=='^'|| t=='$')
     {
          return -1;
     }
     if(t>='a' && t<='z' || t>='A' && t<='Z')
     {
          return 1;
     }
}


 Output

  1. Enter a infix expression with extra closing bracket:
    A$B-C*D+E$F/G)
    Valid
    Postfix of A$B-C*D+E$F/G) is AB$CD*-EF$G/+ 

OR

Output

    1.  Enter a infix expression with extra closing bracket:
      A$B-C*D+E$F/G)
      A     (                                                   0
      A     (A                                                 0
      $     ($            A                                   1
      B     ($B          A                                  1
      -      (-B           AB$                              1
      C     (-C          AB$                               1
      *      (-*          AB$C                             2
      D     (-*D        AB$C                            2
      +     (+*D      AB$CD*-                       1
      E     (+ED     AB$CD*-                        1
      $      (+$D     AB$CD*-E                     2
      F     (+$F      AB$CD*-E                      2
      /      (+/F       AB$CD*-EF$                  2
      G     (+/G      AB$CD*-EF$                  2
      )                    AB$CD*-EF$G/+            1
      Valid
      Postfix of A$B-C*D+E$F/G) is AB$CD*-EF$G/+



For download this program please check the link ➡➡ Download OR Download

Comments

Popular

Basic Program #3

Pointer Program #1

Array Program #4

Singly Linked List program #1

Basic program #2