From 35433ace5517bb5dea22d46fec87b84e0eb7ac06 Mon Sep 17 00:00:00 2001 From: Miloslav Macko Date: Mon, 15 Apr 2024 22:03:45 +0200 Subject: [PATCH] 1111111111 --- cv10/program.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/cv10/program.c b/cv10/program.c index 01d53be..4ea2bca 100644 --- a/cv10/program.c +++ b/cv10/program.c @@ -5,8 +5,8 @@ #define SIZE 100 int compare(const void* arg1, const void* arg2) { - char* s1 = *((char**)arg1); - char* s2 = *((char**)arg2); + const char* s1 = *((const char**)arg1); + const char* s2 = *((const char**)arg2); return strcmp(s1, s2); } @@ -21,17 +21,25 @@ int main() { if (pocet_znakov == 1 && line[0] == '\n') // Skončiť na prázdnom riadku break; + // Odstrániť koniec riadka + if (line[pocet_znakov - 1] == '\n') + line[--pocet_znakov] = '\0'; + // Kontrola duplicít int found = 0; for (int i = 0; i < pocet_mien_v_poli; i++) { - if (memcmp(pole_smernikov[i], line, pocet_znakov) == 0) { + if (strcmp(pole_smernikov[i], line) == 0) { found = 1; break; } } - if (!found) { - pole_smernikov[pocet_mien_v_poli] = malloc(pocet_znakov); - memcpy(pole_smernikov[pocet_mien_v_poli], line, pocet_znakov); + if (!found && pocet_mien_v_poli < SIZE) { + pole_smernikov[pocet_mien_v_poli] = malloc(pocet_znakov + 1); + if (pole_smernikov[pocet_mien_v_poli] == NULL) { + fprintf(stderr, "Chyba pri alokácii pamäte\n"); + exit(EXIT_FAILURE); + } + strcpy(pole_smernikov[pocet_mien_v_poli], line); pocet_mien_v_poli += 1; } } @@ -45,12 +53,12 @@ int main() { printf("Prijati studenti:\n"); for (int i = 0; i < max_students && i < pocet_mien_v_poli; i++) { - printf("%s", pole_smernikov[i]); + printf("%s\n", pole_smernikov[i]); } printf("Neprijati studenti:\n"); for (int i = max_students; i < pocet_mien_v_poli; i++) { - printf("%s", pole_smernikov[i]); + printf("%s\n", pole_smernikov[i]); } // Uvoľnenie pamäte