This commit is contained in:
Tančáková 2024-03-20 15:45:47 +01:00
parent e553e8b016
commit d2f32470b6

View File

@ -5,73 +5,65 @@
#define MAX_NAME_LENGTH 100 #define MAX_NAME_LENGTH 100
#define MAX_BUFFER_LENGTH 256 #define MAX_BUFFER_LENGTH 256
// Štruktúra pre uchovanie informácií o študentovi // Structure to store student information
typedef struct { typedef struct {
char name[MAX_NAME_LENGTH]; char name[MAX_NAME_LENGTH];
int votes; int votes;
} Student; } Student;
// Porovnávacia funkcia pre zoradenie študentov podľa počtu hlasov a mena // Comparison function for sorting students by votes and name
int compare_students(const void *a, const void *b) { int compare_students(const void *a, const void *b) {
const Student *student_a = (const Student *)a; const Student *student_a = (const Student *)a;
const Student *student_b = (const Student *)b; const Student *student_b = (const Student *)b;
// Najprv porovnajte počet hlasov // First compare by number of votes
if (student_a->votes != student_b->votes) { if (student_a->votes != student_b->votes) {
return student_b->votes - student_a->votes; // Zoradiť zostupne return student_b->votes - student_a->votes; // Sort in descending order
} }
// Ak majú rovnaký počet hlasov, porovnajte podľa mena // If votes are tied, compare lexicographically by name
return strcmp(student_a->name, student_b->name); return strcmp(student_a->name, student_b->name);
} }
int main() { int main() {
// Inicializácia poľa študentov // Initialize array to store students
Student students[MAX_BUFFER_LENGTH]; Student students[MAX_BUFFER_LENGTH];
int num_students = 0; int num_students = 0;
// Načítanie hlasov zo štandardného vstupu // Read votes from standard input
char buffer[MAX_BUFFER_LENGTH]; char buffer[MAX_BUFFER_LENGTH];
while (fgets(buffer, sizeof(buffer), stdin) != NULL) { while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
// Načítajte počet hlasov a meno študenta // Read number of votes and student's name
char *token = strtok(buffer, "\x20"); int votes;
if (token == NULL) {
fprintf(stderr, "Chyba: Neplatný formát vstupu.\n");
return 0;
}
int votes = atoi(token);
token = strtok(NULL, "\n");
if (token == NULL) {
fprintf(stderr, "Chyba: Neplatný formát vstupu.\n");
return 0;
}
char name[MAX_NAME_LENGTH]; char name[MAX_NAME_LENGTH];
strncpy(name, token, MAX_NAME_LENGTH); if (sscanf(buffer, "%d %99[^\n]", &votes, name) != 2) {
fprintf(stderr, "Error: Invalid input format.\n");
return 1;
}
// Uložte študenta do poľa // Store student in array
strncpy(students[num_students].name, name, MAX_NAME_LENGTH); strncpy(students[num_students].name, name, MAX_NAME_LENGTH);
students[num_students].votes = votes; students[num_students].votes = votes;
num_students++; num_students++;
// Kontrola prekročenia maximálnej veľkosti poľa študentov // Check for exceeding maximum number of students
if (num_students >= MAX_BUFFER_LENGTH) { if (num_students >= MAX_BUFFER_LENGTH) {
fprintf(stderr, "Chyba: Príliš veľa študentov.\n"); fprintf(stderr, "Error: Too many students.\n");
return 0; return 1;
} }
} }
// Ak neboli načítané žiadne dáta, vypíšte chybovú správu // Print error message if no records were retrieved
if (num_students == 0) { if (num_students == 0) {
fprintf(stderr, "Chyba: Žiadne záznamy načítané.\n"); fprintf(stderr, "Error: No records retrieved.\n");
return 0; return 1;
} }
// Zoradenie študentov podľa počtu hlasov a mena // Sort students by number of votes and name
qsort(students, num_students, sizeof(Student), compare_students); qsort(students, num_students, sizeof(Student), compare_students);
// Výpis výsledkov // Print results
printf("Vysledky:\n"); printf("Results:\n");
for (int i = 0; i < num_students; i++) { for (int i = 0; i < num_students; i++) {
printf("%d %s\n", students[i].votes, students[i].name); printf("%d %s\n", students[i].votes, students[i].name);
} }