From 14303322b4f9fed3e50c50d9720e3a684c3ada1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Dembick=C3=BD?= Date: Tue, 21 Apr 2026 07:26:25 +0000 Subject: [PATCH] Aktualizovat du6/a_station.c. --- du6/a_station.c. | 214 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 150 insertions(+), 64 deletions(-) diff --git a/ du6/a_station.c. b/ du6/a_station.c. index 0cd0aa9..cc49a8d 100644 --- a/ du6/a_station.c. +++ b/ du6/a_station.c. @@ -1,90 +1,176 @@ #include "a_station.h" +#include #include #include +static void normalize_target(char normalized[TARGET_SIZE], const char* target) { + if (target == NULL) { + normalized[0] = '\0'; + return; + } + + strncpy(normalized, target, TARGET_SIZE - 1); + normalized[TARGET_SIZE - 1] = '\0'; +} + 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* station = (struct station*)calloc(1, sizeof(struct station)); + if (station == NULL) { + return NULL; + } + + station->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*)); + if (station->tracks == NULL) { + free(station); + return NULL; + } + + station->track_count = STATION_SIZE; + return station; } void destroy_station(struct station* station) { - if (station == NULL) return; - for (int i = 0; i < station->track_count; i++) { - struct car* current = station->tracks[i]; - while (current != NULL) { - struct car* next = current->next; - free(current); - current = next; - } - } - free(station->tracks); - free(station); + int i; + + if (station == NULL) { + return; + } + + if (station->tracks != NULL) { + for (i = 0; i < station->track_count; i++) { + struct car* current = station->tracks[i]; + while (current != NULL) { + struct car* next = current->next; + free(current); + current = next; + } + } + free(station->tracks); + } + + free(station); } int select_track(struct station* station, const char* target) { - unsigned long hash = 5381; - int c; - while ((c = (unsigned char)*target++)) { - hash = ((hash << 5) + hash) + c; - } - return (int)(hash % (unsigned long)station->track_count); + unsigned long hash = 5381; + const unsigned char* p; + char normalized[TARGET_SIZE]; + + if (station == NULL || station->track_count <= 0 || target == NULL) { + return 0; + } + + normalize_target(normalized, target); + p = (const unsigned char*)normalized; + + while (*p != '\0') { + hash = ((hash << 5) + hash) + *p; + p++; + } + + return (int)(hash % (unsigned long)station->track_count); } void add_target_capacity(struct station* station, const char* target, int capacity) { - int index = select_track(station, target); - struct car* current = station->tracks[index]; + int index; + struct car* current; + struct car* new_car; + char normalized[TARGET_SIZE]; - while (current != NULL) { - if (strncmp(current->value, target, TARGET_SIZE) == 0) { - current->capacity += capacity; - return; - } - current = current->next; - } + if (station == NULL || station->tracks == NULL || target == NULL || capacity <= 0) { + return; + } - struct car* new_car = (struct car*)calloc(1, sizeof(struct car)); - strncpy(new_car->value, target, TARGET_SIZE - 1); - new_car->value[TARGET_SIZE - 1] = '\0'; - new_car->capacity = capacity; - new_car->next = station->tracks[index]; - station->tracks[index] = new_car; + normalize_target(normalized, target); + + index = select_track(station, normalized); + current = station->tracks[index]; + + while (current != NULL) { + if (strcmp(current->value, normalized) == 0) { + if (current->capacity > INT_MAX - capacity) { + current->capacity = INT_MAX; + return; + } + current->capacity += capacity; + return; + } + current = current->next; + } + + new_car = (struct car*)calloc(1, sizeof(struct car)); + if (new_car == NULL) { + return; + } + + strncpy(new_car->value, normalized, TARGET_SIZE - 1); + new_car->value[TARGET_SIZE - 1] = '\0'; + new_car->capacity = capacity; + new_car->next = station->tracks[index]; + station->tracks[index] = new_car; } int get_target_capacity(struct station* station, const char* target) { - int index = select_track(station, target); - struct car* current = station->tracks[index]; + int index; + struct car* current; + char normalized[TARGET_SIZE]; - while (current != NULL) { - if (strncmp(current->value, target, TARGET_SIZE) == 0) { - return current->capacity; - } - current = current->next; - } - return 0; + if (station == NULL || station->tracks == NULL || target == NULL) { + return 0; + } + + normalize_target(normalized, target); + + index = select_track(station, normalized); + current = station->tracks[index]; + + while (current != NULL) { + if (strcmp(current->value, normalized) == 0) { + return current->capacity; + } + current = current->next; + } + + return 0; } int count_targets(struct station* station) { - int count = 0; - for (int i = 0; i < station->track_count; i++) { - struct car* current = station->tracks[i]; - while (current != NULL) { - count++; - current = current->next; - } - } - return count; + int i; + int count = 0; + + if (station == NULL || station->tracks == NULL) { + return 0; + } + + for (i = 0; i < station->track_count; i++) { + struct car* current = station->tracks[i]; + while (current != NULL) { + count++; + current = current->next; + } + } + + return count; } int count_capacity(struct station* station) { - int total = 0; - for (int i = 0; i < station->track_count; i++) { - struct car* current = station->tracks[i]; - while (current != NULL) { - total += current->capacity; - current = current->next; - } - } - return total; -} \ No newline at end of file + int i; + int sum = 0; + + if (station == NULL || station->tracks == NULL) { + return 0; + } + + for (i = 0; i < station->track_count; i++) { + struct car* current = station->tracks[i]; + while (current != NULL) { + if (current->capacity > 0 && sum > INT_MAX - current->capacity) { + return INT_MAX; + } + sum += current->capacity; + current = current->next; + } + } + + return sum; +}