pvjc26/a3/program.c

85 lines
2.0 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];
};
int main()
{
//helper/pomocne lokalne premeny
int studentsAmount;
struct studentApplication student[STUDENT_SIZE];
int i = 0;
char processedName[BUFFER_SIZE];
char newLineSymbol = '\n';
char line[BUFFER_SIZE];
memset(line,0, sizeof(line));
setMemoryOfArrays(processedName, student);
while (fgets(line, BUFFER_SIZE, stdin) != NULL)
{
if (studentsAmount == 0)
{
if(sscanf(line, "%d", &studentsAmount) == 1) { continue; }
}
else if(sscanf(line, "%s", processedName) == 1)
{
strcpy(student[i].name, processedName);
strncat(student[i].name, &newLineSymbol, 1);
i++;
}
}
if (studentsAmount == 0)
{
puts("Nespravny vstup");
return 0;
}
qsort(student, STUDENT_SIZE, sizeof(struct studentApplication), compare);
// vypis vysledkov:
for (int i = 0; i < studentsAmount; i++)
{
puts("Prijati studenti:");
puts(student[i].name);
}
for (int i = studentsAmount; student[i].meno[0] != '\0'; i++)
{
puts("Neprijati studenti:");
puts(student[i].name);
}
return 0;
}
void setMemoryOfArrays(char* processedName, 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;
return strcmp(s1->name, s2->name);
}