fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #define SIZE 100
  6. int count=0;
  7. typedef struct node{
  8. int value;
  9. struct stack *next;
  10. struct stack *prev;
  11. }stack;
  12.  
  13. stack *bottom=NULL;
  14. stack *top=NULL;
  15.  
  16. stack *createnode(int a)
  17. {
  18. stack *newstack;
  19. newstack=(stack*)calloc(1,sizeof(stack));
  20. newstack->value=a;
  21. newstack->prev=NULL;
  22. newstack->next=NULL;
  23. return newstack;
  24. }
  25.  
  26. int isempty()
  27. {
  28. if(count==0)
  29. {return 1;}
  30. else
  31. {return 0;}
  32. }
  33.  
  34. int isfull()
  35. {
  36. if(count==SIZE)
  37. {return 1;}
  38. else
  39. {return 0;}
  40. }
  41.  
  42. void push(int a)
  43. { if(isfull()==1)
  44. {
  45. printf("overflow condition: stack full");
  46. }
  47. else
  48. {count+=1;
  49. stack *newstack = createnode(a);
  50. if(bottom==NULL)
  51. {
  52. bottom=newstack;
  53. top=bottom;
  54. }
  55. else
  56. {
  57. top->next=newstack;
  58. newstack->prev=top;
  59. top=newstack;
  60. }
  61. }
  62. }
  63. char peek()
  64. {
  65. return top->value;
  66. }
  67. char pop()
  68. { if(isempty()==1)
  69. {
  70. printf("underflow condition:empty stack");
  71. }
  72. else
  73. {if(count==1)
  74. { int c;
  75. c=top->value;
  76. top=NULL;
  77. bottom=NULL;
  78. count=count-1;
  79. return c;
  80.  
  81. }
  82. else
  83. {int c;
  84. c=top->value;
  85. top=top->prev;
  86. top->next=NULL;
  87. count=count-1;
  88. return c;
  89.  
  90. }
  91. }
  92. }
  93. char plus=43;
  94. char multi=42;
  95. char divi=47;
  96. char power=94;
  97. char left=40;
  98. char right=41;
  99. char subtract=45;
  100. void *postfix_evaluation(char *string,int length)
  101. { int i; stack operators;
  102. for(i=0;i<length;i++)
  103. {
  104. char c;
  105. c=string[i];
  106. if(c==plus || c==subtract || c==multi || c==divi || c==power)
  107. {
  108. int a =pop();
  109. int b=pop();
  110. if(c==plus)
  111. {
  112. int result=b+a;
  113. push(result);
  114. // printf("%d",result);
  115. }
  116. if(c==subtract)
  117. {
  118. int result=b-a;
  119. push(result);
  120. }
  121. if(c==multi)
  122. {
  123. int result=b*a;
  124. push(result);
  125. }
  126. if(c==divi)
  127. {
  128. int result=b/a;
  129. push(result);
  130. }
  131. if(c==power)
  132. {
  133. int result=pow(b,a);
  134. push(result);
  135. }
  136. }
  137. else
  138. { int k=c-'0';
  139. push(k);
  140. // printf("%d",k);
  141. }
  142. /* if(i==length-1)
  143.   {
  144.   int d=pop();
  145.   printf("%d",d);
  146.   }
  147. */
  148. }
  149. int d=pop();
  150. printf("%d",d);
  151. }
  152.  
  153. // driver code , the main function
  154.  
  155. int main(void)
  156. { char inversion[200];
  157. printf("give the postfix expression\n");
  158. scanf("%s",&inversion);
  159. int longer=strlen(inversion);
  160. postfix_evaluation(inversion,longer);
  161.  
  162. return 0;
  163. }
  164.  
Success #stdin #stdout 0s 9424KB
stdin
23+3^
stdout
give the postfix expression
125