From d1b91c4949f61017f9cb3d4fb6b84fd09df441d9 Mon Sep 17 00:00:00 2001 From: Filip Chochol Date: Tue, 14 Apr 2026 11:29:06 +0200 Subject: [PATCH] push --- du6/a_station.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 du6/a_station.c diff --git a/du6/a_station.c b/du6/a_station.c new file mode 100644 index 0000000..04a699d --- /dev/null +++ b/du6/a_station.c @@ -0,0 +1,93 @@ +#include "a_station.h" +#include +#include + +struct station* create_station(){ + struct station* stanica = calloc(1, sizeof(struct station)); + stanica->tracks = calloc(STATION_SIZE, sizeof(struct car*)); + stanica->track_count = STATION_SIZE; + return stanica; +} + +void destroy_station(struct station* stanica){ + for (int i = 0; i < stanica->track_count; i++){ + struct car* aktualny = stanica->tracks[i]; + while (aktualny != NULL){ + struct car* nasledujuci = aktualny->next; + free(aktualny); + aktualny = nasledujuci; + } + } + free(stanica->tracks); + free(stanica); +} + +int select_track(struct station* stanica, const char* ciel){ + unsigned long heslo = 5381; + int znak; + while ((znak = *ciel++)){ + heslo = ((heslo << 5) + heslo) + znak; + } + return heslo % stanica->track_count; +} + +void add_target_capacity(struct station* stanica, const char* ciel, int kapacita){ + int index = select_track(stanica, ciel); + struct car* aktualny = stanica->tracks[index]; + + while (aktualny != NULL){ + if (strcmp(aktualny->value, ciel) == 0){ + aktualny->capacity += kapacita; + return; + } + aktualny = aktualny->next; + } + + struct car* novy = calloc(1, sizeof(struct car)); + strncpy(novy->value, ciel, TARGET_SIZE - 1); + novy->capacity = kapacita; + novy->next = stanica->tracks[index]; + stanica->tracks[index] = novy; +} + +int get_target_capacity(struct station* stanica, const char* ciel){ + int index = select_track(stanica, ciel); + struct car* aktualny = stanica->tracks[index]; + + while (aktualny != NULL){ + if (strcmp(aktualny->value, ciel) == 0){ + return aktualny->capacity; + } + aktualny = aktualny->next; + } + + return 0; +} + +int count_targets(struct station* stanica){ + int pocet = 0; + + for (int i = 0; i < stanica->track_count; i++){ + struct car* aktualny = stanica->tracks[i]; + while (aktualny != NULL){ + pocet++; + aktualny = aktualny->next; + } + } + + return pocet; +} + +int count_capacity(struct station* stanica){ + int celkovo = 0; + + for (int i = 0; i < stanica->track_count; i++){ + struct car* aktualny = stanica->tracks[i]; + while (aktualny != NULL){ + celkovo += aktualny->capacity; + aktualny = aktualny->next; + } + } + + return celkovo; +}