Skip to main content

Infix To Postfix Conversion.

 Infix To Postfix Conversion.


CODE:-1

πŸ‘‡

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAX 20
  4. typedef struct
  5. {
  6. int data[MAX];
  7. int top;
  8. }STACK;

  9. void initstack(STACK *ps)
  10. {
  11. ps->top=-1;
  12. }

  13. void push(STACK *ps, int num)
  14. {
  15. ps->top++;
  16. ps->data[ps->top]=num;
  17. }

  18. int pop(STACK *ps)
  19. {
  20. return(ps->data[ps->top--]);
  21. }

  22. int isempty(STACK *ps)
  23. {
  24. return(ps->top==-1);
  25. }

  26. int isfull(STACK *ps)
  27. {
  28. return(ps->top=MAX-1);
  29. }

  30. void main()
  31. {
  32. void postfix(char in[], char post[]);
  33. char in[20], post[20];
  34. printf("Enter The Fully Parenthesized Infix Expression:\t");
  35. scanf("%s", in);
  36. postfix(in, post);
  37. printf("\n The Postfix String Is: %s",post);
  38. }

  39. void postfix(char in[], char post[])
  40. {
  41. int i, j=0;
  42. char ch1;
  43. STACK s1;
  44. initstack(&s1);
  45. for(i=0; in[i]!='\0'; i++)
  46. {
  47. switch(in[i])
  48. {
  49. case '+':
  50. case '-':
  51. case '*':
  52. case '/':
  53. case '%':
  54. case '(': push(&s1, in[i]);
  55.   break;
  56.   
  57. case ')': while((ch1=pop(&s1)) != '(')
  58.   post[j++]=ch1;
  59.   break;
  60.   
  61. default: post[j++]=in[i];    
  62. }
  63. }
  64. while(!isempty(&s1))
  65. post[j++]=pop(&s1);
  66. post[j]='\0';
  67. }

πŸ‘‰ExecuteπŸ‘ˆ


//OUTPUT
/*
Enter The Fully Parenthesized Infix Expression: (A+B)*(C-D)

 The Postfix String Is: AB+CD-*
*/


CODE:-2

πŸ‘‡

  • //used by taking into considering operators +, -, /, *, %, (, )
    1. #include<stdio.h>
    2. char ifix[200],pfix[200],stack[100];
    3. int p=0,s=0,i=0;
    4. void fun()
    5. {
    6.     if(ifix[i]==')')
    7.     {
    8.         s--;
    9.         while(stack[s]!='(')
    10.             pfix[p++]=stack[s--];
    11.     }
    12.     if(ifix[i]=='+' || ifix[i]=='-')
    13.     {
    14.         if(!s)
    15.             stack[s++]=ifix[i];
    16.         else if(stack[s-1]=='(')
    17.             stack[s++]=ifix[i];
    18.         else
    19.         {
    20.             s--;
    21.             while(s!=-1 && stack[s]!='(')
    22.             {
    23.                 pfix[p++]=stack[s--];
    24.             }
    25.             stack[++s]=ifix[i];
    26.             s++;
    27.         }
    28.     }
    29. }
    30. int main()
    31. {
    32.     printf("Enter your infix expression(using operators +, -, %%, /, *) : ");
    33.     scanf("%s",ifix);
    34.     for(i;ifix[i]!='\0';i++)
    35.     {
    36.         if(ifix[i]!='*' && ifix[i]!='/' && ifix[i]!='-' && ifix[i]!='+' && ifix[i]!='(' && ifix[i]!=')' && ifix[i]!='%')
    37.             pfix[p++]=ifix[i];
    38.         else if(ifix[i]=='(' || ifix[i]=='*' || ifix[i]=='/' || ifix[i]=='%')
    39.             stack[s++]=ifix[i];
    40.         else
    41.             fun();
    42.     }
    43.     if(ifix[i]=='\0')
    44.     {
    45.         if(s)
    46.         {
    47.             s--;
    48.             while(s!=-1)
    49.             {
    50.                 if(stack[s]==')' || stack[s]=='(')
    51.                     s--;
    52.                 pfix[p++]=stack[s--];
    53.             }
    54.         }
    55.         pfix[p]='\0';
    56.     }
    57.     printf("The postfix expression is : %s",pfix);
    58.     return 0;
    59. }

    πŸ‘‰ExecuteπŸ‘ˆ

    //OUTPUT
    /*

    Enter your infix expression(using operators +, -, %, /, *) :( A+B)*C(A-B)
    The postfix expression is : AB+CAB-*

    */






                                                                               //ThE ProFessoR

    Comments