Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #define BUF_SIZE 1024
  6.  
  7. // <цифра> ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
  8. // <число> ::= <цифра> { <цифра> } [ '.' <цифра> { <цифра> } ]
  9. //
  10. // <выражение> ::= <слагаемое> [ ( '+' | '-' ) <слагаемое> ]
  11. // <слагаемое> ::= <множитель> [ ( '*' | '/' ) <множитель> ]
  12. // <множитель> ::= ( <число> | '(' <выражение> ')' ) [ '^' <множитель> ]
  13.  
  14. double eval(char *str);
  15. double number(char *, unsigned *);
  16. double expr(char *, unsigned *);
  17. double term(char *, unsigned *);
  18. double factor(char *, unsigned *);
  19.  
  20. int main()
  21. {
  22. char str[BUF_SIZE];
  23.  
  24. printf("Enter expression: ");
  25. fgets(str, BUF_SIZE, stdin);
  26.  
  27. printf("Result: %lf\n", eval(str));
  28.  
  29. return 0;
  30. }
  31.  
  32. double eval(char *str)
  33. {
  34. unsigned i = 0;
  35.  
  36. return expr(str, &i);
  37. }
  38.  
  39. double number(char *str, unsigned *idx)
  40. {
  41. double result = 0.0;
  42. double div = 10.0;
  43. int sign = 1;
  44.  
  45. if (str[*idx] == '-')
  46. {
  47. sign = -1;
  48. ++*idx;
  49. }
  50.  
  51. while (str[*idx] >= '0' && str[*idx] <= '9')
  52. {
  53. result = result * 10.0 + (str[*idx] - '0');
  54.  
  55. ++*idx;
  56. }
  57.  
  58. if (str[*idx] == '.')
  59. {
  60. ++*idx;
  61.  
  62. while (str[*idx] >= '0' && str[*idx] <= '9')
  63. {
  64. result = result + (str[*idx] - '0') / div;
  65. div *= 10.0;
  66.  
  67. ++*idx;
  68. }
  69. }
  70.  
  71. return sign * result;
  72. }
  73.  
  74. double expr(char *str, unsigned *idx)
  75. {
  76. double result = term(str, idx);
  77.  
  78. while (str[*idx] == '+' || str[*idx] == '-')
  79. {
  80. switch (str[*idx])
  81. {
  82. case '+':
  83. ++*idx;
  84.  
  85. result += term(str, idx);
  86.  
  87. break;
  88. case '-':
  89. ++*idx;
  90.  
  91. result -= term(str, idx);
  92.  
  93. break;
  94. }
  95. }
  96.  
  97. return result;
  98. }
  99.  
  100. double term(char *str, unsigned *idx)
  101. {
  102. double result = factor(str, idx);
  103. double div;
  104.  
  105. while (str[*idx] == '*' || str[*idx] == '/')
  106. {
  107. switch (str[*idx])
  108. {
  109. case '*':
  110. ++*idx;
  111.  
  112. result *= factor(str, idx);
  113.  
  114. break;
  115. case '/':
  116. ++*idx;
  117.  
  118. div = factor(str, idx);
  119.  
  120. if (div != 0.0)
  121. {
  122. result /= div;
  123. }
  124. else
  125. {
  126. printf("Division by zero!\n");
  127. exit(-1);
  128. }
  129.  
  130. break;
  131. }
  132. }
  133.  
  134. return result;
  135. }
  136.  
  137. double factor(char *str, unsigned *idx)
  138. {
  139. double result;
  140. int sign = 1;
  141.  
  142. if (str[*idx] == '-')
  143. {
  144. sign = -1;
  145.  
  146. ++*idx;
  147. }
  148.  
  149. if (str[*idx] == '(')
  150. {
  151. ++*idx;
  152.  
  153. result = expr(str, idx);
  154.  
  155. if (str[*idx] != ')')
  156. {
  157. printf("Brackets unbalanced!\n");
  158. exit(-2);
  159. }
  160.  
  161. ++*idx;
  162. }
  163. else
  164. {
  165. if (str[*idx]== ')')
  166. {
  167. printf("Bracket error");
  168. exit(-3);
  169. }
  170. result = number(str, idx);
  171. }
  172. return sign * result;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement