“program”
This commit is contained in:
parent
0b02744f3c
commit
85fbe3b98d
@ -8,67 +8,79 @@ int compare(const void *a, const void *b) {
|
|||||||
return strcmp(*(const char **)a, *(const char **)b);
|
return strcmp(*(const char **)a, *(const char **)b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void free_memory(char **array, int length) {
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
free(array[i]);
|
||||||
|
}
|
||||||
|
free(array);
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int max_students;
|
int max_students;
|
||||||
char temp[MAX_NAME_LENGTH];
|
char name[MAX_NAME_LENGTH];
|
||||||
char **students;
|
char **students = NULL;
|
||||||
int count = 0, unique_count = 0;
|
int num_students = 0;
|
||||||
|
|
||||||
if (scanf("%d", &max_students) != 1 || max_students <= 0) {
|
if (scanf("%d\n", &max_students) != 1 || max_students <= 0) {
|
||||||
puts("Nespravny vstup");
|
puts("Nespravny vstup");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
students = malloc(max_students * sizeof(char *));
|
students = (char **)malloc(max_students * sizeof(char *));
|
||||||
if (students == NULL) {
|
if (students == NULL) {
|
||||||
puts("Memory allocation failed");
|
puts("Memory allocation failed");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (scanf("%99s", temp) == 1) {
|
while (fgets(name, MAX_NAME_LENGTH, stdin) && num_students < max_students) {
|
||||||
int is_unique = 1;
|
name[strcspn(name, "\n")] = 0;
|
||||||
for (int i = 0; i < unique_count; i++) {
|
|
||||||
if (strcmp(students[i], temp) == 0) {
|
if (strlen(name) == 0) {
|
||||||
is_unique = 0;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int duplicate = 0;
|
||||||
|
for (int i = 0; i < num_students; i++) {
|
||||||
|
if (strcmp(students[i], name) == 0) {
|
||||||
|
duplicate = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_unique) {
|
if (!duplicate) {
|
||||||
students[unique_count] = strdup(temp);
|
students[num_students] = strdup(name);
|
||||||
if (students[unique_count] == NULL) {
|
if (students[num_students] == NULL) {
|
||||||
puts("Memory allocation failed");
|
puts("Memory allocation failed");
|
||||||
for (int i = 0; i < unique_count; i++) {
|
free_memory(students, num_students);
|
||||||
free(students[i]);
|
|
||||||
}
|
|
||||||
free(students);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
unique_count++;
|
num_students++;
|
||||||
if (unique_count >= max_students) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (unique_count == 0) {
|
if (num_students == 0) {
|
||||||
puts("Ziadne prihlasky");
|
puts("Ziadne prihlasky");
|
||||||
free(students);
|
free(students);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
qsort(students, unique_count, sizeof(char *), compare);
|
qsort(students, num_students, sizeof(char *), compare);
|
||||||
|
|
||||||
puts("Prijati studenti:");
|
puts("Prijati studenti:");
|
||||||
for (int i = 0; i < unique_count; i++) {
|
for (int i = 0; i < num_students; i++) {
|
||||||
puts(students[i]);
|
if (i < max_students) {
|
||||||
|
puts(students[i]);
|
||||||
|
} else {
|
||||||
|
if (i == max_students) {
|
||||||
|
puts("Neprijati studenti:");
|
||||||
|
}
|
||||||
|
puts(students[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < unique_count; i++) {
|
free_memory(students, num_students);
|
||||||
free(students[i]);
|
|
||||||
}
|
|
||||||
free(students);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user