From a2e49258b7390a3275261507053947ebfd2b7805 Mon Sep 17 00:00:00 2001 From: ak643du Date: Thu, 25 Apr 2024 20:10:32 +0200 Subject: [PATCH] Initialization --- cv10/program.c | 76 ++++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 39 deletions(-) diff --git a/cv10/program.c b/cv10/program.c index de71e14..54b92b0 100644 --- a/cv10/program.c +++ b/cv10/program.c @@ -2,69 +2,67 @@ #include #include -int compare_names(const void *a, const void *b) { +// Funkcia na kontrolu abecedného poradia +int compare(const void *a, const void *b) { return strcmp(*(const char **)a, *(const char **)b); } int main() { - int count; + int max_students; + char **applications = NULL; char buffer[100]; - char **applications; - int i, j; + int num_applications = 0; - if (scanf("%d", &count) != 1 || count <= 0) { - printf("Nespravny vstup\n"); + // Načítanie počtu študentov na prijatie + if (scanf("%d", &max_students) != 1 || max_students <= 0) { + puts("Nespravny vstup"); return 1; } - applications = (char **)malloc(count * sizeof(char *)); - if (!applications) { - printf("Chyba pri alokacii pamate\n"); + // Dynamická alokácia pamäte pre zoznam prihlášok + applications = (char **)malloc(max_students * sizeof(char *)); + if (applications == NULL) { + puts("Chyba alokacie pamate"); return 1; } - i = 0; - while (i < count && fgets(buffer, sizeof(buffer), stdin)) { - int len = strlen(buffer); - if (len > 0 && buffer[len - 1] == '\n') { // Remove newline character if present - buffer[len - 1] = '\0'; - } - int found = 0; - for (j = 0; j < i; j++) { - if (strcmp(applications[j], buffer) == 0) { - found = 1; - break; - } - } - if (!found) { - applications[i] = strdup(buffer); - i++; + // Načítanie prihlášok + while (scanf("%s", buffer) == 1 && buffer[0] != '\0') { + applications[num_applications] = strdup(buffer); + if (applications[num_applications] == NULL) { + puts("Chyba alokacie pamate"); + return 1; } + num_applications++; } - if (i == 0) { - printf("Ziadne prihlasky\n"); + // Kontrola, či boli načítané nejaké prihlášky + if (num_applications == 0) { + puts("Ziadne prihlasky"); return 1; } - // Sort the applications alphabetically - qsort(applications, i, sizeof(char *), compare_names); + // Usporiadanie prihlášok podľa abecedy + qsort(applications, num_applications, sizeof(char *), compare); - printf("Prijati studenti:"); - for (j = 0; j < i; j++) { - printf("%s\n", applications[j]); + // Výpis prijatých študentov + puts("Prijati studenti:"); + int i; + for (i = 0; i < num_applications && i < max_students; i++) { + printf("%s\n", applications[i]); } - if (count < i) { - printf("Neprijati studenti:"); - for (; j < i; j++) { - printf("%s\n", applications[j]); + // Výpis neprijatých študentov + if (i < num_applications) { + puts("Neprijati studenti:"); + for (; i < num_applications; i++) { + printf("%s\n", applications[i]); } } - // Free allocated memory - for (j = 0; j < i; j++) { - free(applications[j]); + // Uvoľnenie pamäte + for (i = 0; i < num_applications; i++) { + free(applications[i]); } free(applications);