From 23dc469d6745c1ff095bbd6a348d4166f7e4dae0 Mon Sep 17 00:00:00 2001 From: ak643du Date: Thu, 25 Apr 2024 17:29:10 +0200 Subject: [PATCH] Initialization --- cv10/program.c | 99 ++++++++++++++++++++++++++++---------------------- 1 file changed, 55 insertions(+), 44 deletions(-) diff --git a/cv10/program.c b/cv10/program.c index fcc6775..f59d1a7 100644 --- a/cv10/program.c +++ b/cv10/program.c @@ -1,66 +1,77 @@ #include -#include #include +#include -#define MAX_STUDENTS 100 -#define NAME_SIZE 50 +typedef struct Student { + char name[100]; + int applied; +} Student; -int compareStrings(const void *a, const void *b) { - return strcmp((const char *)a, (const char *)b); +int cmpfunc(const void *a, const void *b) { + Student *student1 = (Student *)a; + Student *student2 = (Student *)b; + return strcmp(student1->name, student2->name); } int main() { - int max_students, num_accepted = 0, num_students = 0; - char names[MAX_STUDENTS][NAME_SIZE]; - char temp_name[NAME_SIZE]; + int max_students, num_students; + char name[100]; + Student students[1000]; + int students_accepted = 0; + int i, j; - scanf("%d", &max_students); - if (max_students <= 0 || max_students > MAX_STUDENTS) { - printf("Nespravny vstup\n"); + if (scanf("%d", &max_students) != 1 || max_students <= 0) { + puts("Nespravny vstup"); return 1; } - memset(names, 0, sizeof(names)); + if (scanf("%d", &num_students) != 1 || num_students <= 0) { + puts("Nespravny vstup"); + return 1; + } - while (fgets(temp_name, sizeof(temp_name), stdin) != NULL && num_accepted < max_students) { - int i, found = 0; + if (num_students > max_students) { + num_students = max_students; + } - // // Remove newline character if present - // temp_name[strcspn(temp_name, "\n")] = '\0'; + for (i = 0; i < num_students; i++) { + if (scanf("%s", name) != 1) { + puts("Ziadne prihlasky"); + return 1; + } - for (i = 0; i < num_accepted; i++) { - if (strcmp(names[i], temp_name) == 0) { - found = 1; + for (j = 0; j < i; j++) { + if (strcmp(students[j].name, name) == 0) { break; } } - if (!found) { - strcpy(names[num_accepted], temp_name); - num_accepted++; - } - num_students++; - } - - if (num_accepted == 0) { - printf("Ziadne prihlasky"); - } else { - qsort(names, num_accepted, sizeof(names[0]), compareStrings); - - printf("Prijati studenti:\n"); - for (int i = 0; i < num_accepted; i++) { - printf("%s\n", names[i]); + if (j == i) { + strcpy(students[i].name, name); + students[i].applied = 1; + } + } + + qsort(students, i, sizeof(Student), cmpfunc); + + printf("Prijati studenti:\n"); + for (i = 0; i < max_students; i++) { + if (students[i].applied == 1) { + printf("%s\n", students[i].name); + students_accepted++; + } else { + break; + } + } + + if (students_accepted < num_students) { + printf("Neprijati studenti:\n"); + for (i = 0; i < num_students; i++) { + if (students[i].applied == 0) { + printf("%s\n", students[i].name); + } } } - - // if (num_accepted < num_students) { - // printf("Neprijati studenti:"); - // for (int i = 0; i < max_students; i++) { - // if (names[i][0] == '\0') { - // printf("%s", names[i]); - // } - // } - // } return 0; -} +} \ No newline at end of file