From 6b822889fefc3b391d23df37ff14301ec42db760 Mon Sep 17 00:00:00 2001 From: Weber Date: Wed, 6 Nov 2024 21:00:33 +0000 Subject: [PATCH] test --- cv6/a_station.c | 93 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 74 insertions(+), 19 deletions(-) diff --git a/cv6/a_station.c b/cv6/a_station.c index 8f659d6..32d5322 100644 --- a/cv6/a_station.c +++ b/cv6/a_station.c @@ -2,32 +2,87 @@ #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; +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; } -void destroy_station(struct station* station){ +void destroy_station(struct station* stanica) { + for (int i = 0; i < stanica->track_count; ++i) { + struct car* aktualny = stanica->tracks[i]; + while (aktualny) { + struct car* docasny = aktualny; + aktualny = aktualny->next; + free(docasny); + } + } + free(stanica->tracks); + free(stanica); } -int select_track(struct station* station, const char* target){ +int select_track(struct station* stanica, const char* ciel) { + unsigned int hash = 0; + for (int i = 0; ciel[i] != '\0'; i++) { + hash = (hash * 31 + ciel[i]) % stanica->track_count; + } + return hash; +} + +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_voz = (struct car*)malloc(sizeof(struct car)); + strncpy(novy_voz->value, ciel, TARGET_SIZE - 1); + novy_voz->value[TARGET_SIZE - 1] = '\0'; + novy_voz->capacity = kapacita; + novy_voz->next = stanica->tracks[index]; + stanica->tracks[index] = novy_voz; +} + +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; } -void add_target_capacity(struct station* station,const char* target, int capacity){ +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) { + pocet++; + aktualny = aktualny->next; + } + } + return pocet; } -int get_target_capacity(struct station* station,const char* target){ - return 0; +int count_capacity(struct station* stanica) { + int celkova_kapacita = 0; + for (int i = 0; i < stanica->track_count; i++) { + struct car* aktualny = stanica->tracks[i]; + while (aktualny) { + celkova_kapacita += aktualny->capacity; + aktualny = aktualny->next; + } + } + return celkova_kapacita; } - -int count_targets(struct station* station){ - return 0; -} - -int count_capacity(struct station* station){ - return 0; -} -