Guest User

Untitled

a guest
Jul 8th, 2020
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct Node{
  5. int info;
  6. struct Node *next;
  7. };
  8.  
  9. typedef struct Node *nodeptr;
  10.  
  11. void display(nodeptr *Stack)
  12. {
  13. nodeptr q = *Stack;
  14. while(q != 0)
  15. {
  16. printf("%d (%c)\n",q->info,q->info);
  17. q = q->next;
  18. }
  19. }
  20.  
  21. void push(nodeptr *s, char x)
  22. {
  23. nodeptr q = (nodeptr)malloc(sizeof(struct Node));
  24. q -> info = x;
  25. if(*s == NULL)
  26. {
  27. q -> next = NULL;
  28. *s = q;
  29. }
  30. else
  31. {
  32. q -> next = *s;
  33. *s = q;
  34. }
  35. }
  36.  
  37. char pop(nodeptr *s)
  38. {
  39. if(*s == NULL)
  40. return;
  41. else{
  42. char val = (*s)->info;
  43. *s = (*s) -> next;
  44. return val;
  45. }
  46. }
  47.  
  48. int is_digit(char c)
  49. {
  50. if(c >= '0' && c <= '9')
  51. return 1;
  52. else
  53. return 0;
  54. }
  55.  
  56. int prec(char c)
  57. {
  58. switch(c){
  59. case '+':
  60. case '-':return 1;
  61. case '*':return 2;
  62. case '/':return 3;
  63. case '^':return 4;
  64. case '(':
  65. case ')':return 0;
  66. }
  67. }
  68.  
  69. char* in_to_pos(char* in)
  70. {
  71. char *out, c;
  72. int i,p;
  73. nodeptr Stack = 0;
  74.  
  75. out = (char*)malloc(sizeof(char) * 128);
  76.  
  77. i = 0;
  78. p = 0;
  79. while((c = in[i++])!='\0')
  80. {
  81. if(is_digit(c))
  82. {
  83. out[p++] = c;
  84. }
  85. else
  86. {
  87. if(c != '(' && c != ')')
  88. {
  89. out[p++] = ' ';
  90.  
  91. if(Stack == 0 || prec(c) > prec(Stack -> info))
  92. {
  93. push(&Stack,c);
  94. }
  95. else
  96. {
  97. out[p++] = ' ';
  98. out[p++] = pop(&Stack);
  99. push(&Stack,c);
  100. }
  101. }
  102. else
  103. {
  104. if(c == '(')
  105. push(&Stack, c);
  106. else if(c == ')')
  107. {
  108. while((c = pop(&Stack))!='('){
  109. out[p++] = ' ';
  110. out[p++] = c;
  111. }
  112. }
  113. }
  114.  
  115. }
  116.  
  117. }
  118. while(Stack != 0)
  119. {
  120. out[p++] = ' ';
  121. out[p++] = pop(&Stack);
  122. }
  123. out[p] = '\0';
  124.  
  125. return out;
  126.  
  127. }
  128.  
  129. int eval(int a,int b,char op)
  130. {
  131. int c = 1;
  132. switch(op){
  133. case '+': return a+b;
  134. case '-': return a-b;
  135. case '*': return a*b;
  136. case '/': return a/b;
  137. case '^': while(b>0)
  138. {
  139. c *= a;
  140. --b;
  141. }
  142. return c;
  143. }
  144. }
  145.  
  146. int pos_eval(char* in)
  147. {
  148. int i, n, result;
  149. char c;
  150. nodeptr Stack = 0;
  151.  
  152. i = 0;
  153. n = 0;
  154. while((c = in[i++])!='\0')
  155. {
  156. if(is_digit(c))
  157. {
  158. while(c!=' '){
  159. n = n*10 + (c - '0');
  160. c = in[i++];
  161. }
  162. push(&Stack, n);
  163. n = 0;
  164. ++c;
  165. }
  166. else if(c == ' ')
  167. continue;
  168. else
  169. {
  170. result = eval(pop(&Stack),pop(&Stack),c);
  171. push(&Stack, result);
  172. }
  173. }
  174. return pop(&Stack);
  175. }
  176.  
  177. int main()
  178. {
  179. char *in, *pos;
  180. int res;
  181.  
  182. in = (char*)malloc(sizeof(char) * 128);
  183.  
  184. printf("Enter Infix Expression:\n");
  185. scanf("%s",in);
  186.  
  187. pos = in_to_pos(in);
  188.  
  189. printf("POSTFIX = %s\n", pos);
  190.  
  191. res = pos_eval(pos);
  192.  
  193. printf("RESULT = %d\n",res);
  194.  
  195. }
Add Comment
Please, Sign In to add comment