Aktualizovat du6/a_station.c.
This commit is contained in:
parent
f9191363d9
commit
14303322b4
122
du6/a_station.c.
122
du6/a_station.c.
@ -1,17 +1,43 @@
|
||||
#include "a_station.h"
|
||||
#include <limits.h>
|
||||
#include <stdlib.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* station = (struct station*)calloc(1, sizeof(struct station));
|
||||
if (station == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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) {
|
||||
if (station == NULL) return;
|
||||
for (int i = 0; i < station->track_count; i++) {
|
||||
int i;
|
||||
|
||||
if (station == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (station->tracks != NULL) {
|
||||
for (i = 0; i < station->track_count; i++) {
|
||||
struct car* current = station->tracks[i];
|
||||
while (current != NULL) {
|
||||
struct car* next = current->next;
|
||||
@ -20,32 +46,64 @@ void destroy_station(struct station* station) {
|
||||
}
|
||||
}
|
||||
free(station->tracks);
|
||||
}
|
||||
|
||||
free(station);
|
||||
}
|
||||
|
||||
int select_track(struct station* station, const char* target) {
|
||||
unsigned long hash = 5381;
|
||||
int c;
|
||||
while ((c = (unsigned char)*target++)) {
|
||||
hash = ((hash << 5) + hash) + c;
|
||||
const unsigned char* p;
|
||||
char normalized[TARGET_SIZE];
|
||||
|
||||
if (station == NULL || station->track_count <= 0 || target == NULL) {
|
||||
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) {
|
||||
int index = select_track(station, target);
|
||||
struct car* current = station->tracks[index];
|
||||
int index;
|
||||
struct car* current;
|
||||
struct car* new_car;
|
||||
char normalized[TARGET_SIZE];
|
||||
|
||||
if (station == NULL || station->tracks == NULL || target == NULL || capacity <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
normalize_target(normalized, target);
|
||||
|
||||
index = select_track(station, normalized);
|
||||
current = station->tracks[index];
|
||||
|
||||
while (current != NULL) {
|
||||
if (strncmp(current->value, target, TARGET_SIZE) == 0) {
|
||||
if (strcmp(current->value, normalized) == 0) {
|
||||
if (current->capacity > INT_MAX - capacity) {
|
||||
current->capacity = INT_MAX;
|
||||
return;
|
||||
}
|
||||
current->capacity += capacity;
|
||||
return;
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
struct car* new_car = (struct car*)calloc(1, sizeof(struct car));
|
||||
strncpy(new_car->value, target, TARGET_SIZE - 1);
|
||||
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];
|
||||
@ -53,38 +111,66 @@ void add_target_capacity(struct station* station, const char* target, int capaci
|
||||
}
|
||||
|
||||
int get_target_capacity(struct station* station, const char* target) {
|
||||
int index = select_track(station, target);
|
||||
struct car* current = station->tracks[index];
|
||||
int index;
|
||||
struct car* current;
|
||||
char normalized[TARGET_SIZE];
|
||||
|
||||
if (station == NULL || station->tracks == NULL || target == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
normalize_target(normalized, target);
|
||||
|
||||
index = select_track(station, normalized);
|
||||
current = station->tracks[index];
|
||||
|
||||
while (current != NULL) {
|
||||
if (strncmp(current->value, target, TARGET_SIZE) == 0) {
|
||||
if (strcmp(current->value, normalized) == 0) {
|
||||
return current->capacity;
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int count_targets(struct station* station) {
|
||||
int i;
|
||||
int count = 0;
|
||||
for (int i = 0; i < station->track_count; i++) {
|
||||
|
||||
if (station == NULL || station->tracks == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (i = 0; i < station->track_count; i++) {
|
||||
struct car* current = station->tracks[i];
|
||||
while (current != NULL) {
|
||||
count++;
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
int count_capacity(struct station* station) {
|
||||
int total = 0;
|
||||
for (int i = 0; i < station->track_count; i++) {
|
||||
int i;
|
||||
int sum = 0;
|
||||
|
||||
if (station == NULL || station->tracks == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (i = 0; i < station->track_count; i++) {
|
||||
struct car* current = station->tracks[i];
|
||||
while (current != NULL) {
|
||||
total += current->capacity;
|
||||
if (current->capacity > 0 && sum > INT_MAX - current->capacity) {
|
||||
return INT_MAX;
|
||||
}
|
||||
sum += current->capacity;
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
return total;
|
||||
|
||||
return sum;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user