diff --git a/du6/program.c b/du6/program.c new file mode 100644 index 0000000..9438496 --- /dev/null +++ b/du6/program.c @@ -0,0 +1,108 @@ +#include "a_station.h" +#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; +} + +// звільнення пам'яті +void destroy_station(struct station* station){ + for(int i = 0; i < station->track_count; i++){ + struct car* cur = station->tracks[i]; + + while(cur != NULL){ + struct car* tmp = cur; + cur = cur->next; + free(tmp); + } + } + + free(station->tracks); + free(station); +} + +// проста хеш-функція +int select_track(struct station* station, const char* target){ + int sum = 0; + + for(int i = 0; target[i] != '\0'; i++){ + sum += target[i]; + } + + return sum % station->track_count; +} + +// додавання або оновлення +void add_target_capacity(struct station* station,const char* target, int capacity){ + int index = select_track(station, target); + struct car* cur = station->tracks[index]; + + // шукаємо існуючий запис + while(cur != NULL){ + if(strcmp(cur->value, target) == 0){ + cur->capacity += capacity; + return; + } + cur = cur->next; + } + + // якщо нема — створюємо новий + struct car* new_car = (struct car*)malloc(sizeof(struct car)); + strcpy(new_car->value, target); + 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* cur = station->tracks[index]; + + while(cur != NULL){ + if(strcmp(cur->value, target) == 0){ + return cur->capacity; + } + cur = cur->next; + } + + return 0; +} + +// кількість станцій +int count_targets(struct station* station){ + int count = 0; + + for(int i = 0; i < station->track_count; i++){ + struct car* cur = station->tracks[i]; + + while(cur != NULL){ + count++; + cur = cur->next; + } + } + + return count; +} + +// загальна кількість пасажирів +int count_capacity(struct station* station){ + int sum = 0; + + for(int i = 0; i < station->track_count; i++){ + struct car* cur = station->tracks[i]; + + while(cur != NULL){ + sum += cur->capacity; + cur = cur->next; + } + } + + return sum; +} \ No newline at end of file