Initialization

This commit is contained in:
Kozar 2024-04-25 20:10:32 +02:00
parent a56e7176ad
commit a2e49258b7

View File

@ -2,69 +2,67 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
int compare_names(const void *a, const void *b) { // Funkcia na kontrolu abecedného poradia
int compare(const void *a, const void *b) {
return strcmp(*(const char **)a, *(const char **)b); return strcmp(*(const char **)a, *(const char **)b);
} }
int main() { int main() {
int count; int max_students;
char **applications = NULL;
char buffer[100]; char buffer[100];
char **applications; int num_applications = 0;
int i, j;
if (scanf("%d", &count) != 1 || count <= 0) { // Načítanie počtu študentov na prijatie
printf("Nespravny vstup\n"); if (scanf("%d", &max_students) != 1 || max_students <= 0) {
puts("Nespravny vstup");
return 1; return 1;
} }
applications = (char **)malloc(count * sizeof(char *)); // Dynamická alokácia pamäte pre zoznam prihlášok
if (!applications) { applications = (char **)malloc(max_students * sizeof(char *));
printf("Chyba pri alokacii pamate\n"); if (applications == NULL) {
puts("Chyba alokacie pamate");
return 1; return 1;
} }
i = 0; // Načítanie prihlášok
while (i < count && fgets(buffer, sizeof(buffer), stdin)) { while (scanf("%s", buffer) == 1 && buffer[0] != '\0') {
int len = strlen(buffer); applications[num_applications] = strdup(buffer);
if (len > 0 && buffer[len - 1] == '\n') { // Remove newline character if present if (applications[num_applications] == NULL) {
buffer[len - 1] = '\0'; puts("Chyba alokacie pamate");
} return 1;
int found = 0;
for (j = 0; j < i; j++) {
if (strcmp(applications[j], buffer) == 0) {
found = 1;
break;
}
}
if (!found) {
applications[i] = strdup(buffer);
i++;
} }
num_applications++;
} }
if (i == 0) { // Kontrola, či boli načítané nejaké prihlášky
printf("Ziadne prihlasky\n"); if (num_applications == 0) {
puts("Ziadne prihlasky");
return 1; return 1;
} }
// Sort the applications alphabetically // Usporiadanie prihlášok podľa abecedy
qsort(applications, i, sizeof(char *), compare_names); qsort(applications, num_applications, sizeof(char *), compare);
printf("Prijati studenti:"); // Výpis prijatých študentov
for (j = 0; j < i; j++) { puts("Prijati studenti:");
printf("%s\n", applications[j]); int i;
for (i = 0; i < num_applications && i < max_students; i++) {
printf("%s\n", applications[i]);
} }
if (count < i) { // Výpis neprijatých študentov
printf("Neprijati studenti:"); if (i < num_applications) {
for (; j < i; j++) { puts("Neprijati studenti:");
printf("%s\n", applications[j]); for (; i < num_applications; i++) {
printf("%s\n", applications[i]);
} }
} }
// Free allocated memory // Uvoľnenie pamäte
for (j = 0; j < i; j++) { for (i = 0; i < num_applications; i++) {
free(applications[j]); free(applications[i]);
} }
free(applications); free(applications);