Advertisement
Aniket_Goku

infix -> postfix

Aug 27th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. //program to infix to post fix
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #include<string.h>
  5. char infix[50],postfix[50],stack[50];
  6. int top=-1;
  7. void push(char ch)
  8. {
  9. top++;
  10. stack[top]=ch;
  11.  
  12. }
  13. char pop()
  14. {
  15. char ch;
  16. ch=stack[top];
  17. top--;
  18. return ch;
  19. }
  20. int main()
  21. {
  22.  
  23. int i,j=0;
  24. clrscr();
  25. printf("\nENter the Infix EXPRESSION: ");
  26. gets(infix);
  27. for(i=0;i<strlen(infix);i++)
  28. {
  29. switch(infix[i])
  30. {
  31. case '[':
  32. push('[');
  33. break;
  34.  
  35. case ']':
  36. while(stack[top]!='[')
  37. {
  38. postfix[j]=pop();
  39. j++;
  40.  
  41. }
  42. top--;
  43. break;
  44. case '(':
  45. push('(');
  46. break;
  47.  
  48. case ')':
  49. while(stack[top]!='(')
  50. {
  51. postfix[j]=pop();
  52. j++;
  53.  
  54. }
  55. top--;
  56. break;
  57.  
  58. case '^':
  59. while(stack[top]=='^')
  60. {
  61. postfix[j]=pop();
  62. j++;
  63. }
  64. push(infix[i]);
  65. break;
  66.  
  67. case '*':
  68. case '/':
  69. while(stack[top]=='^' || stack[top]=='*' || stack[top]=='/')
  70. {
  71. postfix[j]=pop();
  72. j++;
  73. }
  74. push(infix[i]);
  75. break;
  76.  
  77. case '+':
  78. case '-':
  79. while(stack[top]=='^' || stack[top]=='*' || stack[top]=='/'|| stack[top]=='-'|| stack[top]=='+')
  80. {
  81. postfix[j]=pop();
  82. j++;
  83. }
  84. push(infix[i]);
  85. break;
  86.  
  87. default:
  88. postfix[j]=infix[i];
  89. j++;
  90.  
  91. }
  92.  
  93. }while(top!=-1)
  94. {
  95. postfix[j]=pop();
  96. j++;
  97.  
  98. }
  99. postfix[j]=NULL;
  100. printf("\n %s <--- INFIX TO POSTFIX ---> %s",infix,postfix);
  101. getch();
  102. return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement