pvjc24/cv10/program.c

67 lines
1.8 KiB
C
Raw Normal View History

2024-04-22 12:01:17 +00:00
#include <stdio.h>
2024-04-25 16:32:35 +00:00
#include <stdlib.h>
2024-04-25 15:29:10 +00:00
#include <string.h>
2024-04-22 12:01:17 +00:00
2024-04-25 18:10:32 +00:00
// Funkcia na kontrolu abecedného poradia
int compare(const void *a, const void *b) {
2024-04-25 17:04:21 +00:00
return strcmp(*(const char **)a, *(const char **)b);
}
2024-04-22 12:01:17 +00:00
int main() {
2024-04-25 18:10:32 +00:00
int max_students;
char **applications = NULL;
2024-04-25 16:42:45 +00:00
char buffer[100];
2024-04-25 18:10:32 +00:00
int num_applications = 0;
2024-04-22 12:06:40 +00:00
2024-04-25 18:10:32 +00:00
// Načítanie počtu študentov na prijatie
if (scanf("%d", &max_students) != 1 || max_students <= 0) {
puts("Nespravny vstup");
2024-04-22 12:06:40 +00:00
return 1;
}
2024-04-22 12:01:17 +00:00
2024-04-25 18:10:32 +00:00
// Dynamická alokácia pamäte pre zoznam prihlášok
applications = (char **)malloc(max_students * sizeof(char *));
if (applications == NULL) {
puts("Chyba alokacie pamate");
2024-04-25 16:42:45 +00:00
return 1;
2024-04-25 16:32:35 +00:00
}
2024-04-25 15:41:03 +00:00
2024-04-25 18:10:32 +00:00
// Načítanie prihlášok
2024-04-25 18:13:58 +00:00
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
}
2024-04-25 18:10:32 +00:00
applications[num_applications] = strdup(buffer);
if (applications[num_applications] == NULL) {
puts("Chyba alokacie pamate");
return 1;
2024-04-25 16:42:45 +00:00
}
2024-04-25 18:10:32 +00:00
num_applications++;
2024-04-25 16:42:45 +00:00
}
2024-04-25 18:10:32 +00:00
// Kontrola, či boli načítané nejaké prihlášky
if (num_applications == 0) {
puts("Ziadne prihlasky");
2024-04-25 16:32:35 +00:00
return 1;
}
2024-04-25 15:56:47 +00:00
2024-04-25 18:10:32 +00:00
// Usporiadanie prihlášok podľa abecedy
qsort(applications, num_applications, sizeof(char *), compare);
2024-04-22 12:01:17 +00:00
2024-04-25 18:10:32 +00:00
// Výpis prijatých študentov
puts("Prijati studenti:");
int i;
2024-04-25 18:13:58 +00:00
for (i = 0; i < num_applications; i++) {
2024-04-25 18:10:32 +00:00
printf("%s\n", applications[i]);
2024-04-22 12:01:17 +00:00
}
2024-04-25 18:10:32 +00:00
// Uvoľnenie pamäte
for (i = 0; i < num_applications; i++) {
free(applications[i]);
2024-04-25 16:42:45 +00:00
}
free(applications);
2024-04-22 12:01:17 +00:00
return 0;
2024-04-25 18:02:38 +00:00
}