Advertisement
Zennoma

Dz2.3

May 18th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 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. int n = 0;
  9. int step = 0;
  10. struct Btree
  11. {
  12. char data[100];
  13. Btree* left;
  14. Btree* right;
  15. };
  16.  
  17. int priority(char c) //функция определяет приоритет операции или возвращает -1 если это число
  18. {
  19. switch (c)
  20. {
  21. case'+':
  22. case'-':
  23. return 1;
  24.  
  25. case'*':
  26. case'/':
  27. return 2;
  28. }
  29. return 100;
  30. }
  31.  
  32. void PrintTree(Btree* tree, int space)
  33. {
  34. const int COUNT = 5;
  35. if (tree != NULL)
  36. {
  37. space += COUNT;
  38. PrintTree(tree->right, space);
  39. for (int i = COUNT; i < space; i++)
  40. printf("%c", ' ');
  41. printf_s("%s\n", tree->data);
  42. PrintTree(tree->left, space);
  43. }
  44. }
  45. int its_a_number(Btree* tree)
  46. {
  47. int i = 0;
  48. if (!tree) return 0;
  49. while (tree->data[i])
  50. if (!strchr("-0123456789", tree->data[i++]))
  51. return 0;
  52. return 1;
  53. }
  54. void calculate(Btree* tree)
  55. {
  56. while (tree->left->left != NULL && tree->left != NULL)
  57. calculate(tree->left);
  58. while (tree->right->right != NULL && tree->right != NULL)
  59. calculate(tree->right);
  60. int a, b, c = 0;
  61. if (!tree || !its_a_number(tree->left) || !its_a_number(tree->right)) return;
  62. a = atoi(tree->left->data);
  63. b = atoi(tree->right->data);
  64. switch (tree->data[0])
  65. {
  66. case'+':c = a + b; break;
  67. case'-':c = a - b; break;
  68. case'*':c = a * b; break;
  69. case'/':c = a / b; break;
  70. }
  71. free(tree->left);
  72. free(tree->right);
  73. sprintf(tree->data, "%d", c);
  74. step++;
  75. cout << "result for step " << step <<" is " << c << endl;
  76. tree->left = NULL;
  77. tree->right = NULL;
  78. }
  79. void inOrderTravers(Btree* root)
  80. {
  81. if (root) {
  82. inOrderTravers(root->left);
  83. printf("%s ", root->data);
  84. inOrderTravers(root->right);
  85. }
  86. }
  87. char** transform(char* string)
  88. {
  89. int j;
  90. int t = 0;
  91. int count = 0;
  92. int f = 0;
  93. while (string[f] != '\0') {
  94. if (!(string[f] > 47 && string[f] < 58 &&
  95. string[f - 1] > 47 && string[f - 1] < 58))
  96. n++;
  97. f++;
  98. }
  99. char** newstring = (char**)malloc(n * sizeof(char*));
  100. for (int i = 0; i < n; i++)
  101. newstring[i] = (char*)malloc(5 * sizeof(char*));
  102. while (count < strlen(string))
  103. {
  104. j = 0;
  105. if (isdigit(string[count]) > 0)
  106. {
  107. while (isdigit(string[count]) > 0)
  108. {
  109. newstring[t][j] = string[count];
  110. count++;
  111. j++;
  112. newstring[t][j] = '\0';
  113. }
  114. count--;
  115. }
  116. else
  117. {
  118. newstring[t][j] = string[count];
  119. newstring[t][1] = '\0';
  120. }
  121. t++;
  122. count++;
  123. }
  124. return(newstring);
  125. }
  126. Btree* let_it_tree(char** term, int first, int last)
  127. {
  128.  
  129. Btree* tree = (Btree*)malloc(sizeof(Btree));
  130. char c;
  131. int min, i, k, prt;
  132. int nest = 0;
  133. if (first == last)
  134. {
  135. strcpy(tree->data, term[first]);
  136. tree->left = NULL;
  137. tree->right = NULL;
  138. return tree;
  139. }
  140. min = 100;
  141. for (i = first; i <= last; i++)
  142. {
  143. c = term[i][0];
  144. if (c == '(') { nest++; continue; }
  145. if (c == ')') { nest--; continue; }
  146. if (nest > 0) continue;
  147. prt = priority(c);
  148. if (prt <= min)
  149. {
  150. min = prt;
  151. k = i;
  152. }
  153. }
  154. if (min == 100 && term[first][0] == '(' && term[last][0]== ')' )
  155. return let_it_tree(term, first + 1, last - 1);
  156. else
  157. {
  158. strcpy(tree->data, term[k]);
  159. tree->left = let_it_tree(term, first, k - 1);
  160. tree->right = let_it_tree(term, k + 1, last);
  161. return tree;
  162. }
  163. }
  164.  
  165. void Tree2(Btree* r)//обратный
  166. {
  167. int n;
  168. if (r != NULL)
  169. {
  170. Tree2(r->left);
  171. if (isalpha(r->data[0])>0)
  172. {
  173. cout << "Input value for " << r->data[0] << endl;
  174. cin >> n;
  175. sprintf(r->data, "%d", n);
  176. }
  177. Tree2(r->right);
  178. }
  179. }
  180. int main()
  181. {
  182.  
  183. char* input = (char*)malloc(30 * sizeof(char));
  184. char Key;
  185. while (1)
  186. {
  187. printf("\n Enter the number - the type of functiom:"
  188. "\n 1 - TASK FUNCTION ->(f+1)*(6/(1+g)+4)"
  189. "\n 2 - ANY INPUTTED FUNCTION"
  190. "\n 3 - EXIT\n");
  191. fflush(stdin);
  192. scanf("%c", &Key);
  193. switch (Key)
  194. {
  195. case '1':
  196. {
  197. Btree* tree = (Btree*)malloc(sizeof(Btree));
  198. strcpy(input, "(f+1)*(6/(1+g)+4)");
  199. char** term = transform(input);
  200. tree = let_it_tree(term, 0, n - 1);
  201. PrintTree(tree, 10);
  202. inOrderTravers(tree);
  203. cout << " infix show" << endl;
  204. Tree2(tree);
  205. PrintTree(tree, 10);
  206. cout << "result is " << endl;
  207. calculate(tree);
  208. free(tree);
  209. n = 0;
  210. step = 0;
  211. system("pause");
  212. break;
  213. }
  214. case '2':
  215. { Btree* tree = (Btree*)malloc(sizeof(Btree));
  216. cout << "Input your fucntion" << endl;
  217. cin >> input;
  218. char** term = transform(input);
  219. tree = let_it_tree(term, 0, n - 1);
  220. PrintTree(tree, 10);
  221. inOrderTravers(tree);
  222. cout << " infix show" << endl;
  223. Tree2(tree);
  224. PrintTree(tree, 10);
  225. cout << "result is " << endl;
  226. calculate(tree);
  227. free(tree);
  228. n = 0;
  229. step = 0;
  230. system("pause");
  231. break;
  232. }
  233. case '3': return 0;
  234.  
  235. default:
  236. printf("\nIncorrect input! Try again!!!");
  237. system("cls");
  238. break;
  239. }
  240. }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement