From 298918db19d1c04991aa84ba17955dc2655f8ef5 Mon Sep 17 00:00:00 2001 From: Oleksandr Hryshchenko Date: Fri, 19 Nov 2021 00:26:44 +0000 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB(?= =?UTF-8?q?=D0=B0)=20=D0=BD=D0=B0=20'cv7/a=5Fstation.c'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cv7/a_station.c | 71 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 17 deletions(-) diff --git a/cv7/a_station.c b/cv7/a_station.c index 7af372e..6bf7439 100644 --- a/cv7/a_station.c +++ b/cv7/a_station.c @@ -2,39 +2,76 @@ #include #include -//#define SIZE 100; - 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)); + station->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*)); + station->track_count = STATION_SIZE; + return station; } void destroy_station(struct station* station){ - + free(station->tracks); + free(station); } int select_track(struct station* station, const char* target){ - - return 0; + if(!station || station->track_count == 0) + return 0; + + int hash_result = (int)strlen(target) % station->track_count; + struct car* current = station->tracks[hash_result]; + + while(current != NULL && abs(strcmp(station->tracks[hash_result]->value, target))){ + hash_result = (hash_result * 2 + 1) % station->track_count; + current = station->tracks[hash_result]; + } + + return hash_result; } void add_target_capacity(struct station* station,const char* target, int capacity){ + if(!station || station->track_count == 0) + return; + + int myTrack = select_track(station, target); + + struct car* uniqueCar = station->tracks[myTrack]; + if(!uniqueCar) { + uniqueCar = (struct car *) calloc(1, sizeof(struct car)); + uniqueCar->capacity = 0; + strcpy(uniqueCar->value, target); + uniqueCar->next = NULL; + } + uniqueCar->capacity += capacity; + station->tracks[myTrack] = uniqueCar; } int get_target_capacity(struct station* station,const char* target){ - - return 0; + if(!station || station->track_count == 0 || station->tracks[select_track(station, target)] == NULL) + return 0; + + return station->tracks[select_track(station, target)]->capacity; } int count_targets(struct station* station){ - - - return 0; + if(!station || station->track_count == 0) + return 0; + + int numberOfTargets = 0; + for(int i = 0; i < station->track_count; i++) + numberOfTargets += (station->tracks[i] == NULL) ? 0 : 1; + + return numberOfTargets; } int count_capacity(struct station* station){ - - return 0; -} \ No newline at end of file + if(!station || station->track_count == 0) + return 0; + + int totalCapacity = 0; + for(int i = 0; i < station->track_count; i++) + totalCapacity += (station->tracks[i] == NULL) ? 0 : station->tracks[i]->capacity; + + return totalCapacity; +} +