From 6e200a1ac94a8b00235343b5e9b4eafcb825f13e Mon Sep 17 00:00:00 2001 From: Oleksandr Hryshchenko Date: Thu, 18 Nov 2021 22:59:25 +0000 Subject: [PATCH 1/3] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB(?= =?UTF-8?q?=D0=B0)=20=D0=BD=D0=B0=20'cv7/a=5Fstation.c'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cv7/a_station.c | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/cv7/a_station.c b/cv7/a_station.c index 660e98b..7af372e 100644 --- a/cv7/a_station.c +++ b/cv7/a_station.c @@ -1,6 +1,40 @@ -#include +#include "a_station.h" +#include +#include -int main(){ - printf("Helo world\n"); +//#define SIZE 100; + +struct station* create_station(){ + struct station* station = (struct station*)calloc(1,sizeof(struct station)); + station->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*)); + station->track_count = STATION_SIZE; + return station; +} + +void destroy_station(struct station* station){ + +} + +int select_track(struct station* station, const char* target){ + return 0; } + +void add_target_capacity(struct station* station,const char* target, int capacity){ +} + +int get_target_capacity(struct station* station,const char* target){ + + return 0; +} + +int count_targets(struct station* station){ + + + return 0; +} + +int count_capacity(struct station* station){ + + return 0; +} \ No newline at end of file From 298918db19d1c04991aa84ba17955dc2655f8ef5 Mon Sep 17 00:00:00 2001 From: Oleksandr Hryshchenko Date: Fri, 19 Nov 2021 00:26:44 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB(?= =?UTF-8?q?=D0=B0)=20=D0=BD=D0=B0=20'cv7/a=5Fstation.c'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cv7/a_station.c | 71 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 17 deletions(-) diff --git a/cv7/a_station.c b/cv7/a_station.c index 7af372e..6bf7439 100644 --- a/cv7/a_station.c +++ b/cv7/a_station.c @@ -2,39 +2,76 @@ #include #include -//#define SIZE 100; - struct station* create_station(){ - struct station* station = (struct station*)calloc(1,sizeof(struct station)); - station->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*)); - station->track_count = STATION_SIZE; - return station; + struct station* station = (struct station*)calloc(1,sizeof(struct station)); + station->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*)); + station->track_count = STATION_SIZE; + return station; } void destroy_station(struct station* station){ - + free(station->tracks); + free(station); } int select_track(struct station* station, const char* target){ - - return 0; + if(!station || station->track_count == 0) + return 0; + + int hash_result = (int)strlen(target) % station->track_count; + struct car* current = station->tracks[hash_result]; + + while(current != NULL && abs(strcmp(station->tracks[hash_result]->value, target))){ + hash_result = (hash_result * 2 + 1) % station->track_count; + current = station->tracks[hash_result]; + } + + return hash_result; } void add_target_capacity(struct station* station,const char* target, int capacity){ + if(!station || station->track_count == 0) + return; + + int myTrack = select_track(station, target); + + struct car* uniqueCar = station->tracks[myTrack]; + if(!uniqueCar) { + uniqueCar = (struct car *) calloc(1, sizeof(struct car)); + uniqueCar->capacity = 0; + strcpy(uniqueCar->value, target); + uniqueCar->next = NULL; + } + uniqueCar->capacity += capacity; + station->tracks[myTrack] = uniqueCar; } int get_target_capacity(struct station* station,const char* target){ - - return 0; + if(!station || station->track_count == 0 || station->tracks[select_track(station, target)] == NULL) + return 0; + + return station->tracks[select_track(station, target)]->capacity; } int count_targets(struct station* station){ - - - return 0; + if(!station || station->track_count == 0) + return 0; + + int numberOfTargets = 0; + for(int i = 0; i < station->track_count; i++) + numberOfTargets += (station->tracks[i] == NULL) ? 0 : 1; + + return numberOfTargets; } int count_capacity(struct station* station){ - - return 0; -} \ No newline at end of file + if(!station || station->track_count == 0) + return 0; + + int totalCapacity = 0; + for(int i = 0; i < station->track_count; i++) + totalCapacity += (station->tracks[i] == NULL) ? 0 : station->tracks[i]->capacity; + + return totalCapacity; +} + From 8da21f183931ca8d43612c8dca4823488cb6b073 Mon Sep 17 00:00:00 2001 From: Oleksandr Hryshchenko Date: Mon, 22 Nov 2021 16:46:40 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BB(=D0=B0?= =?UTF-8?q?)=20'a3/program.c'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- a3/program.c | 188 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 a3/program.c diff --git a/a3/program.c b/a3/program.c new file mode 100644 index 0000000..6676795 --- /dev/null +++ b/a3/program.c @@ -0,0 +1,188 @@ +// Max binárna kopa +#include +#include +#include + +// Ľubovoľné (dynamické) pole môže byť binárna kopa +struct heap { + int* array; + int size; + int capacity; +}; + +// Výpočet rodičovského uzla +int parent(int i){ + return (i -1) / 2; +} + +// Výpočeť ľavého syna +int left_child(int i){ + return (2*i) + 1; +} + +// Výpočet pravého syna +int right_child(int i){ + return (2*i) + 2; +} + +// Výmena dvoch prvkov v poli +void swap(int* array,int a, int b){ + int c = array[a]; + array[a] = array[b]; + array[b] = c; +} + +// Test kopovitosti +// Pozeráme, či je každý potomok menší ako jeho rodič +// Funkcia zlyhá, ak nájdeme aspoň jedného potomka, väčšieho alebo rovného ako je rodič +void check_heap_property(struct heap* h){ + for (int i = 0; i < h->size / 2 ; i++){ + int c = left_child(i); + if (c >= h->size){ + break; + } + assert(h->array[c] >= h->array[i]); + c = right_child(i); + if (c >= h->size){ + break; + } + assert(h->array[c] >= h->array[i]); + } +} +// +// Vytvorenie prázdnej kopy +struct heap* create_heap(int capacity){ + struct heap* h = malloc(sizeof(struct heap)); + h->array = calloc(capacity,sizeof(int)); + h->capacity = capacity; + h->size = 0; + return h; +} + +// Zmazanie ľubovoľnej kopy +void delete_heap(struct heap* h){ + assert(h); + free(h->array); + free(h); +} + +// Recursive heapification +/** + * Predpokladáme, že ľavý a pravý podstrom prvku i sú kopy. + * + * Výsledkom je, že strom s vrcholom i je kopa. + * + * @param pole + * @param velkost pola + * @param Koren stromu, ktorý sa má upraviť + */ +void heapify2(int* array,int size,int i){ + // Zoberiem i-ty prvok + int largest = i; + int l = left_child(i); + int r = right_child(i); + // Pozrieme jeho hodnotu a hodnoty jeho potomkov + // Zistíme kde v tejto trojici sa nachádza najväčšia hodnota + if (l < size && array[largest] < array[l]){ + largest = l; + } + if (r < size && array[largest] < array[r]){ + largest = r; + } + // Ak je najväčšia hodnota v potomkovi + if (largest != i){ + // Vymeníme ich hodnoty + int v = array[i]; + array[i] = array[largest]; + array[largest] = v; + // Obnovíme kopovitosť v potomkovi + heapify2(array,size, largest); + } +} + + +/////////////////////////////////////////// +void heapify_array(int* array, int size){ + // Začíname od predposlednej úrovne + // sme si istí, že všetky prvky na poslednej + // úrovni sú binárne kopy (lebo nemajú potomkov) + // Postupne tak obnovíme kopovitosť celého poľa + for (int i = size / 2; i >= 0; i-- ){ + heapify2(array,size,i); + } +} +//////////////////////////////////////////// + + + +int remove_max(struct heap* h){ + assert(h->size > 0); + int i = 0; + int res = h->array[i]; + // Vymeníme prvý a posledný prvok a skrátime pole + h->array[i] = h->array[h->size -1]; + h->size -= 1; + // Obnonovíme kopovitosť + heapify2(h->array,h->size,0); + return res; +} + + + +// Pridanie do kopy +void add(struct heap* h,int value){ + // Pridáme prvok na koniec poľa + int i = h->size - 1; + h->size += 1; + assert(h->size <= h->capacity); + h->array[i] = value; + // Pozrieme na jeho rodiča + int p = parent(i); + // Pokiaľ rodič existuje a jeho hodnota je menšia + while(p >= 0 && h->array[p] < h->array[i]){ + // Vymeníme ich hodnoty + int v = h->array[p]; + h->array[p] = h->array[i]; + h->array[i] = v; + // Pokračujeme ďalším rodičom + i = p; + p = parent(p); + } +} + +/*** + * Z kopy vytvorí zotriedené pole + */ +void heap_sort(int* heap,int size){ + // Na začiatku je najväčší prvok + for (int i = size -1; i >= 0; i -= 2){ + // Vymenime prvy a posledny prvok + int v = heap[0]; + heap[0] = heap[i]; + heap[i] = v; + // Na konci je najväčší prvok. + // Uvažujeme, že kopa je o jedno menšia + // Obnovíme kopovitosť + heapify2(heap,i,0); + } +} + +int main(){ + struct heap* kopa = create_heap(10); + add(kopa,10); + add(kopa,20); + add(kopa,40); + add(kopa,15); + for (int i = 0; i < kopa->size; i++){ + printf("%d ",kopa->array[i]); + } + printf("\n"); + int pole[14] = {2,3,1,4,5,3,5,6,7,4,7,4,3,3}; + int sz = 14; + heapify_array(pole,sz); + for (int i = 0; i < sz; i++){ + printf("%d ",pole[i]); + } + printf("\n"); + return 0; +} \ No newline at end of file