116 lines
2.9 KiB
C
116 lines
2.9 KiB
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <stdbool.h>
|
|
#include <string.h>
|
|
|
|
#define BUFFER_SIZE 64
|
|
#define STUDENT_SIZE 32
|
|
|
|
struct studentApplication
|
|
{
|
|
char name[STUDENT_SIZE];
|
|
char surname[STUDENT_SIZE];
|
|
};
|
|
void setMemoryOfArrays(char* processedName, char* processedSurname, struct studentApplication* student)
|
|
{
|
|
//prednastavi pamat mnozinovych premien
|
|
processedName[0] = '\0';
|
|
|
|
for (int i = 0; i < STUDENT_SIZE; i++)
|
|
{
|
|
student[i].name[0] = '\0';
|
|
}
|
|
|
|
}
|
|
int compare(const void* p1, const void* p2)
|
|
{
|
|
//casting, resp. premena dat. typu pointerov na ich spravny typ
|
|
//nakolko qsort() pozaduje od porovnavaciej funkcie, aby akceptovala pointery s lubovolnym dat. typom (const void*)
|
|
struct studentApplication *s1 = (struct studentApplication *)p1;
|
|
struct studentApplication *s2 = (struct studentApplication *)p2;
|
|
|
|
int result = strcmp(s1->name, s2->name);
|
|
|
|
if (result == 0)
|
|
{
|
|
return strcmp(s1->surname, s2->surname);
|
|
}
|
|
else
|
|
{
|
|
return strcmp(s1->name, s2->name);
|
|
}
|
|
}
|
|
int main()
|
|
{
|
|
//helper/pomocne lokalne premeny
|
|
int studentsAmount = 0;
|
|
struct studentApplication student[STUDENT_SIZE];
|
|
int i = 0; //pomocny index
|
|
char processedName[BUFFER_SIZE];
|
|
char processedSurname[BUFFER_SIZE];
|
|
char line[BUFFER_SIZE];
|
|
|
|
memset(line,0, sizeof(line));
|
|
|
|
setMemoryOfArrays(processedName, processedSurname, student);
|
|
|
|
while (fgets(line, BUFFER_SIZE, stdin) != NULL)
|
|
{
|
|
if (studentsAmount == 0)
|
|
{
|
|
if(sscanf(line, "%d", &studentsAmount) == 1) { continue; }
|
|
}
|
|
else if(sscanf(line, "%s %s", processedName, processedSurname) == 2)
|
|
{
|
|
strcpy(student[i].name, processedName);
|
|
strcpy(student[i].surname, processedSurname);
|
|
i++;
|
|
}
|
|
}
|
|
|
|
if (studentsAmount == 0)
|
|
{
|
|
puts("Nespravny vstup");
|
|
return 0;
|
|
}
|
|
|
|
if (i == 0)
|
|
{
|
|
puts("Ziadne prihlasky");
|
|
return 0;
|
|
}
|
|
|
|
qsort(student, studentsAmount, sizeof(struct studentApplication), compare);
|
|
|
|
//vykalkuluje realny mozny limit studentov na zaklade nacitanych zaznamov (index premena 'i' = celkovy pocet najdenych studentov)
|
|
//a celkovom pocte akceptovanych studentov najdenom na prvom riadku (studentsAmount premena) a ano, mam zle pomenovane premeny
|
|
|
|
int limit;
|
|
if (i < studentsAmount)
|
|
{
|
|
limit = i;
|
|
}
|
|
else
|
|
{
|
|
limit = studentsAmount;
|
|
}
|
|
|
|
puts("Prijati studenti:");
|
|
for (int x = 0; x < limit; x++)
|
|
{
|
|
printf("%s %s\n", student[x].name, student[x].surname);
|
|
}
|
|
|
|
//zacne vypisovat zaznamy neprijatych studentov, tam kde prijati koncia... (studentsAmount)
|
|
if (i > studentsAmount)
|
|
{
|
|
puts("Neprijati studenti:");
|
|
for (int x = studentsAmount; x < i; x++)
|
|
{
|
|
printf("%s %s\n", student[x].name, student[x].surname);
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|