2025-03-08 16:55:33 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#define MAX_COUNT_OF_STUDENTS 1000
|
|
|
|
#define NAME_LENGTH 100
|
2025-03-08 17:06:21 +00:00
|
|
|
#define LINE_SIZE 250
|
2025-03-08 16:55:33 +00:00
|
|
|
|
2025-03-08 17:06:21 +00:00
|
|
|
// Struktúra na uloženie informácií o študentovi (meno a počet hlasov)
|
2025-03-08 16:55:33 +00:00
|
|
|
typedef struct {
|
|
|
|
char name[NAME_LENGTH];
|
|
|
|
int votes;
|
|
|
|
} Student;
|
|
|
|
|
2025-03-08 17:06:21 +00:00
|
|
|
// Funkcia na vytvorenie študenta a inicializáciu jeho údajov
|
2025-03-08 16:55:33 +00:00
|
|
|
Student createStudent(const char name[], int votes) {
|
|
|
|
Student student;
|
2025-03-08 17:06:21 +00:00
|
|
|
strncpy(student.name, name, NAME_LENGTH - 1); // Kopíruje meno do štruktúry
|
|
|
|
student.name[NAME_LENGTH - 1] = '\0'; // Pridáva koncovú nulovú hodnotu pre bezpečnosť
|
|
|
|
student.votes = votes; // Ukladá počet hlasov
|
2025-03-08 16:55:33 +00:00
|
|
|
return student;
|
|
|
|
}
|
2025-03-08 17:06:21 +00:00
|
|
|
|
|
|
|
// Porovnávacia funkcia pre qsort na zoradenie študentov
|
2025-03-08 16:55:33 +00:00
|
|
|
int compare_students(const void *a, const void *b) {
|
|
|
|
const Student *studentA = (const Student *)a;
|
|
|
|
const Student *studentB = (const Student *)b;
|
|
|
|
|
2025-03-08 17:06:21 +00:00
|
|
|
// Najskôr porovnáme počet hlasov (zoradené zostupne)
|
2025-03-08 16:55:33 +00:00
|
|
|
if (studentA->votes != studentB->votes) {
|
|
|
|
return studentB->votes - studentA->votes;
|
|
|
|
}
|
|
|
|
|
2025-03-08 17:06:21 +00:00
|
|
|
// Ak majú rovnaký počet hlasov, zoradíme podľa mena abecedne
|
2025-03-08 16:55:33 +00:00
|
|
|
return strcmp(studentA->name, studentB->name);
|
|
|
|
}
|
2025-03-08 17:06:21 +00:00
|
|
|
|
2025-03-08 16:55:33 +00:00
|
|
|
int main() {
|
2025-03-08 17:06:21 +00:00
|
|
|
char line[LINE_SIZE]; // Premenná na čítanie riadka vstupu
|
|
|
|
Student students[MAX_COUNT_OF_STUDENTS]; // Pole študentov
|
|
|
|
int count = 0; // Počet študentov v poli
|
2025-03-08 16:55:33 +00:00
|
|
|
|
2025-03-08 17:06:21 +00:00
|
|
|
// Čítanie vstupných údajov
|
2025-03-08 16:55:33 +00:00
|
|
|
while (fgets(line, LINE_SIZE, stdin) != NULL && line[0] != '\n') {
|
2025-03-08 17:06:21 +00:00
|
|
|
int count_of_vote = 0; // Počet hlasov zo vstupu
|
|
|
|
char name[NAME_LENGTH] = {0}; // Premenná na meno študenta
|
2025-03-08 16:55:33 +00:00
|
|
|
|
2025-03-08 17:06:21 +00:00
|
|
|
line[strcspn(line, "\n")] = '\0'; // Odstráni znak nového riadka na konci
|
2025-03-08 16:55:33 +00:00
|
|
|
|
|
|
|
int i = 0;
|
2025-03-08 17:06:21 +00:00
|
|
|
// Spracovanie číselnej časti (počet hlasov)
|
2025-03-08 16:55:33 +00:00
|
|
|
while (isdigit(line[i])) {
|
|
|
|
count_of_vote = count_of_vote * 10 + (line[i] - '0');
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2025-03-08 17:06:21 +00:00
|
|
|
// Skontroluje, či po čísle je medzera
|
2025-03-08 16:55:33 +00:00
|
|
|
if (line[i] != ' ') {
|
2025-03-08 17:06:21 +00:00
|
|
|
break;
|
2025-03-08 16:55:33 +00:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
|
2025-03-08 17:06:21 +00:00
|
|
|
// Skopíruje meno študenta z riadka
|
2025-03-08 16:55:33 +00:00
|
|
|
strncpy(name, line + i, NAME_LENGTH - 1);
|
|
|
|
name[NAME_LENGTH - 1] = '\0';
|
|
|
|
|
2025-03-08 17:06:21 +00:00
|
|
|
// Hľadanie, či už daný študent existuje v poli
|
2025-03-08 16:55:33 +00:00
|
|
|
bool found = false;
|
|
|
|
for (int j = 0; j < count; ++j) {
|
|
|
|
if (strcmp(name, students[j].name) == 0) {
|
2025-03-08 17:06:21 +00:00
|
|
|
students[j].votes += count_of_vote; // Ak existuje, pripočítame hlasy
|
2025-03-08 16:55:33 +00:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-08 17:06:21 +00:00
|
|
|
// Ak študent neexistuje, pridáme ho do poľa
|
2025-03-08 16:55:33 +00:00
|
|
|
if (!found) {
|
|
|
|
students[count++] = createStudent(name, count_of_vote);
|
|
|
|
}
|
|
|
|
}
|
2025-03-08 17:10:35 +00:00
|
|
|
if(count == 0) {
|
|
|
|
printf("Nepodarilo nacitat nic\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2025-03-08 17:06:21 +00:00
|
|
|
// Zoradenie študentov podľa počtu hlasov a mena
|
2025-03-08 16:55:33 +00:00
|
|
|
qsort(students, count, sizeof(Student), compare_students);
|
|
|
|
|
2025-03-08 17:06:21 +00:00
|
|
|
// Výpis výsledkov
|
2025-03-08 16:55:33 +00:00
|
|
|
printf("Vysledky:\n");
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
printf("%d %s\n", students[i].votes, students[i].name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|