pvjc24/cv10/program.c

108 lines
2.0 KiB
C
Raw Normal View History

2024-04-25 18:43:30 +00:00
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdbool.h>
#define SIZE 100
int compare(const void* arg1, const void* arg2){
char* s1 = *((char**)arg1);
char* s2 = *((char**)arg2);
return strcmp(s1, s2);
}
void printStudents(char* pole_smernikov[], int mena, int MaxCislo) {
printf("Prijati studenti:\n");
for (int i = 0; i < MaxCislo && i < mena; i++) {
printf("%s", pole_smernikov[i]);
}
if (mena > MaxCislo) {
printf("Neprijati studenti:\n");
for (int i = MaxCislo; i < mena; i++) {
printf("%s", pole_smernikov[i]);
}
}
}
void uvolnenie_pamate(char* pole_smernikov[], int mena){
for (int i = 0; i < mena; i++){
free(pole_smernikov[i]);
}
}
bool CheckRepeat(char* pole_smernikov[], char* riadok, int mena) {
for (int i = 0; i < mena; i++) {
if (strcmp(pole_smernikov[i], riadok) == 0) {
return true;
}
}
return false;
}
int main (){
char* pole_smernikov[SIZE];
memset(pole_smernikov,0,SIZE*sizeof(char*));
int MaxCislo = 0;
scanf("%d", &MaxCislo);
getchar();
int mena = 0;
if (MaxCislo == 0 || MaxCislo == -1){
printf("Nespravny vstup");
}
else{
while (1){
char line[SIZE];
memset(line,0,SIZE);
char* r = fgets(line,SIZE,stdin);
if (r == NULL){
//nepodarilo sa
break;
}
//ci je prvy znak medzera alebo EOF
if (isspace(line[0]) || line [0] == EOF) {
break;
}
if (r!=NULL){
// Podarilo sa
int pocet_znakov = strlen(line);
if (CheckRepeat(pole_smernikov, line,mena) == false){
pole_smernikov[mena] = malloc(pocet_znakov + 1);
memcpy(pole_smernikov[mena],line,pocet_znakov + 1);
mena += 1;
}
}
}
}
if (mena == 0){
printf ("Ziadne prihlasky\n");
return 0;
}
qsort(pole_smernikov, mena, sizeof(char*), compare);
printStudents(pole_smernikov, mena, MaxCislo);
uvolnenie_pamate( pole_smernikov, mena);
return 0;
}