pvjc24/cv10/program.c

65 lines
1.6 KiB
C
Raw Normal View History

2024-04-22 12:01:17 +00:00
#include <stdio.h>
2024-04-25 15:29:10 +00:00
#include <string.h>
2024-04-25 15:32:41 +00:00
#include <stdlib.h>
2024-04-22 12:01:17 +00:00
2024-04-25 15:32:41 +00:00
#define MAX_STUDENTS 100
#define NAME_SIZE 50
2024-04-22 12:01:17 +00:00
2024-04-25 15:32:41 +00:00
int compare_names(const void *a, const void *b) {
const char **name_a = a;
const char **name_b = b;
return strcmp(*name_a, *name_b);
2024-04-22 17:15:04 +00:00
}
2024-04-22 12:01:17 +00:00
int main() {
2024-04-25 15:32:41 +00:00
int max_students, num_accepted = 0, num_students = 0;
char names[MAX_STUDENTS][NAME_SIZE];
char temp_name[NAME_SIZE];
2024-04-22 12:06:40 +00:00
2024-04-25 15:32:41 +00:00
scanf("%d", &max_students);
if (max_students <= 0 || max_students > MAX_STUDENTS) {
printf("Nespravny vstup\n");
2024-04-22 12:06:40 +00:00
return 1;
}
2024-04-22 12:01:17 +00:00
2024-04-25 15:32:41 +00:00
memset(names, 0, sizeof(names));
2024-04-25 15:31:18 +00:00
2024-04-25 15:32:41 +00:00
while (fgets(temp_name, sizeof(temp_name), stdin) != NULL && num_accepted < max_students) {
2024-04-22 16:31:59 +00:00
2024-04-25 15:32:41 +00:00
int i, found = 0;
2024-04-22 12:01:17 +00:00
2024-04-25 15:41:03 +00:00
// Remove the newline character from the temp_name array
temp_name[strcspn(temp_name, "\n")] = '\0';
2024-04-25 15:32:41 +00:00
for (i = 0; i < num_accepted; i++) {
if (strcmp(names[i], temp_name) == 0) {
found = 1;
2024-04-22 12:01:17 +00:00
break;
}
}
2024-04-25 15:32:41 +00:00
if (!found) {
strcpy(names[num_accepted], temp_name);
num_accepted++;
2024-04-22 12:01:17 +00:00
}
2024-04-25 15:32:41 +00:00
num_students++;
2024-04-25 15:37:53 +00:00
// Check if we have reached the maximum number of accepted students
if (num_accepted == max_students) {
break; // Exit the loop
}
2024-04-22 12:01:17 +00:00
}
2024-04-25 15:39:11 +00:00
qsort(names, num_accepted, sizeof(names[0]), compare_names);
2024-04-25 15:29:10 +00:00
2024-04-25 15:32:41 +00:00
if (num_accepted == 0) {
printf("Ziadne prihlasky");
} else {
printf("Prijati studenti:\n");
for (int i = 0; i < num_accepted; i++) {
printf("%s", names[i]);
2024-04-22 12:06:40 +00:00
}
2024-04-22 12:01:17 +00:00
}
2024-04-22 16:28:15 +00:00
2024-04-22 12:01:17 +00:00
return 0;
2024-04-25 15:41:03 +00:00
}