usaa24/cv6/a_station.c

91 lines
2.9 KiB
C
Raw Normal View History

2024-11-06 11:10:16 +00:00
#include "a_station.h"
#include <stdlib.h>
#include <string.h>
2024-11-06 11:17:21 +00:00
// Funkce pro vytvoření stanice
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:17:21 +00:00
// Funkce pro zrušení stanice a uvolnění paměti
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++) {
2024-11-06 11:17:21 +00:00
struct car* current = station->tracks[i];
while (current != NULL) {
struct car* next = current->next;
free(current);
current = next;
}
2024-11-06 11:14:33 +00:00
}
free(station->tracks);
free(station);
2024-11-06 11:10:16 +00:00
}
2024-11-06 11:17:21 +00:00
// Funkce pro výběr trati na základě cíle
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++) {
2024-11-06 11:17:21 +00:00
if (station->tracks[i] != NULL && strcmp(station->tracks[i]->value, target) == 0) {
2024-11-06 11:14:33 +00:00
return i;
}
}
return -1;
2024-11-06 11:10:16 +00:00
}
2024-11-06 11:17:21 +00:00
// Přidá novou kapacitu do cíle nebo vytvoří novou trať, pokud neexistuje
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) {
2024-11-06 11:17:21 +00:00
// Pokud trať existuje, přičteme kapacitu
2024-11-06 11:14:33 +00:00
station->tracks[track_index]->capacity += capacity;
} else {
2024-11-06 11:17:21 +00:00
// Pokud trať neexistuje, najdeme prázdné místo a přidáme novou trať
2024-11-06 11:14:33 +00:00
for (int i = 0; i < station->track_count; i++) {
if (station->tracks[i] == NULL) {
2024-11-06 11:17:21 +00:00
struct car* new_car = (struct car*)malloc(sizeof(struct car));
strncpy(new_car->value, target, TARGET_SIZE - 1);
new_car->value[TARGET_SIZE - 1] = '\0';
new_car->capacity = capacity;
new_car->next = NULL;
station->tracks[i] = new_car;
2024-11-06 11:14:33 +00:00
break;
}
}
}
2024-11-06 11:10:16 +00:00
}
2024-11-06 11:17:21 +00:00
// Vrací kapacitu pro daný cíl
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:17:21 +00:00
// Spočítá počet existujících cílů (traťových záznamů)
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:17:21 +00:00
// Vrací celkovou kapacitu všech cílů
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;
}