#include "a_station.h" #include #include struct car { char target[50]; // Cíl trati, řetězec int capacity; // Kapacita trati }; struct station { struct car** tracks; // Pole ukazatelů na `car` (jednotlivé tratě) int track_count; // Počet tratí }; 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) { 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) { 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; } 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; }