Guest User

Untitled

a guest
May 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. #include <string.h>
  6. #include <math.h>
  7.  
  8. /* ********** Stack handling routines *********/
  9.  
  10. // Push element to top of stack
  11. template<class x>
  12. int stack_push(x *stk, int &top, x el)
  13. {
  14. stk[top] = el;
  15.  
  16. return ++top;
  17. }
  18.  
  19. // Get top most element
  20.  
  21. template<class x>
  22. int stack_pop(x *stk, int &top, x &el)
  23. {
  24. if(top > 0) el = stk[--top];
  25. else return -1;
  26.  
  27. return top;
  28. }
  29.  
  30. // Get value of top most element
  31. template<class x>
  32. int stack_top(x *stk, int top, x &el)
  33. {
  34. return (top > 0) ? el = stk[top - 1], top : -1;
  35. }
  36.  
  37. // Find out priority of operation given
  38. int get_op_priority(char op)
  39. {
  40. switch(op)
  41. {
  42. case '+':
  43. case '(':
  44. case ')':
  45. return 1;
  46.  
  47. case '-':
  48. return 2;
  49.  
  50. case '*':
  51. return 3;
  52.  
  53. case '/':
  54. return 4;
  55.  
  56. case 'sin':
  57. case 'cos':
  58. case 'tg':
  59. case 'ln':
  60. case 'lg':
  61. return 99;
  62.  
  63. default:
  64. return 0;
  65. }
  66. }
  67.  
  68. char atoop(char *str)
  69. {
  70. static char *table[] = {"sin", "cos", "tg", "ln", "lg", NULL};
  71. int i = 0;
  72.  
  73. while(table[i] != NULL)
  74. {
  75. if(stricmp(str, table[i]) == 0) return i + 1;
  76.  
  77. i++;
  78. }
  79.  
  80. return 0;
  81. }
  82.  
  83. // Perform calculation
  84. float calculate(float a, float b, char op)
  85. {
  86. switch(op)
  87. {
  88. case '+':
  89. return a + b;
  90.  
  91. case '-':
  92. return a - b;
  93.  
  94. case '*':
  95. return a * b;
  96.  
  97. case '/':
  98. return a / b;
  99.  
  100. case '1':
  101. return -1;
  102.  
  103. case 2: // cos
  104. return -2;
  105.  
  106. case 3: // tg
  107. return -3;
  108.  
  109. case 4: // ln
  110. return -4;
  111.  
  112. case 5: // lg
  113. return -5;
  114.  
  115. default:
  116. return -1;
  117. }
  118. }
  119.  
  120. // Main function
  121. int main(void)
  122. {
  123. // Opearands stack
  124. long operands[128] = {0};
  125. int opr_top = 0;
  126. // Opeartions stack
  127. char operations[128] = {0};
  128. int ops_top = 0;
  129. char expr[1024] = {0};
  130. int i = 0;
  131. long a = 0, b = 0, c = 0;
  132.  
  133. // "345+765" = "345" + "+" + "765"
  134. // "34*(32+78)" "34*sin(32+78)"
  135. printf("Enter expression to calculate\n");
  136. gets(expr);
  137.  
  138. stack_push<char>(operations, ops_top, '(');
  139. a = strlen(expr);
  140. expr[a] = ')';
  141. expr[a + 1] = '\0';
  142.  
  143. while(expr[i] != '\0')
  144. {
  145. char tmpexpr[128];
  146. int n = 0;
  147.  
  148. // If current character is numeric put it to buffer
  149. while(isdigit(expr[i])) tmpexpr[n++] = expr[i++];
  150.  
  151. if(n > 0)
  152. {
  153. tmpexpr[n] = '\0';
  154. stack_push<long>(operands, opr_top, atoi(tmpexpr));
  155. n = 0;
  156. }
  157. else
  158. {
  159. char op = 0, op_next = 0, tmpop;
  160.  
  161. while(isalpha(expr[i])) tmpexpr[n++] = expr[i++];
  162.  
  163. if(n > 0)
  164. {
  165. tmpop = atoop(tmpexpr);
  166.  
  167. if(tmpop == 0) tmpop = expr[i++];
  168. }
  169. // else if(((expr[i] != '(') && (expr[i] != ')')) &&((i > 0) && (!isdigit(expr[i - 1])) || (i < 1)))
  170. // {
  171. // stack_push<long>(operands, opr_top, 0);
  172. // i++;
  173. // }
  174. else tmpop = expr[i++];
  175.  
  176. switch(tmpop)
  177. {
  178. case '1':
  179. case '2':
  180. case '3':
  181. case '4':
  182. case '5':
  183. stack_push<long>(operands, opr_top, 0);
  184. case '+':
  185. case '-':
  186. case '*':
  187. case '/':
  188. case '(':
  189. stack_push<char>(operations, ops_top, tmpop);
  190. break;
  191.  
  192. case ')':
  193. while(stack_pop<char>(operations, ops_top, op) > 0)
  194. {
  195. if(op == '(') break;
  196.  
  197. n = stack_top<char>(operations, ops_top, op_next);
  198.  
  199. if((n < 1) || (get_op_priority(op) >= get_op_priority(op_next)))
  200. {
  201. stack_pop<long>(operands, opr_top, b);
  202. stack_pop<long>(operands, opr_top, a);
  203. stack_push<long>(operands, opr_top, calculate(a, b, op));
  204. }
  205. else
  206. {
  207. stack_pop<char>(operations, ops_top, op_next);
  208. c = b;
  209. b = a;
  210. stack_pop<long>(operands, opr_top, a);
  211. stack_push<long>(operands, opr_top, calculate(a,b, op_next));
  212. stack_push<char>(operations, ops_top, op);
  213.  
  214. }
  215. }
  216. break;
  217.  
  218. default:
  219. printf("Operation is not supported or incorrect symbol \"%c\" at %d\n",
  220. expr[i], i);
  221. getch();
  222.  
  223. return 0;
  224. }
  225. }
  226. }
  227.  
  228. stack_pop<long>(operands, opr_top, a);
  229. printf("result: %d", a);
  230. getch();
  231.  
  232. return 0;
  233. }
Add Comment
Please, Sign In to add comment