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