Update cv6/a_station.c

This commit is contained in:
Viktor Daniv 2024-11-06 11:17:21 +00:00
parent b092ab2b21
commit cca70ebd33

View File

@ -2,18 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
// Funkce pro vytvoření stanice
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* create_station() {
struct station* station = (struct station*)calloc(1, sizeof(struct station)); struct station* station = (struct station*)calloc(1, sizeof(struct station));
station->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*)); station->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*));
@ -21,43 +10,56 @@ struct station* create_station() {
return station; return station;
} }
// Funkce pro zrušení stanice a uvolnění paměti
void destroy_station(struct station* station) { void destroy_station(struct station* station) {
if (station == NULL) return; if (station == NULL) return;
for (int i = 0; i < station->track_count; i++) { for (int i = 0; i < station->track_count; i++) {
free(station->tracks[i]); struct car* current = station->tracks[i];
while (current != NULL) {
struct car* next = current->next;
free(current);
current = next;
}
} }
free(station->tracks); free(station->tracks);
free(station); free(station);
} }
// Funkce pro výběr trati na základě cíle
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++) { for (int i = 0; i < station->track_count; i++) {
if (station->tracks[i] != NULL && strcmp(station->tracks[i]->target, target) == 0) { if (station->tracks[i] != NULL && strcmp(station->tracks[i]->value, target) == 0) {
return i; return i;
} }
} }
return -1; return -1;
} }
// Přidá novou kapacitu do cíle nebo vytvoří novou trať, pokud neexistuje
void add_target_capacity(struct station* station, const char* target, int capacity) { void add_target_capacity(struct station* station, const char* target, int capacity) {
int track_index = select_track(station, target); int track_index = select_track(station, target);
if (track_index != -1) { if (track_index != -1) {
// Pokud trať existuje, přičteme kapacitu
station->tracks[track_index]->capacity += capacity; station->tracks[track_index]->capacity += capacity;
} else { } else {
// Pokud trať neexistuje, najdeme prázdné místo a přidáme novou trať
for (int i = 0; i < station->track_count; i++) { for (int i = 0; i < station->track_count; i++) {
if (station->tracks[i] == NULL) { if (station->tracks[i] == NULL) {
station->tracks[i] = (struct car*)malloc(sizeof(struct car)); struct car* new_car = (struct car*)malloc(sizeof(struct car));
strncpy(station->tracks[i]->target, target, sizeof(station->tracks[i]->target) - 1); strncpy(new_car->value, target, TARGET_SIZE - 1);
station->tracks[i]->target[sizeof(station->tracks[i]->target) - 1] = '\0'; new_car->value[TARGET_SIZE - 1] = '\0';
station->tracks[i]->capacity = capacity; new_car->capacity = capacity;
new_car->next = NULL;
station->tracks[i] = new_car;
break; break;
} }
} }
} }
} }
// Vrací kapacitu pro daný cíl
int get_target_capacity(struct station* station, const char* target) { int get_target_capacity(struct station* station, const char* target) {
int track_index = select_track(station, target); int track_index = select_track(station, target);
if (track_index != -1) { if (track_index != -1) {
@ -66,6 +68,7 @@ int get_target_capacity(struct station* station, const char* target) {
return 0; return 0;
} }
// Spočítá počet existujících cílů (traťových záznamů)
int count_targets(struct station* station) { int count_targets(struct station* station) {
int count = 0; int count = 0;
for (int i = 0; i < station->track_count; i++) { for (int i = 0; i < station->track_count; i++) {
@ -76,6 +79,7 @@ int count_targets(struct station* station) {
return count; return count;
} }
// Vrací celkovou kapacitu všech cílů
int count_capacity(struct station* station) { int count_capacity(struct station* station) {
int total_capacity = 0; int total_capacity = 0;
for (int i = 0; i < station->track_count; i++) { for (int i = 0; i < station->track_count; i++) {