134 lines
3.9 KiB
C
134 lines
3.9 KiB
C
#include "a_station.h"
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
|
|
#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
|
|
struct station* create_station() {
|
|
struct station* station = calloc(1, sizeof(struct station));
|
|
station->tracks = calloc(STATION_SIZE, sizeof(struct car*));
|
|
station->track_count = STATION_SIZE;
|
|
return station;
|
|
}
|
|
|
|
// Zrušení stanice a uvolnění paměti
|
|
void destroy_station(struct station* station) {
|
|
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);
|
|
}
|
|
|
|
// Přidání nebo aktualizace kapacity pro stanici
|
|
void add_target_capacity(struct station* station, const char* target, int capacity) {
|
|
int track_index = select_track(station, target);
|
|
struct car* current = station->tracks[track_index];
|
|
|
|
// 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;
|
|
}
|
|
current = current->next;
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
// Vyhledání kapacity pro stanici
|
|
int get_target_capacity(struct station* station, const char* target) {
|
|
int track_index = select_track(station, target);
|
|
struct car* current = station->tracks[track_index];
|
|
|
|
while (current != NULL) {
|
|
if (strcmp(current->value, target) == 0) {
|
|
return current->capacity;
|
|
}
|
|
current = current->next;
|
|
}
|
|
return 0; // Stanice nenalezena
|
|
}
|
|
|
|
// Počet různých destinací
|
|
int count_targets(struct station* station) {
|
|
int count = 0;
|
|
for (int i = 0; i < station->track_count; i++) {
|
|
struct car* current = station->tracks[i];
|
|
while (current != NULL) {
|
|
count++;
|
|
current = current->next;
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
|
|
// Celková kapacita všech destinací
|
|
int count_capacity(struct station* station) {
|
|
int total_capacity = 0;
|
|
for (int i = 0; i < station->track_count; i++) {
|
|
struct car* current = station->tracks[i];
|
|
while (current != NULL) {
|
|
total_capacity += current->capacity;
|
|
current = current->next;
|
|
}
|
|
}
|
|
return total_capacity;
|
|
}
|
|
|
|
// 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");
|
|
} |