Aktualizovat du6/a_station.c.

This commit is contained in:
Daniel Dembický 2026-04-21 07:26:25 +00:00
parent f9191363d9
commit 14303322b4

View File

@ -1,90 +1,176 @@
#include "a_station.h" #include "a_station.h"
#include <limits.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
static void normalize_target(char normalized[TARGET_SIZE], const char* target) {
if (target == NULL) {
normalized[0] = '\0';
return;
}
strncpy(normalized, target, TARGET_SIZE - 1);
normalized[TARGET_SIZE - 1] = '\0';
}
struct station* create_station() { struct station* create_station() {
struct station* station = (struct station*)calloc(1, sizeof(struct station)); struct station* station = (struct station*)calloc(1, sizeof(struct station));
station->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*)); if (station == NULL) {
station->track_count = STATION_SIZE; return NULL;
return station; }
station->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*));
if (station->tracks == NULL) {
free(station);
return NULL;
}
station->track_count = STATION_SIZE;
return station;
} }
void destroy_station(struct station* station) { void destroy_station(struct station* station) {
if (station == NULL) return; int i;
for (int i = 0; i < station->track_count; i++) {
struct car* current = station->tracks[i]; if (station == NULL) {
while (current != NULL) { return;
struct car* next = current->next; }
free(current);
current = next; if (station->tracks != NULL) {
} for (i = 0; i < station->track_count; i++) {
} struct car* current = station->tracks[i];
free(station->tracks); while (current != NULL) {
free(station); struct car* next = current->next;
free(current);
current = next;
}
}
free(station->tracks);
}
free(station);
} }
int select_track(struct station* station, const char* target) { int select_track(struct station* station, const char* target) {
unsigned long hash = 5381; unsigned long hash = 5381;
int c; const unsigned char* p;
while ((c = (unsigned char)*target++)) { char normalized[TARGET_SIZE];
hash = ((hash << 5) + hash) + c;
} if (station == NULL || station->track_count <= 0 || target == NULL) {
return (int)(hash % (unsigned long)station->track_count); return 0;
}
normalize_target(normalized, target);
p = (const unsigned char*)normalized;
while (*p != '\0') {
hash = ((hash << 5) + hash) + *p;
p++;
}
return (int)(hash % (unsigned long)station->track_count);
} }
void add_target_capacity(struct station* station, const char* target, int capacity) { void add_target_capacity(struct station* station, const char* target, int capacity) {
int index = select_track(station, target); int index;
struct car* current = station->tracks[index]; struct car* current;
struct car* new_car;
char normalized[TARGET_SIZE];
while (current != NULL) { if (station == NULL || station->tracks == NULL || target == NULL || capacity <= 0) {
if (strncmp(current->value, target, TARGET_SIZE) == 0) { return;
current->capacity += capacity; }
return;
}
current = current->next;
}
struct car* new_car = (struct car*)calloc(1, sizeof(struct car)); normalize_target(normalized, target);
strncpy(new_car->value, target, TARGET_SIZE - 1);
new_car->value[TARGET_SIZE - 1] = '\0'; index = select_track(station, normalized);
new_car->capacity = capacity; current = station->tracks[index];
new_car->next = station->tracks[index];
station->tracks[index] = new_car; while (current != NULL) {
if (strcmp(current->value, normalized) == 0) {
if (current->capacity > INT_MAX - capacity) {
current->capacity = INT_MAX;
return;
}
current->capacity += capacity;
return;
}
current = current->next;
}
new_car = (struct car*)calloc(1, sizeof(struct car));
if (new_car == NULL) {
return;
}
strncpy(new_car->value, normalized, TARGET_SIZE - 1);
new_car->value[TARGET_SIZE - 1] = '\0';
new_car->capacity = capacity;
new_car->next = station->tracks[index];
station->tracks[index] = new_car;
} }
int get_target_capacity(struct station* station, const char* target) { int get_target_capacity(struct station* station, const char* target) {
int index = select_track(station, target); int index;
struct car* current = station->tracks[index]; struct car* current;
char normalized[TARGET_SIZE];
while (current != NULL) { if (station == NULL || station->tracks == NULL || target == NULL) {
if (strncmp(current->value, target, TARGET_SIZE) == 0) { return 0;
return current->capacity; }
}
current = current->next; normalize_target(normalized, target);
}
return 0; index = select_track(station, normalized);
current = station->tracks[index];
while (current != NULL) {
if (strcmp(current->value, normalized) == 0) {
return current->capacity;
}
current = current->next;
}
return 0;
} }
int count_targets(struct station* station) { int count_targets(struct station* station) {
int count = 0; int i;
for (int i = 0; i < station->track_count; i++) { int count = 0;
struct car* current = station->tracks[i];
while (current != NULL) { if (station == NULL || station->tracks == NULL) {
count++; return 0;
current = current->next; }
}
} for (i = 0; i < station->track_count; i++) {
return count; struct car* current = station->tracks[i];
while (current != NULL) {
count++;
current = current->next;
}
}
return count;
} }
int count_capacity(struct station* station) { int count_capacity(struct station* station) {
int total = 0; int i;
for (int i = 0; i < station->track_count; i++) { int sum = 0;
struct car* current = station->tracks[i];
while (current != NULL) { if (station == NULL || station->tracks == NULL) {
total += current->capacity; return 0;
current = current->next; }
}
} for (i = 0; i < station->track_count; i++) {
return total; struct car* current = station->tracks[i];
while (current != NULL) {
if (current->capacity > 0 && sum > INT_MAX - current->capacity) {
return INT_MAX;
}
sum += current->capacity;
current = current->next;
}
}
return sum;
} }