From 651bd07061200288e6402e1abf5e81ad21ce58e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1=C5=A1?= Date: Thu, 18 Apr 2024 12:35:06 +0000 Subject: [PATCH] Update 'cv10/program.c' --- cv10/program.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/cv10/program.c b/cv10/program.c index e69de29..9a565af 100644 --- a/cv10/program.c +++ b/cv10/program.c @@ -0,0 +1,90 @@ +#include +#include +#include + +#define MAX_NAME_LENGTH 100 + +int compare(const void *a, const void *b) { + const char *name1 = *(const char **)a; + const char *name2 = *(const char **)b; + return strcmp(name1, name2); +} + +int main() { + int maxStudents; + char name[MAX_NAME_LENGTH]; + char **names = NULL; + int count = 0; + int capacity = 0; + + + if (scanf("%d\n", &maxStudents) != 1 || maxStudents <= 0) { + puts("Nespravny vstup"); + return 1; + } + + + while (fgets(name, MAX_NAME_LENGTH, stdin)) { + if (name[0] == '\n') break; + name[strcspn(name, "\n")] = '\0'; + + + if (count >= capacity) { + capacity = capacity == 0 ? 4 : capacity * 2; + names = realloc(names, capacity * sizeof(char *)); + if (names == NULL) { + perror("Failed to reallocate memory"); + return 1; + } + } + + + int isDuplicate = 0; + for (int i = 0; i < count; i++) { + if (strcmp(names[i], name) == 0) { + isDuplicate = 1; + break; + } + } + + if (!isDuplicate) { + names[count] = strdup(name); + if (names[count] == NULL) { + perror("Failed to duplicate name"); + return 1; + } + count++; + } + } + + if (count == 0) { + puts("Ziadne prihlasky"); + return 1; + } + + + qsort(names, count, sizeof(char *), compare); + + + puts("Prijati studenti:"); + int limit = count < maxStudents ? count : maxStudents; + for (int i = 0; i < limit; i++) { + puts(names[i]); + } + + + if (count > maxStudents) { + puts("Neprijati studenti:"); + for (int i = maxStudents; i < count; i++) { + puts(names[i]); + } + } + + + for (int i = 0; i < count; i++) { + free(names[i]); + } + free(names); + + return 0; +}