Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct Tree {
  6. int val;
  7. int count;
  8. struct Tree *left;
  9. struct Tree *right;
  10. } tree;
  11.  
  12. int kategorije[8];
  13. int mojaKategorija;
  14. tree* pibs = NULL;
  15. FILE* f_out;
  16.  
  17. tree* insert(tree* head, int val) {
  18. tree* new = (tree*) malloc(sizeof(tree));
  19. new->val = val;
  20. new->left = NULL;
  21. new->right = NULL;
  22. new->count = 0;
  23.  
  24. if (!head) {
  25. return new;
  26. }
  27.  
  28. tree* t = head;
  29. tree* pt = NULL;
  30. while (t) {
  31. pt = t;
  32. if (val < t->val) {
  33. t = t->left;
  34. } else {
  35. t = t->right;
  36. }
  37. }
  38.  
  39. if (val < pt->val) {
  40. pt->left = new;
  41. } else {
  42. pt->right = new;
  43. }
  44.  
  45. return head;
  46. }
  47.  
  48. tree* find(tree* head, int val) {
  49. tree* t = head;
  50. while (t) {
  51. if (t->val == val) {
  52. return t;
  53. }
  54. if (val < t->val) {
  55. t = t->left;
  56. } else {
  57. t = t->right;
  58. }
  59. }
  60.  
  61. return t;
  62. }
  63.  
  64. int tree_max(tree* head, int max) {
  65. if (!head) return 0;
  66. max = head->count;
  67. int a = tree_max(head->left, max);
  68. int b = tree_max(head->right, max);
  69. if (a > max) max = a;
  70. if (b > max) max = b;
  71. return max;
  72. }
  73.  
  74. void print_maxes(tree* head, int max) {
  75. if (!head) return;
  76. if (head->count == max) {
  77. fprintf(f_out, "%d\n", head->val);
  78. }
  79. print_maxes(head->left, max);
  80. print_maxes(head->right, max);
  81. }
  82.  
  83. int brojKategorije(int iznos) {
  84. if (iznos < kategorije[0]) return -1;
  85. for (int i = 0; i < 8; i++) {
  86. if (iznos >= kategorije[i]) {
  87. if (i == 7) return 8;
  88. if (iznos < kategorije[i + 1]) return i + 1;
  89. }
  90. }
  91. return -1;
  92. }
  93.  
  94. // ./a <broj_kategorije>
  95. int main(int argc, char** argv) {
  96. mojaKategorija = argv[1][0] - '0';
  97. mojaKategorija++;
  98. char f_out_name[20] = "kat";
  99. f_out_name[3] = mojaKategorija + '0';
  100. f_out_name[4] = '\0';
  101. f_out = fopen(strcat(f_out_name, ".txt"), "w");
  102.  
  103. FILE* kat_f = fopen("kategorizacija.txt", "r");
  104. for (int i = 0; i < 8; i++) {
  105. fscanf(kat_f, "%d", &kategorije[i]);
  106. }
  107. fclose(kat_f);
  108.  
  109. char f_name[20] = "kvartal";
  110. int pibu, pibp, iznos;
  111. FILE* f_kvart;
  112. tree* t;
  113. for (int i = 1; i <= 4; i++) {
  114. f_name[7] = i + '0';
  115. f_name[8] = '\0';
  116. f_kvart = fopen(strcat(f_name, ".txt"), "r");
  117.  
  118. while (1) {
  119. fscanf(f_kvart, "%d", &pibu);
  120. if (feof(f_kvart)) break;
  121. fscanf(f_kvart, "%d%d", &pibp, &iznos);
  122.  
  123. if (brojKategorije(iznos) == mojaKategorija) {
  124. if (!find(pibs, pibu)) {
  125. pibs = insert(pibs, pibu);
  126. }
  127. if (!find(pibs, pibp)) {
  128. pibs = insert(pibs, pibp);
  129. }
  130. t = find(pibs, pibu);
  131. t->count++;
  132. t = find(pibs, pibp);
  133. t->count++;
  134. }
  135. }
  136.  
  137.  
  138. fclose(f_kvart);
  139. }
  140.  
  141. int max = tree_max(pibs, 0);
  142. print_maxes(pibs, max);
  143.  
  144. fclose(f_out);
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement