pvjc24/cv10/program.c

75 lines
1.7 KiB
C
Raw Normal View History

2024-04-23 07:41:48 +00:00
#include <stdio.h>
2024-04-17 21:09:04 +00:00
#include <stdlib.h>
#include <string.h>
2024-04-19 13:39:33 +00:00
#define MAX_NAME_LENGTH 100
int compare(const void *a, const void *b) {
2024-04-23 07:41:48 +00:00
return strcmp(*(const char **)a, *(const char **)b);
2024-04-17 21:09:04 +00:00
}
int main() {
2024-04-19 13:39:33 +00:00
int max_students;
2024-04-23 07:41:48 +00:00
char temp[MAX_NAME_LENGTH];
char **students;
int count = 0, unique_count = 0;
if (scanf("%d", &max_students) != 1 || max_students <= 0) {
2024-04-17 21:09:04 +00:00
puts("Nespravny vstup");
2024-04-23 07:41:48 +00:00
return 1;
2024-04-17 21:09:04 +00:00
}
2024-04-23 07:41:48 +00:00
students = malloc(max_students * sizeof(char *));
if (students == NULL) {
puts("Memory allocation failed");
return 1;
}
while (scanf("%99s", temp) == 1) {
int is_unique = 1;
for (int i = 0; i < unique_count; i++) {
if (strcmp(students[i], temp) == 0) {
is_unique = 0;
2024-04-19 13:39:33 +00:00
break;
}
}
2024-04-23 07:41:48 +00:00
if (is_unique) {
students[unique_count] = strdup(temp);
if (students[unique_count] == NULL) {
puts("Memory allocation failed");
for (int i = 0; i < unique_count; i++) {
free(students[i]);
}
free(students);
return 1;
}
unique_count++;
if (unique_count >= max_students) {
break;
2024-04-17 21:09:04 +00:00
}
}
}
2024-04-23 07:41:48 +00:00
if (unique_count == 0) {
2024-04-17 21:09:04 +00:00
puts("Ziadne prihlasky");
2024-04-23 07:41:48 +00:00
free(students);
return 1;
2024-04-17 21:09:04 +00:00
}
2024-04-23 07:41:48 +00:00
qsort(students, unique_count, sizeof(char *), compare);
2024-04-17 21:09:04 +00:00
puts("Prijati studenti:");
2024-04-23 07:41:48 +00:00
for (int i = 0; i < unique_count; i++) {
puts(students[i]);
2024-04-17 21:09:04 +00:00
}
2024-04-23 07:41:48 +00:00
for (int i = 0; i < unique_count; i++) {
free(students[i]);
2024-04-17 21:09:04 +00:00
}
2024-04-23 07:41:48 +00:00
free(students);
2024-04-17 21:09:04 +00:00
return 0;
}
2024-04-19 13:48:31 +00:00