2024-03-21 21:01:53 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
typedef struct student {
|
2024-03-22 00:36:40 +00:00
|
|
|
char *name;
|
|
|
|
int vote_count;
|
2024-03-21 21:01:53 +00:00
|
|
|
} student_t;
|
|
|
|
|
|
|
|
int compare_students(const void *a, const void *b) {
|
2024-03-22 00:36:40 +00:00
|
|
|
const student_t *student_a = (const student_t *)a;
|
|
|
|
const student_t *student_b = (const student_t *)b;
|
2024-03-21 21:01:53 +00:00
|
|
|
|
2024-03-22 00:36:40 +00:00
|
|
|
if (student_a->vote_count > student_b->vote_count) {
|
|
|
|
return -1;
|
|
|
|
} else if (student_a->vote_count < student_b->vote_count) {
|
|
|
|
return 1;
|
|
|
|
}
|
2024-03-21 21:01:53 +00:00
|
|
|
|
2024-03-22 00:36:40 +00:00
|
|
|
return strcmp(student_a->name, student_b->name);
|
2024-03-21 21:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2024-03-22 00:36:40 +00:00
|
|
|
student_t *students = NULL;
|
|
|
|
int students_capacity = 0;
|
|
|
|
int student_count = 0;
|
2024-03-22 00:22:30 +00:00
|
|
|
|
2024-03-22 00:36:40 +00:00
|
|
|
char line[1024];
|
2024-03-22 00:39:21 +00:00
|
|
|
int total_votes = 0;
|
|
|
|
|
2024-03-22 00:36:40 +00:00
|
|
|
while (fgets(line, sizeof(line), stdin)) {
|
|
|
|
int vote_count;
|
|
|
|
char name[1024];
|
|
|
|
sscanf(line, "%d %[^\n]", &vote_count, name);
|
2024-03-22 00:22:30 +00:00
|
|
|
|
2024-03-22 00:39:21 +00:00
|
|
|
if (student_count == 0) {
|
|
|
|
total_votes = vote_count;
|
2024-03-22 00:36:40 +00:00
|
|
|
} else {
|
2024-03-22 00:41:10 +00:00
|
|
|
int existing_student_index = -1;
|
|
|
|
for (int i = 0; i < student_count; i++) {
|
|
|
|
if (strcmp(students[i].name, name) == 0) {
|
|
|
|
existing_student_index = i;
|
|
|
|
break;
|
2024-03-22 00:36:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 00:41:10 +00:00
|
|
|
if (existing_student_index != -1) {
|
|
|
|
students[existing_student_index].vote_count += vote_count;
|
|
|
|
} else {
|
|
|
|
if (student_count >= students_capacity) {
|
|
|
|
students_capacity = (students_capacity + 1) * 2;
|
|
|
|
student_t *new_students = realloc(students, students_capacity * sizeof(student_t));
|
|
|
|
if (new_students == NULL) {
|
|
|
|
fprintf(stderr, "Error: Memory reallocation failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
students = new_students;
|
|
|
|
}
|
|
|
|
|
|
|
|
students[student_count].name = strdup(name);
|
|
|
|
students[student_count].vote_count = vote_count;
|
|
|
|
student_count++;
|
|
|
|
}
|
2024-03-22 00:32:24 +00:00
|
|
|
}
|
2024-03-22 00:29:34 +00:00
|
|
|
}
|
2024-03-22 00:22:30 +00:00
|
|
|
|
2024-03-22 00:41:10 +00:00
|
|
|
if (student_count == 0) {
|
|
|
|
printf("Žiadni študenti.\n");
|
|
|
|
} else if (student_count == 1) {
|
2024-03-22 00:39:21 +00:00
|
|
|
printf("Vysledky:\n");
|
|
|
|
printf("%d %s\n", total_votes, students[0].name);
|
|
|
|
} else {
|
|
|
|
qsort(students, student_count, sizeof(student_t), compare_students);
|
|
|
|
printf("Vysledky:\n");
|
|
|
|
for (int i = 0; i < student_count; i++) {
|
|
|
|
printf("%d %s\n", students[i].vote_count, students[i].name);
|
|
|
|
}
|
2024-03-22 00:35:12 +00:00
|
|
|
}
|
2024-03-22 00:22:30 +00:00
|
|
|
|
2024-03-22 00:36:40 +00:00
|
|
|
for (int i = 0; i < student_count; i++) {
|
|
|
|
free(students[i].name);
|
|
|
|
}
|
|
|
|
free(students);
|
2024-03-22 00:22:30 +00:00
|
|
|
|
2024-03-22 00:36:40 +00:00
|
|
|
return 0;
|
2024-03-22 00:29:34 +00:00
|
|
|
}
|