2024-11-06 11:10:16 +00:00
|
|
|
#include "a_station.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2024-11-06 11:16:09 +00:00
|
|
|
|
|
|
|
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í
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2024-11-06 11:14:33 +00:00
|
|
|
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;
|
2024-11-06 11:10:16 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 11:14:33 +00:00
|
|
|
void destroy_station(struct station* station) {
|
|
|
|
if (station == NULL) return;
|
2024-11-06 11:10:16 +00:00
|
|
|
|
2024-11-06 11:14:33 +00:00
|
|
|
for (int i = 0; i < station->track_count; i++) {
|
|
|
|
free(station->tracks[i]);
|
|
|
|
}
|
|
|
|
free(station->tracks);
|
|
|
|
free(station);
|
2024-11-06 11:10:16 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 11:14:33 +00:00
|
|
|
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;
|
2024-11-06 11:10:16 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 11:14:33 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-06 11:10:16 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 11:14:33 +00:00
|
|
|
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;
|
|
|
|
}
|
2024-11-06 11:10:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-11-06 11:14:33 +00:00
|
|
|
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;
|
2024-11-06 11:10:16 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 11:14:33 +00:00
|
|
|
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;
|
|
|
|
}
|