LeRou

Untitled

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