Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct Tree {
- int val;
- int count;
- struct Tree *left;
- struct Tree *right;
- } tree;
- int kategorije[8];
- int mojaKategorija;
- tree* pibs = NULL;
- FILE* f_out;
- tree* insert(tree* head, int val) {
- tree* new = (tree*) malloc(sizeof(tree));
- new->val = val;
- new->left = NULL;
- new->right = NULL;
- new->count = 0;
- if (!head) {
- return new;
- }
- tree* t = head;
- tree* pt = NULL;
- while (t) {
- pt = t;
- if (val < t->val) {
- t = t->left;
- } else {
- t = t->right;
- }
- }
- if (val < pt->val) {
- pt->left = new;
- } else {
- pt->right = new;
- }
- return head;
- }
- tree* find(tree* head, int val) {
- tree* t = head;
- while (t) {
- if (t->val == val) {
- return t;
- }
- if (val < t->val) {
- t = t->left;
- } else {
- t = t->right;
- }
- }
- return t;
- }
- int tree_max(tree* head, int max) {
- if (!head) return 0;
- max = head->count;
- int a = tree_max(head->left, max);
- int b = tree_max(head->right, max);
- if (a > max) max = a;
- if (b > max) max = b;
- return max;
- }
- void print_maxes(tree* head, int max) {
- if (!head) return;
- if (head->count == max) {
- fprintf(f_out, "%d\n", head->val);
- }
- print_maxes(head->left, max);
- print_maxes(head->right, max);
- }
- int brojKategorije(int iznos) {
- if (iznos < kategorije[0]) return -1;
- for (int i = 0; i < 8; i++) {
- if (iznos >= kategorije[i]) {
- if (i == 7) return 8;
- if (iznos < kategorije[i + 1]) return i + 1;
- }
- }
- return -1;
- }
- // ./a <broj_kategorije>
- int main(int argc, char** argv) {
- mojaKategorija = argv[1][0] - '0';
- mojaKategorija++;
- char f_out_name[20] = "kat";
- f_out_name[3] = mojaKategorija + '0';
- f_out_name[4] = '\0';
- f_out = fopen(strcat(f_out_name, ".txt"), "w");
- FILE* kat_f = fopen("kategorizacija.txt", "r");
- for (int i = 0; i < 8; i++) {
- fscanf(kat_f, "%d", &kategorije[i]);
- }
- fclose(kat_f);
- char f_name[20] = "kvartal";
- int pibu, pibp, iznos;
- FILE* f_kvart;
- tree* t;
- for (int i = 1; i <= 4; i++) {
- f_name[7] = i + '0';
- f_name[8] = '\0';
- f_kvart = fopen(strcat(f_name, ".txt"), "r");
- while (1) {
- fscanf(f_kvart, "%d", &pibu);
- if (feof(f_kvart)) break;
- fscanf(f_kvart, "%d%d", &pibp, &iznos);
- if (brojKategorije(iznos) == mojaKategorija) {
- if (!find(pibs, pibu)) {
- pibs = insert(pibs, pibu);
- }
- if (!find(pibs, pibp)) {
- pibs = insert(pibs, pibp);
- }
- t = find(pibs, pibu);
- t->count++;
- t = find(pibs, pibp);
- t->count++;
- }
- }
- fclose(f_kvart);
- }
- int max = tree_max(pibs, 0);
- print_maxes(pibs, max);
- fclose(f_out);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement