Update cv6/a_station.c
This commit is contained in:
parent
cca70ebd33
commit
8cda1cc888
125
cv6/a_station.c
125
cv6/a_station.c
@ -1,19 +1,30 @@
|
||||
#include "a_station.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Funkce pro vytvoření stanice
|
||||
#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 = (struct station*)calloc(1, sizeof(struct station));
|
||||
station->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*));
|
||||
struct station* station = calloc(1, sizeof(struct station));
|
||||
station->tracks = calloc(STATION_SIZE, sizeof(struct car*));
|
||||
station->track_count = STATION_SIZE;
|
||||
return station;
|
||||
}
|
||||
|
||||
// Funkce pro zrušení stanice a uvolnění paměti
|
||||
// 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) {
|
||||
@ -26,66 +37,98 @@ void destroy_station(struct station* station) {
|
||||
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
|
||||
// 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];
|
||||
|
||||
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;
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Vrací kapacitu pro daný cíl
|
||||
// Vyhledání kapacity pro stanici
|
||||
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;
|
||||
struct car* current = station->tracks[track_index];
|
||||
|
||||
while (current != NULL) {
|
||||
if (strcmp(current->value, target) == 0) {
|
||||
return current->capacity;
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
return 0;
|
||||
return 0; // Stanice nenalezena
|
||||
}
|
||||
|
||||
// Spočítá počet existujících cílů (traťových záznamů)
|
||||
// Počet různých destinací
|
||||
int count_targets(struct station* station) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < station->track_count; i++) {
|
||||
if (station->tracks[i] != NULL) {
|
||||
struct car* current = station->tracks[i];
|
||||
while (current != NULL) {
|
||||
count++;
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// Vrací celkovou kapacitu všech cílů
|
||||
// Celková kapacita všech destinací
|
||||
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;
|
||||
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");
|
||||
}
|
Loading…
Reference in New Issue
Block a user