2024-11-06 11:10:16 +00:00
|
|
|
#include "a_station.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2024-11-06 11:19:56 +00:00
|
|
|
#include <stdio.h>
|
2024-11-06 11:10:16 +00:00
|
|
|
|
2024-11-06 11:19:56 +00:00
|
|
|
#define HASH_BASE 31 // Hashovací základ pro lepší rozložení hodnot
|
|
|
|
|
|
|
|
// Hešovací funkce, která převede hodnotu na index
|
|
|
|
int select_track(struct station* station, const char* target) {
|
|
|
|
unsigned long hash = 0;
|
|
|
|
while (*target) {
|
|
|
|
hash = hash * HASH_BASE + (unsigned char)(*target);
|
|
|
|
target++;
|
|
|
|
}
|
|
|
|
return hash % station->track_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inicializace stanice
|
2024-11-06 11:14:33 +00:00
|
|
|
struct station* create_station() {
|
2024-11-06 11:19:56 +00:00
|
|
|
struct station* station = calloc(1, sizeof(struct station));
|
|
|
|
station->tracks = calloc(STATION_SIZE, sizeof(struct car*));
|
2024-11-06 11:14:33 +00:00
|
|
|
station->track_count = STATION_SIZE;
|
|
|
|
return station;
|
2024-11-06 11:10:16 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 11:19:56 +00:00
|
|
|
// Zrušení stanice a uvolnění paměti
|
2024-11-06 11:14:33 +00:00
|
|
|
void destroy_station(struct station* station) {
|
|
|
|
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:19:56 +00:00
|
|
|
// Přidání nebo aktualizace kapacity pro stanici
|
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);
|
2024-11-06 11:19:56 +00:00
|
|
|
struct car* current = station->tracks[track_index];
|
2024-11-06 11:14:33 +00:00
|
|
|
|
2024-11-06 11:19:56 +00:00
|
|
|
// Najdeme, zda již existuje záznam pro daný cíl
|
|
|
|
while (current != NULL) {
|
|
|
|
if (strcmp(current->value, target) == 0) {
|
|
|
|
current->capacity += capacity; // Zvýšení kapacity
|
|
|
|
return;
|
2024-11-06 11:14:33 +00:00
|
|
|
}
|
2024-11-06 11:19:56 +00:00
|
|
|
current = current->next;
|
2024-11-06 11:14:33 +00:00
|
|
|
}
|
2024-11-06 11:19:56 +00:00
|
|
|
|
|
|
|
// Pokud neexistuje, přidáme nový záznam
|
|
|
|
struct car* new_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 = station->tracks[track_index];
|
|
|
|
station->tracks[track_index] = new_car;
|
2024-11-06 11:10:16 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 11:19:56 +00:00
|
|
|
// Vyhledání kapacity pro stanici
|
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);
|
2024-11-06 11:19:56 +00:00
|
|
|
struct car* current = station->tracks[track_index];
|
|
|
|
|
|
|
|
while (current != NULL) {
|
|
|
|
if (strcmp(current->value, target) == 0) {
|
|
|
|
return current->capacity;
|
|
|
|
}
|
|
|
|
current = current->next;
|
2024-11-06 11:14:33 +00:00
|
|
|
}
|
2024-11-06 11:19:56 +00:00
|
|
|
return 0; // Stanice nenalezena
|
2024-11-06 11:10:16 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 11:19:56 +00:00
|
|
|
// Počet různých destinací
|
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++) {
|
2024-11-06 11:19:56 +00:00
|
|
|
struct car* current = station->tracks[i];
|
|
|
|
while (current != NULL) {
|
2024-11-06 11:14:33 +00:00
|
|
|
count++;
|
2024-11-06 11:19:56 +00:00
|
|
|
current = current->next;
|
2024-11-06 11:14:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
2024-11-06 11:10:16 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 11:19:56 +00:00
|
|
|
// Celková kapacita všech destinací
|
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++) {
|
2024-11-06 11:19:56 +00:00
|
|
|
struct car* current = station->tracks[i];
|
|
|
|
while (current != NULL) {
|
|
|
|
total_capacity += current->capacity;
|
|
|
|
current = current->next;
|
2024-11-06 11:14:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return total_capacity;
|
2024-11-06 11:19:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Načtení existující databáze z textového souboru
|
|
|
|
void load_database(struct station* station, const char* filename) {
|
|
|
|
FILE* file = fopen(filename, "r");
|
|
|
|
if (!file) {
|
|
|
|
perror("Chyba při otevírání souboru");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char target[TARGET_SIZE];
|
|
|
|
int capacity;
|
|
|
|
|
|
|
|
while (fscanf(file, "%s %d", target, &capacity) == 2) {
|
|
|
|
add_target_capacity(station, target, capacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Výpis aktuálního stavu stanice (pro ladění)
|
|
|
|
void print_station(struct station* station) {
|
|
|
|
printf("station>>>\n");
|
|
|
|
for (int i = 0; i < station->track_count; i++) {
|
|
|
|
struct car* current = station->tracks[i];
|
|
|
|
printf("[%d] ", i);
|
|
|
|
while (current != NULL) {
|
|
|
|
printf("%s (%d) -> ", current->value, current->capacity);
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
printf("NULL\n");
|
|
|
|
}
|
|
|
|
printf("<<<station\n");
|
2024-11-06 11:14:33 +00:00
|
|
|
}
|