2024-03-21 22:47:06 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2024-03-21 23:01:55 +00:00
|
|
|
#define SIZE 100
|
2024-03-21 22:47:06 +00:00
|
|
|
|
2024-03-21 23:01:55 +00:00
|
|
|
typedef struct {
|
2024-03-21 23:04:41 +00:00
|
|
|
char* meno;
|
|
|
|
int hlasov;
|
2024-03-21 23:01:55 +00:00
|
|
|
} Student;
|
2024-03-21 22:47:06 +00:00
|
|
|
|
2024-03-21 23:09:53 +00:00
|
|
|
int find_student_index(Student* students, int num_students, const char* meno) {
|
|
|
|
for (int i = 0; i < num_students; i++) {
|
|
|
|
if (strcmp(students[i].meno, meno) == 0) {
|
|
|
|
return i; // Nájdený študent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1; // Študent neexistuje
|
|
|
|
}
|
|
|
|
|
2024-03-21 22:47:06 +00:00
|
|
|
int main() {
|
2024-03-21 23:01:55 +00:00
|
|
|
char line[SIZE];
|
2024-03-21 23:04:41 +00:00
|
|
|
int num_students = 0;
|
|
|
|
Student* students = NULL;
|
2024-03-21 23:01:55 +00:00
|
|
|
|
2024-03-21 23:04:41 +00:00
|
|
|
while (fgets(line, SIZE, stdin) != NULL) {
|
2024-03-21 23:06:32 +00:00
|
|
|
char* endptr;
|
|
|
|
int hlasov = strtol(line, &endptr, 10);
|
|
|
|
if (endptr == line || *endptr != ' ' || hlasov < 0) {
|
2024-03-21 22:58:42 +00:00
|
|
|
printf("CHYBA: Neplatny zapis na riadku.\n");
|
2024-03-21 23:04:41 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2024-03-21 23:06:32 +00:00
|
|
|
endptr++; // Preskočenie medzery
|
|
|
|
|
|
|
|
// Preskočenie medzier za počtom hlasov
|
|
|
|
while (*endptr == ' ') {
|
|
|
|
endptr++;
|
|
|
|
}
|
|
|
|
|
2024-03-21 23:07:54 +00:00
|
|
|
// Zistenie dĺžky mena
|
|
|
|
int len = strlen(endptr);
|
|
|
|
if (len == 0 || endptr[len - 1] == '\n') {
|
|
|
|
printf("CHYBA: Neplatny zapis na riadku.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-03-21 23:06:32 +00:00
|
|
|
// Alokácia pamäte pre meno študenta
|
2024-03-21 23:07:54 +00:00
|
|
|
char* meno = malloc(len + 1);
|
2024-03-21 23:06:32 +00:00
|
|
|
if (meno == NULL) {
|
2024-03-21 23:04:41 +00:00
|
|
|
printf("CHYBA: Nepodarilo sa alokovať pamäť.\n");
|
|
|
|
return 1;
|
2024-03-21 22:58:42 +00:00
|
|
|
}
|
2024-03-21 23:01:55 +00:00
|
|
|
|
2024-03-21 23:06:32 +00:00
|
|
|
// Kopírovanie mena
|
|
|
|
strcpy(meno, endptr);
|
|
|
|
|
2024-03-21 23:07:54 +00:00
|
|
|
// Odstránenie konca riadka z mena
|
|
|
|
meno[len - 1] = '\0';
|
|
|
|
|
2024-03-21 23:09:53 +00:00
|
|
|
// Zistiť, či študent už existuje v databáze
|
|
|
|
int index = find_student_index(students, num_students, meno);
|
|
|
|
if (index == -1) {
|
|
|
|
// Študent neexistuje, pridáme ho do databázy
|
|
|
|
students = realloc(students, (num_students + 1) * sizeof(Student));
|
|
|
|
if (students == NULL) {
|
|
|
|
printf("CHYBA: Nepodarilo sa alokovať pamäť.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
students[num_students].meno = meno;
|
|
|
|
students[num_students].hlasov = hlasov;
|
|
|
|
num_students++;
|
|
|
|
} else {
|
|
|
|
// Študent existuje, zvýšime počet hlasov
|
|
|
|
students[index].hlasov += hlasov;
|
|
|
|
free(meno); // Uvoľníme meno, pretože sme ho nevyužili
|
2024-03-21 23:04:41 +00:00
|
|
|
}
|
2024-03-21 22:47:06 +00:00
|
|
|
}
|
2024-03-21 23:01:55 +00:00
|
|
|
|
|
|
|
// Výpis výsledkov
|
2024-03-21 23:04:41 +00:00
|
|
|
printf("Vysledky:\n");
|
|
|
|
for (int i = 0; i < num_students; i++) {
|
|
|
|
printf("%d %s\n", students[i].hlasov, students[i].meno);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uvoľnenie pamäte
|
|
|
|
for (int i = 0; i < num_students; i++) {
|
|
|
|
free(students[i].meno);
|
2024-03-21 22:58:42 +00:00
|
|
|
}
|
2024-03-21 23:04:41 +00:00
|
|
|
free(students);
|
2024-03-21 23:01:55 +00:00
|
|
|
|
2024-03-21 22:47:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|