67 lines
1.8 KiB
C
67 lines
1.8 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
// 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 max_students;
|
|
char **applications = NULL;
|
|
char buffer[100];
|
|
int num_applications = 0;
|
|
|
|
// Načítanie počtu študentov na prijatie
|
|
if (scanf("%d", &max_students) != 1 || max_students <= 0) {
|
|
puts("Nespravny vstup");
|
|
return 1;
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
// Načítanie prihlášok
|
|
while (num_applications < max_students && fgets(buffer, sizeof(buffer), stdin) != NULL && buffer[0] != '\n') {
|
|
size_t len = strlen(buffer);
|
|
if (buffer[len - 1] == '\n') {
|
|
buffer[len - 1] = '\0'; // odstránenie koncového znaku nového riadku
|
|
}
|
|
applications[num_applications] = strdup(buffer);
|
|
if (applications[num_applications] == NULL) {
|
|
puts("Chyba alokacie pamate");
|
|
return 1;
|
|
}
|
|
num_applications++;
|
|
}
|
|
|
|
// Kontrola, či boli načítané nejaké prihlášky
|
|
if (num_applications == 0) {
|
|
puts("Ziadne prihlasky");
|
|
return 1;
|
|
}
|
|
|
|
// Usporiadanie prihlášok podľa abecedy
|
|
qsort(applications, num_applications, sizeof(char *), compare);
|
|
|
|
// Výpis prijatých študentov
|
|
puts("Prijati studenti:");
|
|
int i;
|
|
for (i = 0; i < num_applications; i++) {
|
|
printf("%s\n", applications[i]);
|
|
}
|
|
|
|
// Uvoľnenie pamäte
|
|
for (i = 0; i < num_applications; i++) {
|
|
free(applications[i]);
|
|
}
|
|
free(applications);
|
|
|
|
return 0;
|
|
}
|