Update 'cv10/program.c'

This commit is contained in:
Tokarčík 2024-04-25 18:49:44 +00:00
parent abf624ddb6
commit ad6c53e639

View File

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