DS | Practical-4 | C Program to Convert Infix to Postfix Expression using Stack

 

Write a C Program to Convert Infix to Postfix Expression using Stack


GTU DS Practical-4

Implement a program to convert Infix to Postfix notation using Stack.

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<ctype.h>
  4. #include<string.h>
  5. #define SIZE 100
  6. char stack[SIZE];
  7. int top = -1;
  8. void push(char item)
  9. {
  10. if(top >= SIZE-1)
  11. {
  12. printf("\nStack Overflow.");
  13. }
  14. else
  15. {
  16. top = top+1;
  17. stack[top] = item;
  18. }
  19. }
  20. char pop()
  21. {
  22. char item ;
  23. if(top <0)
  24. {
  25. printf("stack under flow: invalid infix expression");
  26. getchar();
  27. /* underflow may occur for invalid expression */
  28. /* where ( and ) are not matched */
  29. exit(1);
  30. }
  31. else
  32. {
  33. item = stack[top];
  34. top = top-1;
  35. return(item);
  36. }
  37. }
  38. int is_operator(char symbol)
  39. {
  40. if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol =='-')
  41. {
  42. return 1;
  43. }
  44. else
  45. {
  46. return 0;
  47. }
  48. }
  49. int precedence(char symbol)
  50. {
  51. if(symbol == '^')
  52. {
  53. return(3);
  54. }
  55. else if(symbol == '*' || symbol == '/')
  56. {
  57. return(2);
  58. }
  59. else if(symbol == '+' || symbol == '-')
  60. {
  61. return(1);
  62. }
  63. else
  64. {
  65. return(0);
  66. }
  67. }
  68. void InfixToPostfix(char infix_exp[], char postfix_exp[])
  69. {
  70. int i, j;
  71. char item;
  72. char x;
  73. push('(');
  74. strcat(infix_exp,")");
  75. i=0;
  76. j=0;
  77. item=infix_exp[i];
  78. while(item != '\0')
  79. {
  80. if(item == '(')
  81. {
  82. push(item);
  83. }
  84. else if( isdigit(item) || isalpha(item))
  85. {
  86. postfix_exp[j] = item;
  87. j++;
  88. }
  89. else if(is_operator(item) == 1)
  90. {
  91. x=pop();
  92. while(is_operator(x) == 1 && precedence(x)>= precedence(item))
  93. {
  94. postfix_exp[j] = x;
  95. j++;
  96. x = pop();
  97. }
  98. push(x);
  99. push(item);
  100. }
  101. else if(item == ')')
  102. {
  103. x = pop();
  104. while(x != '(')
  105. {
  106. postfix_exp[j] = x;
  107. j++;
  108. x = pop();
  109. }
  110. }
  111. else
  112. {
  113. printf("\nInvalid infix Expression.\n");
  114. getchar();
  115. exit(1);
  116. }
  117. i++;
  118. item = infix_exp[i];
  119. }
  120. if(top>0)
  121. {
  122. printf("\nInvalid infix Expression.\n");
  123. getchar();
  124. exit(1);
  125. }
  126. if(top>0)
  127. {
  128. printf("\nInvalid infix Expression.\n");
  129. getchar();
  130. exit(1);
  131. }
  132. postfix_exp[j] = '\0';
  133. }
  134. int main()
  135. {
  136. char infix[SIZE], postfix[SIZE];
  137. printf("ASSUMPTION: The infix expression contains single letter variables and single digit constants only.\n");
  138. printf("\nEnter Infix expression : ");
  139. gets(infix);
  140. InfixToPostfix(infix,postfix);
  141. printf("Postfix Expression: ");
  142. puts(postfix);
  143. return 0;
  144. }

Output:




Go To :

Home Page :

Practical-3 :

Practical-5 :

Comments