Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<stdlib.h>
  5.  
  6. #define BLANK ' '
  7. #define TAB '\t'
  8. #define MAX 50
  9.  
  10. char *pop();
  11. char prefix[MAX];
  12. char stack[MAX][MAX];
  13. void push(char *str);
  14. int isempty();
  15. int white_space(char symbol);
  16. void prefix_to_postfix();
  17. int top;
  18.  
  19. int main()
  20. {
  21. top = -1;
  22. printf("Enter Prefix Expression : ");
  23. gets(prefix);
  24. printf("%s\n", prefix);
  25. prefix_to_postfix();
  26.  
  27. }/*End of main()*/
  28.  
  29. void prefix_to_postfix()
  30. {
  31. int i;
  32. char operand1[MAX], operand2[MAX];
  33. char symbol;
  34. char temp[2];
  35. char strin[MAX];
  36. for(i=strlen(prefix)-1;i>=0;i--)
  37. {
  38. symbol=prefix[i];
  39. temp[0]=symbol;
  40. temp[1]='\0';
  41.  
  42. if(!white_space(symbol))
  43. {
  44. switch(symbol)
  45. {
  46. case '+':
  47. case '-':
  48. case '*':
  49. case '/':
  50. case '%':
  51. case '^':
  52. strcpy(operand1,pop());
  53. strcpy(operand2,pop());
  54. strcpy(strin,operand1);
  55. strcat(strin,operand2);
  56. strcat(strin,temp);
  57. push(strin);
  58. break;
  59. default: /*if an operand comes*/
  60. push(temp);
  61. }
  62. }
  63. }
  64. printf("\nPostfix Expression :: ");
  65. puts(stack[0]);
  66. }/*End of prefix_to_postfix()*/
  67.  
  68.  
  69. void push(char *str)
  70. {
  71. if(top > MAX)
  72. {
  73. printf("\nStack overflow\n");
  74. exit(1);
  75. }
  76. else
  77. {
  78. top=top+1;
  79. strcpy( stack[top], str);
  80. }
  81. }/*End of push()*/
  82.  
  83. char *pop()
  84. {
  85. if(top == -1 )
  86. {
  87. printf("\nStack underflow \n");
  88. exit(2);
  89. }
  90. else
  91. return (stack[top--]);
  92. }/*End of pop()*/
  93. int isempty()
  94. {
  95. if(top==-1)
  96. return 1;
  97. else
  98. return 0;
  99. }
  100. int white_space(char symbol)
  101. {
  102. if(symbol==BLANK || symbol==TAB || symbol=='\0')
  103. return 1;
  104. else
  105. return 0;
  106. }/*End of white_space()*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement