pvjc24/cv10/program.c

65 lines
1.7 KiB
C
Raw Normal View History

2024-04-15 20:00:45 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
int compare(const void* arg1, const void* arg2) {
2024-04-15 20:09:20 +00:00
char* s1 = *((char**)arg1);
char* s2 = *((char**)arg2);
2024-04-15 20:00:45 +00:00
return strcmp(s1, s2);
}
int main() {
2024-04-15 20:09:20 +00:00
char* pole_smernikov[SIZE];
memset(pole_smernikov, 0, SIZE * sizeof(char*));
2024-04-15 20:00:45 +00:00
int pocet_mien_v_poli = 0;
char line[SIZE];
while (fgets(line, SIZE, stdin) != NULL) {
2024-04-15 20:09:20 +00:00
line[strcspn(line, "\n")] = 0; // Remove the newline character
int pocet_znakov = strlen(line) + 1; // Include the null terminator
if (pocet_znakov == 1) // Skip empty lines
continue;
2024-04-15 20:03:45 +00:00
2024-04-15 20:00:45 +00:00
// Kontrola duplicít
int found = 0;
for (int i = 0; i < pocet_mien_v_poli; i++) {
2024-04-15 20:03:45 +00:00
if (strcmp(pole_smernikov[i], line) == 0) {
2024-04-15 20:00:45 +00:00
found = 1;
break;
}
}
2024-04-15 20:09:20 +00:00
if (!found) {
pole_smernikov[pocet_mien_v_poli] = malloc(pocet_znakov);
2024-04-15 20:03:45 +00:00
strcpy(pole_smernikov[pocet_mien_v_poli], line);
2024-04-15 20:00:45 +00:00
pocet_mien_v_poli += 1;
}
}
// Triedenie
qsort(pole_smernikov, pocet_mien_v_poli, sizeof(char*), compare);
// Výpis prijatých a neprijatých študentov
int max_students;
2024-04-15 20:13:50 +00:00
fgets(line, SIZE, stdin);
sscanf(line, "%d", &max_students);
2024-04-15 20:00:45 +00:00
printf("Prijati studenti:\n");
for (int i = 0; i < max_students && i < pocet_mien_v_poli; i++) {
2024-04-15 20:03:45 +00:00
printf("%s\n", pole_smernikov[i]);
2024-04-15 20:00:45 +00:00
}
printf("Neprijati studenti:\n");
for (int i = max_students; i < pocet_mien_v_poli; i++) {
2024-04-15 20:03:45 +00:00
printf("%s\n", pole_smernikov[i]);
2024-04-15 20:00:45 +00:00
}
// Uvoľnenie pamäte
for (int i = 0; i < pocet_mien_v_poli; i++) {
free(pole_smernikov[i]);
}
return 0;
}