This commit is contained in:
Macko 2024-04-15 22:09:20 +02:00
parent 0c730c6c4f
commit f2064443b2

View File

@ -5,24 +5,22 @@
#define SIZE 100 #define SIZE 100
int compare(const void* arg1, const void* arg2) { int compare(const void* arg1, const void* arg2) {
const char* s1 = *((const char**)arg1); char* s1 = *((char**)arg1);
const char* s2 = *((const char**)arg2); char* s2 = *((char**)arg2);
return strcmp(s1, s2); return strcmp(s1, s2);
} }
int main() { 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; int pocet_mien_v_poli = 0;
char line[SIZE]; char line[SIZE];
while (fgets(line, SIZE, stdin) != NULL) { while (fgets(line, SIZE, stdin) != NULL) {
int pocet_znakov = strlen(line); line[strcspn(line, "\n")] = 0; // Remove the newline character
if (pocet_znakov == 1 && line[0] == '\n') // Skončiť na prázdnom riadku int pocet_znakov = strlen(line) + 1; // Include the null terminator
break; if (pocet_znakov == 1) // Skip empty lines
continue;
// Odstrániť koniec riadka
if (line[pocet_znakov - 1] == '\n')
line[--pocet_znakov] = '\0';
// Kontrola duplicít // Kontrola duplicít
int found = 0; int found = 0;
@ -32,12 +30,8 @@ int main() {
break; break;
} }
} }
if (!found && pocet_mien_v_poli < SIZE) { if (!found) {
pole_smernikov[pocet_mien_v_poli] = malloc(pocet_znakov + 1); pole_smernikov[pocet_mien_v_poli] = malloc(pocet_znakov);
if (pole_smernikov[pocet_mien_v_poli] == NULL) {
fprintf(stderr, "Chyba pri alokácii pamäte\n");
exit(EXIT_FAILURE);
}
strcpy(pole_smernikov[pocet_mien_v_poli], line); strcpy(pole_smernikov[pocet_mien_v_poli], line);
pocet_mien_v_poli += 1; pocet_mien_v_poli += 1;
} }
@ -48,10 +42,7 @@ int main() {
// Výpis prijatých a neprijatých študentov // Výpis prijatých a neprijatých študentov
int max_students; int max_students;
if (scanf("%d", &max_students) != 1) { // Kontrola úspešnosti načítania scanf("%d", &max_students);
fprintf(stderr, "Chyba pri načítaní počtu študentov\n");
exit(EXIT_FAILURE);
}
printf("Prijati studenti:\n"); printf("Prijati studenti:\n");
for (int i = 0; i < max_students && i < pocet_mien_v_poli; i++) { for (int i = 0; i < max_students && i < pocet_mien_v_poli; i++) {