Advertisement
Sathvikks8

Infix-to-Postfix.c

Sep 25th, 2020 (edited)
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.19 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<ctype.h>
  4. #define MAX 100
  5. char infix[200], postfix[200], STACK[MAX];
  6. int top=-1;
  7. int isEmpty()
  8. {
  9.   if(top==-1)
  10.     return 1;
  11.   else
  12.     return 0;
  13. }
  14. int isFull()
  15. {
  16.   if(top==MAX-1)
  17.     return 1;
  18.   else
  19.     return 0;
  20. }
  21. void PUSH(char ch)
  22. {
  23.   if(isFull())
  24.   {
  25.     printf("\nStack Overflow");
  26.     exit(0);
  27.   }
  28.   else
  29.     STACK[++top]=ch;
  30. }
  31. char POP()
  32. {
  33.   if(isEmpty())
  34.   {
  35.     printf("\nStack Underflow");
  36.     exit(0);
  37.   }
  38.   else
  39.   {
  40.     char ch=STACK[top--];
  41.     return (ch);
  42.   }
  43. }
  44. int precedence(char ch)
  45. {
  46.   if(ch=='(' || ch==')')
  47.     return 0;
  48.   if(ch=='^')
  49.     return 3;
  50.   else if (ch=='*' || ch=='/' || ch=='%')
  51.     return 2;
  52.   else if(ch=='+' || ch=='-')
  53.     return 1;
  54.   else
  55.   {
  56.     printf("\nUnknown operator entered\n");
  57.     exit(0);
  58.   }
  59. }
  60. void infix_to_postfix()
  61. {
  62.   int i=0, j=0; char ch, tmp;
  63.   for(;infix[i]!='\0';i++)
  64.   {
  65.     ch=infix[i];
  66.     if(ch==' ')
  67.       continue;
  68.     if(isalnum(ch))
  69.       postfix[j++]=ch;
  70.     else
  71.     {
  72.       if(isEmpty())
  73.       {
  74.         precedence(ch);
  75.         PUSH(ch);
  76.         continue;
  77.       }
  78.       if(!isFull())
  79.       {
  80.         if(ch=='(')
  81.         {
  82.           PUSH(ch);
  83.           continue;
  84.         }
  85.         else if(ch==')')
  86.         {
  87.           tmp=POP();
  88.           while(tmp!='(')
  89.           {
  90.             postfix[j++]=tmp;
  91.             tmp=POP();
  92.           }
  93.           continue;
  94.         }
  95.         int pch=precedence(ch), pst=precedence(STACK[top]);
  96.         while(pch<=pst)
  97.         {
  98.           if(isEmpty())
  99.             break;
  100.           tmp=POP();
  101.           if(tmp=='(')
  102.           {}
  103.           else
  104.             postfix[j++]=tmp;
  105.           if(top==-1)
  106.           {
  107.             pst=-1;
  108.             continue;
  109.           }
  110.           pst=precedence(STACK[top]);
  111.         }
  112.         PUSH(ch);
  113.       }
  114.       else
  115.       {
  116.         printf("\nStack Overflow. Please reduce the equation length");
  117.         exit(0);
  118.       }
  119.     }
  120.   }
  121.   while(!isEmpty())
  122.     postfix[j++]=POP();
  123. }
  124. int main()
  125. {
  126.   printf("\nEnter the Infix Expression: ");
  127.   gets(infix);
  128.   infix_to_postfix();
  129.   printf("\nThe expression after converting to Postfix is: %s",postfix);
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement