Advertisement
Zennoma

Dz2.3

May 13th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. /*построить дерево бинарного разбора выражения (f+1)*(6/(1+g)+4) */
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include <iostream>
  4. #include <string.h>
  5. #include <time.h>
  6. #include <stdlib.h>
  7. using namespace std;
  8.  
  9. struct Btree
  10. {
  11. char data[10];
  12. Btree* left;
  13. Btree* right;
  14. };
  15.  
  16. int priority(char c) //функция определяет приоритет операции или возвращает -1 если это число
  17. {
  18. switch (c)
  19. {
  20. case'+':
  21. case'-':
  22. return 1;
  23.  
  24. case'*':
  25. case'/':
  26. return 2;
  27. }
  28. return 100;
  29. }
  30.  
  31. void PrintTree(Btree* tree, int space)
  32. {
  33. const int COUNT = 5;
  34. if (tree != NULL)
  35. {
  36. space += COUNT;
  37. PrintTree(tree->right, space);
  38. for (int i = COUNT; i < space; i++)
  39. printf("%c", ' ');
  40. printf_s("%s\n", tree->data);
  41. PrintTree(tree->left, space);
  42. }
  43. }
  44. int its_a_number(Btree* tree)
  45. {
  46. int i = 0;
  47. if (!tree) return 0;
  48. while (tree->data[i])
  49. if (!strchr("-0123456789", tree->data[i++]))
  50. return 0;
  51. return 1;
  52. }
  53. void calculate(Btree* tree)
  54. {
  55. while (tree->left->left != NULL && tree->left != NULL)
  56. calculate(tree->left);
  57. while (tree->right->right != NULL && tree->right != NULL)
  58. calculate(tree->right);
  59. int a, b, c = 0;
  60. if (!tree || !its_a_number(tree->left) || !its_a_number(tree->right)) return;
  61. a = atoi(tree->left->data);
  62. b = atoi(tree->right->data);
  63. switch (tree->data[0])
  64. {
  65. case'+':c = a + b; break;
  66. case'-':c = a - b; break;
  67. case'*':c = a * b; break;
  68. case'/':c = a / b; break;
  69. }
  70. free(tree->left);
  71. free(tree->right);
  72. sprintf(tree->data, "%d", c);
  73. cout << c << endl;
  74. tree->left = NULL;
  75. tree->right = NULL;
  76. }
  77. Btree* let_it_tree(char term[], int first, int last)
  78. {
  79. Btree* tree = (Btree*)malloc(sizeof(Btree));
  80. char c;
  81. int min, i, k, prt;
  82. int nest = 0;
  83. if (first == last || first == (last - 1))
  84. {
  85. tree->data[0] = term[first];
  86. tree->data[1] = '\0';
  87. tree->left = NULL;
  88. tree->right = NULL;
  89. return tree;
  90. }
  91. min = 100;
  92. for (i = first; i < last; i++)
  93. {
  94. c = term[i];
  95. if (c == '(') { nest++; continue; }
  96. if (c == ')') { nest--; continue; }
  97. if (nest > 0) continue;
  98. prt = priority(c);
  99. if (prt <= min)
  100. {
  101. min = prt;
  102. k = i;
  103. }
  104. }
  105. if (min == 100 && term[first] == '(' && (term[last] == ')' || term[last] == '\0'))
  106. return let_it_tree(term, first + 1, last - 1);
  107. else
  108. {
  109. tree->data[0] = term[k];
  110. tree->data[1] = '\0';
  111. tree->left = let_it_tree(term, first, k - 1);
  112. tree->right = let_it_tree(term, k + 1, last);
  113. return tree;
  114. }
  115. }
  116. void genrand(int* x)
  117. {
  118.  
  119. for (int i = 0; i < 10; i++)
  120. {
  121. x[i] = rand() % 15;
  122. cout.width(5);
  123. cout<< x[i];
  124. }
  125. cout<< endl;
  126. }
  127. void Tree2(Btree* r, int n, int m)//обратный
  128. {
  129. if (r != NULL)
  130. {
  131. Tree2(r->left, n, m);
  132. if (r->data[0] == 'a')
  133. {
  134. sprintf(r->data, "%d", n);
  135. }
  136. if (r->data[0] == 'b')
  137. {
  138. sprintf(r->data, "%d", m);
  139. }
  140. Tree2(r->right, n, m);
  141. }
  142. }
  143. int main()
  144. {
  145. srand(time(NULL));
  146. int* n = (int*)malloc(10 * sizeof(int));
  147. int* m = (int*)malloc(10 * sizeof(int));
  148. Btree* tree = (Btree*)malloc(sizeof(Btree));
  149. char* term = (char*)malloc(30 * sizeof(char));
  150. cout << "Input your fucntion" << endl;
  151. cin>> term;
  152. tree = let_it_tree(term, 0, strlen(term));
  153. PrintTree(tree, 10);
  154. cout<< "n is ";
  155. genrand(n);
  156. cout<< "m is ";
  157. n[0] = 7;
  158. genrand(m);
  159. Tree2(tree, n[0], m[0]);
  160. PrintTree(tree, 10);
  161. cout<< "result is "<< endl;
  162. calculate(tree);
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement