Advertisement
LeRou

Untitled

Mar 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <math.h>
  6.  
  7. #define TYPE int
  8. #define SIZE 1000
  9.  
  10. typedef struct{
  11. int top;
  12. TYPE stuff[SIZE];
  13. }Stack;
  14.  
  15. void error()
  16. {
  17. printf("Something is just not right. Try Again!\n");
  18. exit(-1);
  19. }
  20.  
  21. int isfull(Stack *s)
  22. {
  23. return s->top<SIZE?0:1 ;
  24. }
  25.  
  26. void initialize(Stack *s)
  27. {
  28. int i=0;
  29. s->top=0;
  30. for(i=0;i<SIZE;i++)
  31. s->stuff[i]=0;
  32. }
  33.  
  34. int isEmpty(Stack *s)
  35. {
  36. return s->top==0?1:0 ;
  37. }
  38.  
  39. int top(Stack *s)
  40. {
  41. if(!isEmpty(s))
  42. return s->stuff[s->top-1];
  43. }
  44.  
  45. void push(Stack *s,TYPE value)
  46. {
  47. if(!isfull(s))
  48. {
  49. s->stuff[s->top]=value;
  50. s->top++;
  51. }
  52. else error();
  53.  
  54. }
  55.  
  56. int pop(Stack *s)
  57. {
  58. if(isEmpty(s))
  59. {
  60. error();
  61. }
  62. else
  63. {
  64. s->top--;
  65. return s->stuff[s->top];
  66. }
  67. return -1;
  68. }
  69.  
  70. int Precedence(char c)
  71. {
  72. switch (c)
  73. {
  74. case '+':
  75. return 1;
  76. case '-':
  77. return 1;
  78.  
  79. case '*':
  80. return 2;
  81. case '/':
  82. return 2;
  83.  
  84. case '^':
  85. return 3;
  86. }
  87. return -1;
  88. }
  89.  
  90. int evaluatePostfix(char postfix[],Stack *s)
  91. {
  92. printf("Postfix= %s.\n",postfix);
  93. int i=0,flag=0,temp=0, calc;
  94. char ch;
  95. while((ch=postfix[i++])!= '\0')
  96. {
  97. if (ch==' ')
  98. {
  99. flag=0;
  100. continue;
  101. }
  102. if (isdigit(ch))
  103. {
  104. temp=ch-48;
  105. if(flag==1)
  106. {
  107. temp+=pop(s)*10;
  108. }
  109. push(s,temp);
  110. flag=1;
  111. }
  112. else if(ch=='+' || ch=='*' || ch=='-' || ch=='/'|| ch=='^')
  113. {
  114. flag=0;
  115. int val1,val2;
  116. if (isEmpty(s))
  117. error();
  118. else
  119. val1=pop(s);
  120. if (isEmpty(s))
  121. error();
  122. else
  123. val2=pop(s);
  124. switch (ch)
  125. {
  126. case '+':
  127. calc= val2+val1;
  128. push(s,calc);
  129. break;
  130. case '*':
  131. calc= val2*val1;
  132. push(s,calc);
  133. break;
  134. case '-':
  135. calc= val2-val1;
  136. push(s,calc);
  137. break;
  138. case '/':
  139. calc= val2/val1;
  140. push(s,calc);
  141. break;
  142. case '^':
  143. calc= pow(val2,val1);
  144. push(s,calc);
  145. break;
  146. }
  147. }
  148. }
  149. return pop(s);
  150. }
  151.  
  152. char nextchar(char* pt)
  153. {
  154. int i=0;
  155. while(pt[i])
  156. {
  157. if(pt[i]==' ')
  158. continue;
  159. else
  160. return pt[i];
  161. }
  162. return '\0';
  163. }
  164.  
  165. void infixTopostfix(char infix[], char *postfix[])
  166. {
  167. printf("Infix=%s.\n",infix);
  168. int i=0,j=0;
  169. Stack s1;
  170. initialize(&s1);
  171. char swap,ch,ch1,*pt=infix,*temp=malloc(sizeof(infix));
  172. while(ch=pt[j++])
  173. {
  174. if (ch>='0' && ch<='9')
  175. {
  176. temp[i]=ch;
  177. i++;
  178. }
  179. if (ch==' ')
  180. {
  181. temp[i]=ch;
  182. i++;
  183. }
  184. else
  185. switch(ch)
  186. {
  187. case '(':
  188. case '^':
  189. case '+':
  190. case '-':
  191. case '*':
  192. case '/':
  193. if(Precedence(ch) < Precedence(top(&s1)) && nextchar(pt)!='(')
  194. {
  195. swap=pop(&s1);
  196. push(&s1,ch);
  197. push(&s1,swap);
  198. }
  199. else
  200. push(&s1,ch);
  201. break;
  202. case ')':
  203. while(!isEmpty(&s1))
  204. {
  205. ch1=pop(&s1);
  206. if (ch1=='(')
  207. break;
  208. temp[i]=ch1;
  209. i++;
  210. }
  211. break;
  212. }
  213. }
  214. while(!isEmpty(&s1))
  215. {
  216. ch1=pop(&s1);
  217. if (ch1=='(')
  218. break;
  219. temp[i]=ch1;
  220. i++;
  221. }
  222. temp[i]='\0';
  223. *postfix=temp;
  224. }
  225.  
  226. int main()
  227. {
  228. Stack s;
  229. char *infix=malloc(sizeof(char)*50);
  230. char *postfix=malloc(sizeof(char)*50);
  231. initialize(&s);
  232. printf("Please enter an infix expression:\n");
  233. fflush(stdin);
  234. fgets(infix,50,stdin);
  235. infix[strlen(infix)-1]='\0';
  236. infixTopostfix(infix,&postfix);
  237. printf("Result = %d",evaluatePostfix(postfix,&s));
  238. return 0;
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement