“program”
This commit is contained in:
parent
b67900303f
commit
0b02744f3c
116
cv10/program.c
116
cv10/program.c
@ -1,94 +1,74 @@
|
|||||||
~~#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define MAX_NAME_LENGTH 100
|
#define MAX_NAME_LENGTH 100
|
||||||
|
|
||||||
int compare(const void *a, const void *b) {
|
int compare(const void *a, const void *b) {
|
||||||
const char *name1 = *(const char **)a;
|
return strcmp(*(const char **)a, *(const char **)b);
|
||||||
const char *name2 = *(const char **)b;
|
|
||||||
return strcmp(name1, name2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int max_students;
|
int max_students;
|
||||||
char name[MAX_NAME_LENGTH];
|
char temp[MAX_NAME_LENGTH];
|
||||||
char **names = NULL;
|
char **students;
|
||||||
int size = 0, capacity = 10;
|
int count = 0, unique_count = 0;
|
||||||
|
|
||||||
names = (char **)malloc(capacity * sizeof(char *));
|
if (scanf("%d", &max_students) != 1 || max_students <= 0) {
|
||||||
if (names == NULL) {
|
|
||||||
puts("Memory allocation error");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (scanf("%d\n", &max_students) != 1 || max_students <= 0) {
|
|
||||||
puts("Nespravny vstup");
|
puts("Nespravny vstup");
|
||||||
free(names);
|
return 1;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while (fgets(name, MAX_NAME_LENGTH, stdin) != NULL) {
|
students = malloc(max_students * sizeof(char *));
|
||||||
if (name[0] == '\n') break;
|
if (students == NULL) {
|
||||||
name[strcspn(name, "\n")] = 0;
|
puts("Memory allocation failed");
|
||||||
|
return 1;
|
||||||
int duplicate = 0;
|
}
|
||||||
for (int i = 0; i < size; i++) {
|
|
||||||
if (strcmp(names[i], name) == 0) {
|
while (scanf("%99s", temp) == 1) {
|
||||||
duplicate = 1;
|
int is_unique = 1;
|
||||||
|
for (int i = 0; i < unique_count; i++) {
|
||||||
|
if (strcmp(students[i], temp) == 0) {
|
||||||
|
is_unique = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (duplicate) continue;
|
|
||||||
|
if (is_unique) {
|
||||||
if (size == capacity) {
|
students[unique_count] = strdup(temp);
|
||||||
capacity *= 2;
|
if (students[unique_count] == NULL) {
|
||||||
char **new_names = (char **)realloc(names, capacity * sizeof(char *));
|
puts("Memory allocation failed");
|
||||||
if (new_names == NULL) {
|
for (int i = 0; i < unique_count; i++) {
|
||||||
puts("Memory allocation error");
|
free(students[i]);
|
||||||
for (int j = 0; j < size; j++) free(names[j]);
|
}
|
||||||
free(names);
|
free(students);
|
||||||
return 0;
|
return 1;
|
||||||
|
}
|
||||||
|
unique_count++;
|
||||||
|
if (unique_count >= max_students) {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
names = new_names;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
names[size] = strdup(name);
|
|
||||||
if (names[size] == NULL) {
|
|
||||||
puts("Memory allocation error");
|
|
||||||
for (int j = 0; j < size; j++) free(names[j]);
|
|
||||||
free(names);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
size++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (size == 0) {
|
if (unique_count == 0) {
|
||||||
puts("Ziadne prihlasky");
|
puts("Ziadne prihlasky");
|
||||||
free(names);
|
free(students);
|
||||||
return 0;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
qsort(names, size, sizeof(char *), compare);
|
qsort(students, unique_count, sizeof(char *), compare);
|
||||||
|
|
||||||
puts("Prijati studenti:");
|
puts("Prijati studenti:");
|
||||||
int accepted = size < max_students ? size : max_students;
|
for (int i = 0; i < unique_count; i++) {
|
||||||
for (int i = 0; i < accepted; i++) {
|
puts(students[i]);
|
||||||
puts(names[i]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (size > max_students) {
|
for (int i = 0; i < unique_count; i++) {
|
||||||
puts("Neprijati studenti:");
|
free(students[i]);
|
||||||
for (int i = max_students; i < size; i++) {
|
|
||||||
puts(names[i]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
free(students);
|
||||||
for (int i = 0; i < size; i++) {
|
|
||||||
free(names[i]);
|
|
||||||
}
|
|
||||||
free(names);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user