a4
This commit is contained in:
		
							parent
							
								
									ed50e09388
								
							
						
					
					
						commit
						a4f237c52b
					
				
							
								
								
									
										0
									
								
								a4/program.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								a4/program.c
									
									
									
									
									
										Normal file
									
								
							| @ -3,74 +3,74 @@ | ||||
| #include <stdlib.h> | ||||
| #include <string.h> | ||||
| 
 | ||||
| // Pridá nový vozeň na koniec vlaku
 | ||||
| struct car* add_car(struct car* first, const char* target) { | ||||
|     struct car* new_car = (struct car*)malloc(sizeof(struct car)); | ||||
|     if (new_car == NULL) { | ||||
|     struct car* new_car = (struct car*)malloc(sizeof(struct car));  // Alokuje pamäť pre nový vozeň
 | ||||
|     if (new_car == NULL) {                                         // Skontroluje, či sa pamäť podarilo alokovať
 | ||||
|         printf("Chyba: Nepodarilo sa alokovať pamäť pre nový vozeň.\n"); | ||||
|         return first; | ||||
|         return first;                                              // Ak nie, vráti pôvodný zoznam
 | ||||
|     } | ||||
|     strncpy(new_car->value, target, SIZE); | ||||
|     new_car->next = NULL; | ||||
|     strncpy(new_car->value, target, SIZE);                         // Nastaví cieľovú stanicu vozňa
 | ||||
|     new_car->next = NULL;                                          // Nový vozeň bude posledný v zozname
 | ||||
| 
 | ||||
|     if (first == NULL) { | ||||
|         return new_car; | ||||
|     if (first == NULL) {                                           // Ak je vlak prázdny
 | ||||
|         return new_car;                                            // Nový vozeň sa stane prvým
 | ||||
|     } | ||||
| 
 | ||||
|     struct car* temp = first; | ||||
|     while (temp->next != NULL) { | ||||
|     struct car* temp = first;                                      // Začne na začiatku vlaku
 | ||||
|     while (temp->next != NULL) {                                   // Prejde na koniec zoznamu
 | ||||
|         temp = temp->next; | ||||
|     } | ||||
|     temp->next = new_car; | ||||
|     temp->next = new_car;                                          // Pridá nový vozeň na koniec
 | ||||
| 
 | ||||
|     return first; | ||||
|     return first;                                                  // Vráti začiatok zoznamu
 | ||||
| } | ||||
| 
 | ||||
| // Vytlačí všetky vozne vo vlaku
 | ||||
| void print_train(struct car* first) { | ||||
|     if (first == NULL) { | ||||
|     if (first == NULL) {                                           // Ak je vlak prázdny
 | ||||
|         printf("Vlak je prazdny.\n"); | ||||
|         return; | ||||
|         return;                                                    // Ukončí funkciu
 | ||||
|     } | ||||
| 
 | ||||
|     struct car* temp = first; | ||||
|     while (temp != NULL) { | ||||
|         printf("Cielova stanica: %s\n", temp->value); | ||||
|         temp = temp->next; | ||||
|     struct car* temp = first;                                      // Začne na začiatku vlaku
 | ||||
|     while (temp != NULL) {                                         // Prechádza cez všetky vozne
 | ||||
|         printf("Cielova stanica: %s\n", temp->value);              // Vytlačí cieľovú stanicu vozňa
 | ||||
|         temp = temp->next;                                         // Prejde na ďalší vozeň
 | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| // Zruší všetky vozne vo vlaku
 | ||||
| void cancel_train(struct car* first) { | ||||
|     struct car* temp; | ||||
|     while (first != NULL) { | ||||
|         temp = first; | ||||
|         first = first->next; | ||||
|         free(temp); | ||||
|     while (first != NULL) {                                        // Prechádza cez všetky vozne
 | ||||
|         temp = first;                                              // Dočasne uloží aktuálny vozeň
 | ||||
|         first = first->next;                                       // Prejde na ďalší vozeň
 | ||||
|         free(temp);                                                // Uvoľní pamäť aktuálneho vozňa
 | ||||
|     } | ||||
|     printf("Vsetky vozne boli zrusene.\n"); | ||||
| } | ||||
| 
 | ||||
| // Odstráni všetky vozne s danou cieľovou stanicou
 | ||||
| struct car* clear_train(struct car* first, const char* target) { | ||||
|     struct car* current = first; | ||||
|     struct car* previous = NULL; | ||||
|     struct car* current = first;                                   // Začne na začiatku vlaku
 | ||||
|     struct car* previous = NULL;                                  // Uchováva predchádzajúci vozeň
 | ||||
| 
 | ||||
|     while (current != NULL) { | ||||
|         // Porovnanie hodnoty cielovej stanice
 | ||||
|         if (strcmp(current->value, target) == 0) { | ||||
|             // Ak sa cielova stanica zhoduje, odstráni vozeň
 | ||||
|             if (previous == NULL) { | ||||
|                 // Ak je to prvý vozeň
 | ||||
|                 first = current->next; | ||||
|     while (current != NULL) {                                     // Prechádza cez všetky vozne
 | ||||
|         if (strcmp(current->value, target) == 0) {                // Ak sa cieľová stanica zhoduje
 | ||||
|             if (previous == NULL) {                               // Ak je to prvý vozeň
 | ||||
|                 first = current->next;                            // Aktualizuje začiatok zoznamu
 | ||||
|             } else { | ||||
|                 previous->next = current->next; | ||||
|                 previous->next = current->next;                   // Preskočí aktuálny vozeň
 | ||||
|             } | ||||
|             struct car* temp = current; | ||||
|             current = current->next; | ||||
|             free(temp); | ||||
|             struct car* temp = current;                           // Dočasne uloží aktuálny vozeň
 | ||||
|             current = current->next;                              // Prejde na ďalší vozeň
 | ||||
|             free(temp);                                           // Uvoľní pamäť aktuálneho vozňa
 | ||||
|         } else { | ||||
|             // Ak sa nezhoduje, prejde na nasledujúci vozeň
 | ||||
|             previous = current; | ||||
|             current = current->next; | ||||
|             previous = current;                                   // Aktualizuje predchádzajúci vozeň
 | ||||
|             current = current->next;                              // Prejde na ďalší vozeň
 | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     return first; | ||||
|     return first;                                                 // Vráti začiatok zoznamu
 | ||||
| } | ||||
|  | ||||
							
								
								
									
										118
									
								
								cv6/a_station.c
									
									
									
									
									
								
							
							
						
						
									
										118
									
								
								cv6/a_station.c
									
									
									
									
									
								
							| @ -2,80 +2,86 @@ | ||||
| #include <stdlib.h> | ||||
| #include <string.h> | ||||
| 
 | ||||
| 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; | ||||
| // Vytvorí štruktúru stanice
 | ||||
| struct station* create_station() { | ||||
|     struct station* station = (struct station*)calloc(1, sizeof(struct station)); // Alokuje pamäť pre stanicu
 | ||||
|     station->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*));    // Alokuje pamäť pre pole koľají
 | ||||
|     station->track_count = STATION_SIZE;                                         // Nastaví počet koľají
 | ||||
|     return station;                                                              // Vráti ukazovateľ na stanicu
 | ||||
| } | ||||
| 
 | ||||
| void destroy_station(struct station* station){ | ||||
| // Zničí štruktúru stanice (zatiaľ prázdna implementácia)
 | ||||
| void destroy_station(struct station* station) { | ||||
|     // Tu by malo byť uvoľnenie pamäte pre všetky objekty a pole
 | ||||
| } | ||||
| 
 | ||||
| int select_track(struct station* station, const char* target){ | ||||
|     unsigned long hash = 5381; | ||||
|         int c; | ||||
|         while ((c = *target++)) { | ||||
|             hash = ((hash << 5) + hash) + c;  // hash * 33 + c
 | ||||
| // Vypočíta index koľaje pre daný cieľ pomocou hashovacej funkcie
 | ||||
| int select_track(struct station* station, const char* target) { | ||||
|     unsigned long hash = 5381;  // Inicializuje hash hodnotu
 | ||||
|     int c; | ||||
|     while ((c = *target++)) {   // Prechádza každým znakom reťazca
 | ||||
|         hash = ((hash << 5) + hash) + c;  // hash * 33 + c
 | ||||
|     } | ||||
|     return hash % station->track_count;  // Vráti index v rozsahu počtu koľají
 | ||||
| } | ||||
| 
 | ||||
| // Pridá kapacitu cieľu alebo vytvorí nový cieľ
 | ||||
| void add_target_capacity(struct station* station, const char* target, int capacity) { | ||||
|     int index = select_track(station, target);              // Získa index koľaje pre cieľ
 | ||||
|     struct car* current = station->tracks[index];           // Získa zoznam cieľov na danej koľaji
 | ||||
| 
 | ||||
|     while (current) {                                       // Prechádza zoznam cieľov
 | ||||
|         if (strcmp(current->value, target) == 0) {          // Ak cieľ už existuje
 | ||||
|             current->capacity += capacity;                 // Zvýši jeho kapacitu
 | ||||
|             return;                                         // Ukončí funkciu
 | ||||
|         } | ||||
|     return hash % 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]; | ||||
|      | ||||
|     while (current) { | ||||
|         if (strcmp(current->value, target) == 0) { | ||||
|             current->capacity += capacity;  // Pridá kapacitu
 | ||||
|             return; | ||||
|         } | ||||
|         current = current->next; | ||||
|         current = current->next;                           // Pokračuje na ďalší prvok v zozname
 | ||||
|     } | ||||
| 
 | ||||
|      | ||||
|     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 = station->tracks[index]; | ||||
|     station->tracks[index] = new_car; | ||||
|     struct car* new_car = (struct car*)malloc(sizeof(struct car));  // Alokuje pamäť pre nový cieľ
 | ||||
|     strncpy(new_car->value, target, TARGET_SIZE - 1);              // Nastaví názov cieľa
 | ||||
|     new_car->value[TARGET_SIZE - 1] = '\0';                        // Ukončí reťazec nulou
 | ||||
|     new_car->capacity = capacity;                                 // Nastaví kapacitu cieľa
 | ||||
|     new_car->next = station->tracks[index];                       // Pridá cieľ na začiatok zoznamu
 | ||||
|     station->tracks[index] = new_car;                             // Uloží nový cieľ do koľaje
 | ||||
| } | ||||
| 
 | ||||
| int get_target_capacity(struct station* station,const char* target){ | ||||
| int index = select_track(station, target); | ||||
|     struct car* current = station->tracks[index]; | ||||
| // Vráti kapacitu cieľa podľa názvu
 | ||||
| int get_target_capacity(struct station* station, const char* target) { | ||||
|     int index = select_track(station, target);            // Získa index koľaje pre cieľ
 | ||||
|     struct car* current = station->tracks[index];         // Získa zoznam cieľov na danej koľaji
 | ||||
| 
 | ||||
|     while (current) { | ||||
|         if (strcmp(current->value, target) == 0) { | ||||
|             return current->capacity; | ||||
|     while (current) {                                     // Prechádza zoznam cieľov
 | ||||
|         if (strcmp(current->value, target) == 0) {        // Ak nájde cieľ
 | ||||
|             return current->capacity;                    // Vráti jeho kapacitu
 | ||||
|         } | ||||
|         current = current->next; | ||||
|         current = current->next;                         // Pokračuje na ďalší prvok
 | ||||
|     } | ||||
|     return 0;   | ||||
|     return 0;                                            // Ak cieľ neexistuje, vráti 0
 | ||||
| } | ||||
| 
 | ||||
| int count_targets(struct station* station){ | ||||
|    int count = 0; | ||||
|     for (int i = 0; i < station->track_count; ++i) { | ||||
|         struct car* current = station->tracks[i]; | ||||
|         while (current) { | ||||
|             ++count; | ||||
|             current = current->next; | ||||
| // Počíta počet cieľov vo všetkých koľajach
 | ||||
| int count_targets(struct station* station) { | ||||
|     int count = 0;                                       // Inicializuje počet na 0
 | ||||
|     for (int i = 0; i < station->track_count; ++i) {     // Prechádza všetky koľaje
 | ||||
|         struct car* current = station->tracks[i];        // Získa zoznam cieľov na koľaji
 | ||||
|         while (current) {                                // Prechádza každý cieľ
 | ||||
|             ++count;                                     // Zvyšuje počet
 | ||||
|             current = current->next;                    // Pokračuje na ďalší prvok
 | ||||
|         } | ||||
|     } | ||||
|     return count; | ||||
|     return count;                                       // Vráti celkový počet cieľov
 | ||||
| } | ||||
| 
 | ||||
| int count_capacity(struct station* station){ | ||||
|     int total_capacity = 0; | ||||
|     for (int i = 0; i < station->track_count; ++i) { | ||||
|         struct car* current = station->tracks[i]; | ||||
|         while (current) { | ||||
|             total_capacity += current->capacity; | ||||
|             current = current->next; | ||||
| // Počíta celkovú kapacitu vo všetkých koľajach
 | ||||
| int count_capacity(struct station* station) { | ||||
|     int total_capacity = 0;                             // Inicializuje celkovú kapacitu na 0
 | ||||
|     for (int i = 0; i < station->track_count; ++i) {    // Prechádza všetky koľaje
 | ||||
|         struct car* current = station->tracks[i];       // Získa zoznam cieľov na koľaji
 | ||||
|         while (current) {                               // Prechádza každý cieľ
 | ||||
|             total_capacity += current->capacity;        // Pridá jeho kapacitu
 | ||||
|             current = current->next;                   // Pokračuje na ďalší prvok
 | ||||
|         } | ||||
|     } | ||||
|     return total_capacity; | ||||
|     return total_capacity;                             // Vráti celkovú kapacitu
 | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user