zmeny
This commit is contained in:
parent
8b0fcc38cf
commit
19d91f2d97
@ -6,72 +6,86 @@
|
||||
|
||||
#define MAX_COUNT_OF_STUDENTS 1000
|
||||
#define NAME_LENGTH 100
|
||||
#define LINE_SIZE 250
|
||||
#define LINE_SIZE 250
|
||||
|
||||
// Struktúra na uloženie informácií o študentovi (meno a počet hlasov)
|
||||
typedef struct {
|
||||
char name[NAME_LENGTH];
|
||||
int votes;
|
||||
} Student;
|
||||
|
||||
// Funkcia na vytvorenie študenta a inicializáciu jeho údajov
|
||||
Student createStudent(const char name[], int votes) {
|
||||
Student student;
|
||||
strncpy(student.name, name, NAME_LENGTH - 1);
|
||||
student.name[NAME_LENGTH - 1] = '\0';
|
||||
student.votes = votes;
|
||||
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
|
||||
return student;
|
||||
}
|
||||
|
||||
// Porovnávacia funkcia pre qsort na zoradenie študentov
|
||||
int compare_students(const void *a, const void *b) {
|
||||
const Student *studentA = (const Student *)a;
|
||||
const Student *studentB = (const Student *)b;
|
||||
|
||||
// Najskôr porovnáme počet hlasov (zoradené zostupne)
|
||||
if (studentA->votes != studentB->votes) {
|
||||
return studentB->votes - studentA->votes;
|
||||
}
|
||||
|
||||
// Ak majú rovnaký počet hlasov, zoradíme podľa mena abecedne
|
||||
return strcmp(studentA->name, studentB->name);
|
||||
}
|
||||
|
||||
int main() {
|
||||
char line[LINE_SIZE];
|
||||
Student students[MAX_COUNT_OF_STUDENTS];
|
||||
int count = 0;
|
||||
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
|
||||
|
||||
// Čítanie vstupných údajov
|
||||
while (fgets(line, LINE_SIZE, stdin) != NULL && line[0] != '\n') {
|
||||
int count_of_vote = 0;
|
||||
char name[NAME_LENGTH] = {0};
|
||||
int count_of_vote = 0; // Počet hlasov zo vstupu
|
||||
char name[NAME_LENGTH] = {0}; // Premenná na meno študenta
|
||||
|
||||
line[strcspn(line, "\n")] = '\0';
|
||||
line[strcspn(line, "\n")] = '\0'; // Odstráni znak nového riadka na konci
|
||||
|
||||
int i = 0;
|
||||
// Spracovanie číselnej časti (počet hlasov)
|
||||
while (isdigit(line[i])) {
|
||||
count_of_vote = count_of_vote * 10 + (line[i] - '0');
|
||||
i++;
|
||||
}
|
||||
|
||||
// Skontroluje, či po čísle je medzera
|
||||
if (line[i] != ' ') {
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
|
||||
// Skopíruje meno študenta z riadka
|
||||
strncpy(name, line + i, NAME_LENGTH - 1);
|
||||
name[NAME_LENGTH - 1] = '\0';
|
||||
|
||||
// Hľadanie, či už daný študent existuje v poli
|
||||
bool found = false;
|
||||
for (int j = 0; j < count; ++j) {
|
||||
if (strcmp(name, students[j].name) == 0) {
|
||||
students[j].votes += count_of_vote;
|
||||
students[j].votes += count_of_vote; // Ak existuje, pripočítame hlasy
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Ak študent neexistuje, pridáme ho do poľa
|
||||
if (!found) {
|
||||
students[count++] = createStudent(name, count_of_vote);
|
||||
}
|
||||
}
|
||||
|
||||
// Zoradenie študentov podľa počtu hlasov a mena
|
||||
qsort(students, count, sizeof(Student), compare_students);
|
||||
|
||||
// Výpis výsledkov
|
||||
printf("Vysledky:\n");
|
||||
for (int i = 0; i < count; i++) {
|
||||
printf("%d %s\n", students[i].votes, students[i].name);
|
||||
|
Loading…
Reference in New Issue
Block a user