diff --git a/cv10/program.c b/cv10/program.c index 51c1989..3fac192 100644 --- a/cv10/program.c +++ b/cv10/program.c @@ -1,51 +1,50 @@ #include -#include #include -#define SIZE 100 - -int compare(const void* arg1, const void* arg2) { - char* s1 = *((char**)arg1); - char* s2 = *((char**)arg2); - return strcmp(s1, s2); -} +#define MAX_STUDENTS 100 +#define NAME_SIZE 50 int main() { - char* pole_smernikov[SIZE]; - memset(pole_smernikov, 0, SIZE * sizeof(char*)); + int max_students, num_accepted = 0; + char names[MAX_STUDENTS][NAME_SIZE]; + char temp_name[NAME_SIZE]; - char line[SIZE]; - memset(line, 0, SIZE); + scanf("%d", &max_students); + if (max_students <= 0 || max_students > MAX_STUDENTS) { + printf("Nespravny vstup\n"); + return 1; + } - int pocet_mien_v_poli = 0; + while (scanf("%s", temp_name) != EOF && num_accepted < max_students) { + int i, found = 0; - while (fgets(line, SIZE, stdin) != NULL && strlen(line) > 0) { - int found = 0; - for (int i = 0; i < pocet_mien_v_poli; i++) { - if (memcmp(pole_smernikov[i], line, strlen(line)) == 0) { + for (i = 0; i < num_accepted; i++) { + if (strcmp(names[i], temp_name) == 0) { found = 1; break; } } if (!found) { - pole_smernikov[pocet_mien_v_poli] = malloc(strlen(line) + 1); - memcpy(pole_smernikov[pocet_mien_v_poli], line, strlen(line) + 1); - pocet_mien_v_poli++; + strcpy(names[num_accepted], temp_name); + num_accepted++; } - - memset(line, 0, SIZE); } - qsort(pole_smernikov, pocet_mien_v_poli, sizeof(char*), compare); - - printf("Prijati studenti:\n"); - for (int i = 0; i < pocet_mien_v_poli; i++) { - printf("%s", pole_smernikov[i]); + if (num_accepted == 0) { + printf("Ziadne prihlasky\n"); + } else { + printf("Prijati studenti:\n"); + for (int i = 0; i < num_accepted; i++) { + printf("%s\n", names[i]); + } } - for (int i = 0; i < pocet_mien_v_poli; i++) { - free(pole_smernikov[i]); + if (num_accepted < max_students) { + printf("Neprijati studenti:\n"); + for (int i = num_accepted; i < max_students; i++) { + printf("%s\n", names[i]); + } } return 0;