refresh
This commit is contained in:
parent
4492629238
commit
26551e9f44
118
du5/a_program.c
118
du5/a_program.c
@ -1,111 +1,23 @@
|
|||||||
|
#include <stdio.h>
|
||||||
#include "a_station.h"
|
#include "a_station.h"
|
||||||
|
|
||||||
struct station* create_station() {
|
int main() {
|
||||||
struct station* s = calloc(1, sizeof(struct station));
|
struct station* s = create_station();
|
||||||
s->tracks = calloc(STATION_SIZE, sizeof(struct car*));
|
|
||||||
s->track_count = STATION_SIZE;
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
int select_track(struct station* s, const char* target) {
|
add_target_capacity(s, "Kosice", 120);
|
||||||
unsigned long hash = 5381; //5381 это алгоритм DJB2
|
add_target_capacity(s, "Presov", 60);
|
||||||
int c;
|
add_target_capacity(s, "Kosice", 30);
|
||||||
|
add_target_capacity(s, "Bratislava", 200);
|
||||||
|
add_target_capacity(s, "Zilina", 90);
|
||||||
|
|
||||||
while ((c = *target++)) { // не забываем ++ чтобы идти по строке
|
printf("Počet staníc: %d\n", count_targets(s));
|
||||||
hash = ((hash << 5) + hash) + c; //hash << 5 сдвигает все биты числа hash на 5 позиций влево. Это тоже самое, что умножить на 2 в пятой степени
|
printf("Celkova kapacita: %d\n", count_capacity(s));
|
||||||
}
|
|
||||||
|
|
||||||
return (int)(hash % s->track_count);
|
printf("Presov ma kapacitu: %d\n", get_target_capacity(s, "Presov"));
|
||||||
}
|
printf("Kosice ma kapacitu: %d\n", get_target_capacity(s, "Kosice"));
|
||||||
|
printf("Bratislava ma kapacitu: %d\n", get_target_capacity(s, "Bratislava"));
|
||||||
void add_target_capacity(struct station* s, const char* target, int capacity) {
|
printf("Zvolen ma kapacitu: %d\n", get_target_capacity(s, "Zvolen"));
|
||||||
if (!s || !target) return;
|
|
||||||
|
|
||||||
int index = select_track(s, target);
|
|
||||||
struct car* current = s->tracks[index];
|
|
||||||
|
|
||||||
if (current == NULL) { //Этот кусок кода создаёт “поезд” на пустой колее.
|
|
||||||
struct car* new_car = calloc(1, sizeof(struct car));
|
|
||||||
strcpy(new_car->value, target);
|
|
||||||
new_car->capacity = capacity;
|
|
||||||
new_car->next = NULL;
|
|
||||||
s->tracks[index] = new_car;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct car* prev = NULL; //создаём указатель prev, который будет хранить предыдущий элемент списка
|
|
||||||
while (current != NULL) {
|
|
||||||
if (strcmp(current->value, target) == 0) {
|
|
||||||
current->capacity += capacity;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
prev = current;
|
|
||||||
current = current->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct car* new_car = calloc(1, sizeof(struct car));
|
|
||||||
strcpy(new_car->value, target);
|
|
||||||
new_car->capacity = capacity;
|
|
||||||
new_car->next = NULL;
|
|
||||||
prev->next = new_car;
|
|
||||||
}
|
|
||||||
|
|
||||||
int get_target_capacity(struct station* s, const char* target) {
|
|
||||||
if (!s || !target) return 0;
|
|
||||||
|
|
||||||
int index = select_track(s, target);
|
|
||||||
struct car* current = s->tracks[index];
|
|
||||||
|
|
||||||
while (current != NULL) {
|
|
||||||
if (strcmp(current->value, target) == 0) {
|
|
||||||
return current->capacity;
|
|
||||||
}
|
|
||||||
current = current->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
destroy_station(s);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int count_targets(struct station* s) {
|
|
||||||
if (!s) return 0;
|
|
||||||
|
|
||||||
int count = 0;
|
|
||||||
for (int i = 0; i < s->track_count; i++) {
|
|
||||||
struct car* current = s->tracks[i];
|
|
||||||
while (current != NULL) {
|
|
||||||
count++;
|
|
||||||
current = current->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
|
|
||||||
int count_capacity(struct station* s) {
|
|
||||||
if (!s) return 0;
|
|
||||||
|
|
||||||
int total = 0;
|
|
||||||
for (int i = 0; i < s->track_count; i++) {
|
|
||||||
struct car* current = s->tracks[i];
|
|
||||||
while (current != NULL) {
|
|
||||||
total += current->capacity;
|
|
||||||
current = current->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return total;
|
|
||||||
}
|
|
||||||
|
|
||||||
void destroy_station(struct station* s) {
|
|
||||||
if (!s) return;
|
|
||||||
|
|
||||||
for (int i = 0; i < s->track_count; i++) {
|
|
||||||
struct car* current = s->tracks[i];
|
|
||||||
while (current != NULL) {
|
|
||||||
struct car* to_delete = current;
|
|
||||||
current = current->next;
|
|
||||||
free(to_delete);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
free(s->tracks);
|
|
||||||
free(s);
|
|
||||||
}
|
|
||||||
|
|||||||
120
du5/a_station.c
120
du5/a_station.c
@ -1,27 +1,111 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include "a_station.h"
|
#include "a_station.h"
|
||||||
|
|
||||||
int main() {
|
struct station* create_station() {
|
||||||
struct station* s = create_station();
|
struct station* s = calloc(1, sizeof(struct station));
|
||||||
|
s->tracks = calloc(STATION_SIZE, sizeof(struct car*));
|
||||||
|
s->track_count = STATION_SIZE;
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
add_target_capacity(s, "Kosice", 120);
|
int select_track(struct station* s, const char* target) {
|
||||||
add_target_capacity(s, "Presov", 60);
|
unsigned long hash = 5381; //5381 это алгоритм DJB2
|
||||||
add_target_capacity(s, "Kosice", 30);
|
int c;
|
||||||
add_target_capacity(s, "Bratislava", 200);
|
|
||||||
add_target_capacity(s, "Zilina", 90);
|
|
||||||
|
|
||||||
print_station(s);
|
while ((c = *target++)) { // не забываем ++ чтобы идти по строке
|
||||||
|
hash = ((hash << 5) + hash) + c; //hash << 5 сдвигает все биты числа hash на 5 позиций влево. Это тоже самое, что умножить на 2 в пятой степени
|
||||||
struct car* found = find_target(s, "Presov");
|
|
||||||
if (found) {
|
|
||||||
printf("Nájdená stanica: %s, kapacita: %d\n", found->value, found->capacity);
|
|
||||||
} else {
|
|
||||||
printf("Stanica 'Presov' sa nenašla.\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Počet staníc: %d\n", get_total_targets(s));
|
return (int)(hash % s->track_count);
|
||||||
printf("Celkova kapacita: %d\n", get_total_capacity(s));
|
}
|
||||||
|
|
||||||
|
void add_target_capacity(struct station* s, const char* target, int capacity) {
|
||||||
|
if (!s || !target) return;
|
||||||
|
|
||||||
|
int index = select_track(s, target);
|
||||||
|
struct car* current = s->tracks[index];
|
||||||
|
|
||||||
|
if (current == NULL) { //Этот кусок кода создаёт “поезд” на пустой колее.
|
||||||
|
struct car* new_car = calloc(1, sizeof(struct car));
|
||||||
|
strcpy(new_car->value, target);
|
||||||
|
new_car->capacity = capacity;
|
||||||
|
new_car->next = NULL;
|
||||||
|
s->tracks[index] = new_car;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct car* prev = NULL; //создаём указатель prev, который будет хранить предыдущий элемент списка
|
||||||
|
while (current != NULL) {
|
||||||
|
if (strcmp(current->value, target) == 0) {
|
||||||
|
current->capacity += capacity;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
prev = current;
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct car* new_car = calloc(1, sizeof(struct car));
|
||||||
|
strcpy(new_car->value, target);
|
||||||
|
new_car->capacity = capacity;
|
||||||
|
new_car->next = NULL;
|
||||||
|
prev->next = new_car;
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_target_capacity(struct station* s, const char* target) {
|
||||||
|
if (!s || !target) return 0;
|
||||||
|
|
||||||
|
int index = select_track(s, target);
|
||||||
|
struct car* current = s->tracks[index];
|
||||||
|
|
||||||
|
while (current != NULL) {
|
||||||
|
if (strcmp(current->value, target) == 0) {
|
||||||
|
return current->capacity;
|
||||||
|
}
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
|
||||||
destroy_station(s);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int count_targets(struct station* s) {
|
||||||
|
if (!s) return 0;
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
for (int i = 0; i < s->track_count; i++) {
|
||||||
|
struct car* current = s->tracks[i];
|
||||||
|
while (current != NULL) {
|
||||||
|
count++;
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
int count_capacity(struct station* s) {
|
||||||
|
if (!s) return 0;
|
||||||
|
|
||||||
|
int total = 0;
|
||||||
|
for (int i = 0; i < s->track_count; i++) {
|
||||||
|
struct car* current = s->tracks[i];
|
||||||
|
while (current != NULL) {
|
||||||
|
total += current->capacity;
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
void destroy_station(struct station* s) {
|
||||||
|
if (!s) return;
|
||||||
|
|
||||||
|
for (int i = 0; i < s->track_count; i++) {
|
||||||
|
struct car* current = s->tracks[i];
|
||||||
|
while (current != NULL) {
|
||||||
|
struct car* to_delete = current;
|
||||||
|
current = current->next;
|
||||||
|
free(to_delete);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(s->tracks);
|
||||||
|
free(s);
|
||||||
|
}
|
||||||
|
|||||||
@ -1,16 +1,16 @@
|
|||||||
#ifndef A_STATION_H
|
#ifndef STATION_H
|
||||||
#define A_STATION_H
|
#define STATION_H
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define SIZE 100
|
|
||||||
#define STATION_SIZE 10
|
#define STATION_SIZE 10
|
||||||
|
#define TARGET_SIZE 36
|
||||||
|
|
||||||
struct car {
|
struct car {
|
||||||
|
char value[TARGET_SIZE];
|
||||||
int capacity;
|
int capacity;
|
||||||
char value[SIZE];
|
|
||||||
struct car* next;
|
struct car* next;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -20,12 +20,11 @@ struct station {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct station* create_station();
|
struct station* create_station();
|
||||||
void destroy_station(struct station* s);
|
void destroy_station(struct station* station);
|
||||||
int select_track(struct station* s, const char* target);
|
int select_track(struct station* station, const char* target);
|
||||||
void add_target_capacity(struct station* s, const char* target, int capacity);
|
void add_target_capacity(struct station* station, const char* target, int capacity);
|
||||||
struct car* find_target(struct station* s, const char* target);
|
int get_target_capacity(struct station* station, const char* target);
|
||||||
int get_total_targets(struct station* s);
|
int count_targets(struct station* station);
|
||||||
int get_total_capacity(struct station* s);
|
int count_capacity(struct station* station);
|
||||||
void print_station(struct station* s);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user