From f2064443b2f31bab6df4748429e35c6fe2bfc229 Mon Sep 17 00:00:00 2001 From: Miloslav Macko Date: Mon, 15 Apr 2024 22:09:20 +0200 Subject: [PATCH] 123 --- cv10/program.c | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/cv10/program.c b/cv10/program.c index 44359fd..7bb14ad 100644 --- a/cv10/program.c +++ b/cv10/program.c @@ -5,24 +5,22 @@ #define SIZE 100 int compare(const void* arg1, const void* arg2) { - const char* s1 = *((const char**)arg1); - const char* s2 = *((const char**)arg2); + char* s1 = *((char**)arg1); + char* s2 = *((char**)arg2); return strcmp(s1, s2); } int main() { - char* pole_smernikov[SIZE] = {NULL}; // Inicializujeme všetky smerníky na NULL + char* pole_smernikov[SIZE]; + memset(pole_smernikov, 0, SIZE * sizeof(char*)); int pocet_mien_v_poli = 0; char line[SIZE]; while (fgets(line, SIZE, stdin) != NULL) { - int pocet_znakov = strlen(line); - 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'; + line[strcspn(line, "\n")] = 0; // Remove the newline character + int pocet_znakov = strlen(line) + 1; // Include the null terminator + if (pocet_znakov == 1) // Skip empty lines + continue; // Kontrola duplicít int found = 0; @@ -32,12 +30,8 @@ int main() { break; } } - 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); - } + if (!found) { + pole_smernikov[pocet_mien_v_poli] = malloc(pocet_znakov); strcpy(pole_smernikov[pocet_mien_v_poli], line); pocet_mien_v_poli += 1; } @@ -48,10 +42,7 @@ int main() { // Výpis prijatých a neprijatých študentov int max_students; - if (scanf("%d", &max_students) != 1) { // Kontrola úspešnosti načítania - fprintf(stderr, "Chyba pri načítaní počtu študentov\n"); - exit(EXIT_FAILURE); - } + scanf("%d", &max_students); printf("Prijati studenti:\n"); for (int i = 0; i < max_students && i < pocet_mien_v_poli; i++) {