91 lines
2.9 KiB
C
91 lines
2.9 KiB
C
#include "a_station.h"
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
// Funkce pro vytvoření stanice
|
|
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;
|
|
}
|
|
|
|
// Funkce pro zrušení stanice a uvolnění paměti
|
|
void destroy_station(struct station* station) {
|
|
if (station == NULL) return;
|
|
|
|
for (int i = 0; i < station->track_count; i++) {
|
|
struct car* current = station->tracks[i];
|
|
while (current != NULL) {
|
|
struct car* next = current->next;
|
|
free(current);
|
|
current = next;
|
|
}
|
|
}
|
|
free(station->tracks);
|
|
free(station);
|
|
}
|
|
|
|
// Funkce pro výběr trati na základě cíle
|
|
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]->value, target) == 0) {
|
|
return i;
|
|
}
|
|
}
|
|
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) {
|
|
int track_index = select_track(station, target);
|
|
|
|
if (track_index != -1) {
|
|
// Pokud trať existuje, přičteme kapacitu
|
|
station->tracks[track_index]->capacity += capacity;
|
|
} else {
|
|
// Pokud trať neexistuje, najdeme prázdné místo a přidáme novou trať
|
|
for (int i = 0; i < station->track_count; i++) {
|
|
if (station->tracks[i] == NULL) {
|
|
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;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Vrací kapacitu pro daný cíl
|
|
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;
|
|
}
|
|
|
|
// Spočítá počet existujících cílů (traťových záznamů)
|
|
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;
|
|
}
|
|
|
|
// Vrací celkovou kapacitu všech cílů
|
|
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;
|
|
} |