Initialization
This commit is contained in:
parent
b5a6d7ece8
commit
9cef985af2
@ -2,50 +2,71 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define MAX_LENGTH 100 // Maximálna dĺžka mena študenta
|
|
||||||
#define MAX_STUDENTS 100 // Maximálny počet študentov
|
|
||||||
|
|
||||||
int compare(const void *a, const void *b) {
|
|
||||||
return strcmp(*(char **)a, *(char **)b);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
char prihlasky[MAX_STUDENTS][MAX_LENGTH]; // Pole pre uloženie prihlášok
|
int count;
|
||||||
int max_prijati, pocet_prihlasek = 0;
|
char buffer[100];
|
||||||
|
char **applications;
|
||||||
|
int i, j;
|
||||||
|
|
||||||
// Načítanie maximálneho počtu študentov na prijatie
|
if (scanf("%d", &count) != 1 || count <= 0) {
|
||||||
if (scanf("%d", &max_prijati)!= 1 || max_prijati <= 0) {
|
|
||||||
printf("Nespravny vstup\n");
|
printf("Nespravny vstup\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Načítanie zoznamu prihlášok
|
applications = (char **)malloc(count * sizeof(char *));
|
||||||
while (scanf("%s", prihlasky[pocet_prihlasek]) == 1) {
|
if (!applications) {
|
||||||
pocet_prihlasek++;
|
printf("Chyba pri alokacii pamate\n");
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Kontrola, či neboli načítané žiadne prihlášky
|
i = 0;
|
||||||
if (pocet_prihlasek == 0) {
|
while (scanf("%s", buffer) == 1 && strcmp(buffer, "") != 0) {
|
||||||
|
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++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i == 0) {
|
||||||
printf("Ziadne prihlasky\n");
|
printf("Ziadne prihlasky\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Zoradenie zoznamu prihlášok podľa abecedy
|
// Sort the applications alphabetically
|
||||||
qsort(prihlasky, pocet_prihlasek, MAX_LENGTH, compare);
|
for (int m = 0; m < i - 1; m++) {
|
||||||
|
for (int n = m + 1; n < i; n++) {
|
||||||
// Výber študentov na prijatie podľa abecedy a maximálneho počtu
|
if (strcmp(applications[m], applications[n]) > 0) {
|
||||||
printf("Prijati studenti:\n");
|
char *temp = applications[m];
|
||||||
for (int i = 0; i < max_prijati && i < pocet_prihlasek; i++) {
|
applications[m] = applications[n];
|
||||||
printf("%s\n", prihlasky[i]);
|
applications[n] = temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Výpis neprijatých študentov
|
|
||||||
if (max_prijati < pocet_prihlasek) {
|
|
||||||
printf("Neprijati studenti:\n");
|
|
||||||
for (int i = max_prijati; i < pocet_prihlasek; i++) {
|
|
||||||
printf("%s\n", prihlasky[i]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printf("Prijati studenti:\n");
|
||||||
|
for (j = 0; j < (count < i ? count : i); j++) {
|
||||||
|
printf("%s\n", applications[j]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count < i) {
|
||||||
|
printf("Neprijati studenti:\n");
|
||||||
|
for (; j < i; j++) {
|
||||||
|
printf("%s\n", applications[j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Free allocated memory
|
||||||
|
for (j = 0; j < i; j++) {
|
||||||
|
free(applications[j]);
|
||||||
|
}
|
||||||
|
free(applications);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user