From 238a1d7c6620d2011adf284c17c68ad8b34214b8 Mon Sep 17 00:00:00 2001 From: Illia Korpan Date: Fri, 24 Apr 2026 10:30:33 +0200 Subject: [PATCH] 123 --- du6/a_station.h | 91 ++++++++++++++++++++++++++++++++++++++++++ du6/program.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 195 insertions(+) create mode 100644 du6/a_station.h create mode 100644 du6/program.c diff --git a/du6/a_station.h b/du6/a_station.h new file mode 100644 index 0000000..ce8c733 --- /dev/null +++ b/du6/a_station.h @@ -0,0 +1,91 @@ +#ifndef STATION_H +#define STATION_H +// Pocet trati k dispozicii +#define STATION_SIZE 10 +// Maximalny pocet znakov pre ulozenie nazvu cielovej stanice +#define TARGET_SIZE 36 + +/** + * Jeden zaznam o kapacite do cielovej stanice + */ +struct car { + // Cielova stanica / nazov + char value[TARGET_SIZE]; + // Pocet cestujuchich + int capacity; + // Smernik na dalsi zaznam + struct car* next; +}; + +/** + * Cela databaza zaznamov + */ +struct station { + // Dynamicke pole smernikov na zaznamy + // jeden zaznam ma typ struct car* + struct car** tracks; + // Velkost pola tracks + int track_count; +}; + + +/** + * Vytvori prazdnu stanicu. + * Alokuje pole smernikov tracks na pociatocnu kapacitu STATION_SIZE + * nastavi velkost capacity na STATION_SIZE + * @return smernik na prazdnu stanicu + */ +struct station* create_station(); + +/** + * Uvolni pamat + * @param smernik na databazu + */ +void destroy_station(struct station* station); + +/** + * Vyberie poziciu v poli station->tracks pre ulozenie zaznamu target + * + * Proces vyberu by mal splnat kriteria pre hash funkciu: + * - rovnaky retazec by mal vzdy mat rovnaky vysledok + * - pre rozne retazce by mali byt vysledky co najviac rozne + * + * @param smernik na databazu + * @param nazov cielovej stanice + * @return cislo v intervale 0 az N-1, kde N je station->track_count + */ +int select_track(struct station* station, const char* target); + +/** + * Zvysi zaznam o pocte cestujucich do danej cielovej stanice. + * + * Najprv sa vyberie cielova trat pomocou select_track(). Ak zaznam neexistuje, + * vytvori sa novy. Ak zaznam na danej trati (spojkovom zozname) existuje, cislo sa pripocita. + * V databaze nesmu byt dva zaznamy s rovnakou cielovou stanicou. + * + * @param smernik na databazu + * @param nazov cielovej stanice + */ +void add_target_capacity(struct station* station,const char* target, int capacity); + +/** + * Ziska zaznam o cielovej stanici. + * @param smernik na databazu + * @param nazov cielovej stanice + * + * @return kapacitu do cielovej stanice. Ak sa zaznam nenachedza, vrati nula. + */ +int get_target_capacity(struct station* station,const char* target); + +/** + * Spocita pocet cielovych stanic + * @param smernik na databazu + */ +int count_targets(struct station* station); + +/** + * Spocita kapacitu vo vsetkych zaznamoch + * @param smernik na databazu + */ +int count_capacity(struct station* station); +#endif diff --git a/du6/program.c b/du6/program.c new file mode 100644 index 0000000..765c6bf --- /dev/null +++ b/du6/program.c @@ -0,0 +1,104 @@ +#include "a_station.h" +#include +#include + +struct station* create_station(){ + struct station* station = (struct station*)calloc(1,sizeof(struct station)); + station->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*)); + station->track_count = STATION_SIZE; + return station; +} + + +/* djb2 hash function by Dan Bernstein - deterministic and simple */ +static unsigned long hash_str(const char *str) { + unsigned long hash = 5381; + int c; + while ((c = (unsigned char)*str++)) + hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ + return hash; +} + +void destroy_station(struct station* station){ + if(!station) return; + if(station->tracks){ + for(int i = 0; i < station->track_count; ++i){ + struct car* cur = station->tracks[i]; + while(cur){ + struct car* next = cur->next; + free(cur); + cur = next; + } + } + free(station->tracks); + } + free(station); +} + +int select_track(struct station* station, const char* target){ + if(!station || !target) return 0; + unsigned long h = hash_str(target); + return (int)(h % (unsigned long)station->track_count); +} + +void add_target_capacity(struct station* station,const char* target, int capacity){ + if(!station || !target || capacity == 0) return; + int idx = select_track(station, target); + struct car* cur = station->tracks[idx]; + struct car* prev = NULL; + while(cur){ + if(strncmp(cur->value, target, TARGET_SIZE) == 0){ + cur->capacity += capacity; + return; + } + prev = cur; + cur = cur->next; + } + /* not found -> create new record and append at head for simplicity */ + struct car* node = (struct car*)calloc(1, sizeof(struct car)); + if(!node) return; /* allocation failed */ + strncpy(node->value, target, TARGET_SIZE - 1); + node->value[TARGET_SIZE-1] = '\0'; + node->capacity = capacity; + node->next = station->tracks[idx]; + station->tracks[idx] = node; +} + +int get_target_capacity(struct station* station,const char* target){ + if(!station || !target) return 0; + int idx = select_track(station, target); + struct car* cur = station->tracks[idx]; + while(cur){ + if(strncmp(cur->value, target, TARGET_SIZE) == 0){ + return cur->capacity; + } + cur = cur->next; + } + return 0; +} + +int count_targets(struct station* station){ + if(!station) return 0; + int count = 0; + for(int i = 0; i < station->track_count; ++i){ + struct car* cur = station->tracks[i]; + while(cur){ + count++; + cur = cur->next; + } + } + return count; +} + +int count_capacity(struct station* station){ + if(!station) return 0; + int total = 0; + for(int i = 0; i < station->track_count; ++i){ + struct car* cur = station->tracks[i]; + while(cur){ + total += cur->capacity; + cur = cur->next; + } + } + return total; +}