From a8b18d286477ad56965f543e23ac8007ac228587 Mon Sep 17 00:00:00 2001 From: Viktor Daniv Date: Wed, 6 Nov 2024 11:14:33 +0000 Subject: [PATCH] Update cv6/a_station.c --- cv6/a_station.c | 82 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 62 insertions(+), 20 deletions(-) diff --git a/cv6/a_station.c b/cv6/a_station.c index 8f659d6..d0e7f97 100644 --- a/cv6/a_station.c +++ b/cv6/a_station.c @@ -2,32 +2,74 @@ #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* station) { + if (station == NULL) return; + + for (int i = 0; i < station->track_count; i++) { + free(station->tracks[i]); + } + free(station->tracks); + free(station); } -int select_track(struct station* station, const char* target){ +int select_track(struct station* station, const char* target) { + for (int i = 0; i < station->track_count; i++) { + if (station->tracks[i] != NULL && strcmp(station->tracks[i]->target, target) == 0) { + return i; + } + } + return -1; +} + +void add_target_capacity(struct station* station, const char* target, int capacity) { + int track_index = select_track(station, target); + + if (track_index != -1) { + station->tracks[track_index]->capacity += capacity; + } else { + for (int i = 0; i < station->track_count; i++) { + if (station->tracks[i] == NULL) { + station->tracks[i] = (struct car*)malloc(sizeof(struct car)); + strncpy(station->tracks[i]->target, target, sizeof(station->tracks[i]->target) - 1); + station->tracks[i]->target[sizeof(station->tracks[i]->target) - 1] = '\0'; + station->tracks[i]->capacity = capacity; + break; + } + } + } +} + +int get_target_capacity(struct station* station, const char* target) { + int track_index = select_track(station, target); + if (track_index != -1) { + return station->tracks[track_index]->capacity; + } return 0; } -void add_target_capacity(struct station* station,const char* target, int capacity){ -} - -int get_target_capacity(struct station* station,const char* target){ - return 0; -} - -int count_targets(struct station* station){ - return 0; -} - -int count_capacity(struct station* station){ - return 0; +int count_targets(struct station* station) { + int count = 0; + for (int i = 0; i < station->track_count; i++) { + if (station->tracks[i] != NULL) { + count++; + } + } + return count; } +int count_capacity(struct station* station) { + int total_capacity = 0; + for (int i = 0; i < station->track_count; i++) { + if (station->tracks[i] != NULL) { + total_capacity += station->tracks[i]->capacity; + } + } + return total_capacity; +} \ No newline at end of file