59 lines
1.4 KiB
C
59 lines
1.4 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define SIZE 100
|
|
|
|
typedef struct {
|
|
char* meno;
|
|
int hlasov;
|
|
} Student;
|
|
|
|
int main() {
|
|
char line[SIZE];
|
|
int num_students = 0;
|
|
Student* students = NULL;
|
|
|
|
while (fgets(line, SIZE, stdin) != NULL) {
|
|
int hlasov;
|
|
char meno[SIZE];
|
|
if (sscanf(line, "%d %99[^\n]", &hlasov, meno) != 2) {
|
|
printf("CHYBA: Neplatny zapis na riadku.\n");
|
|
return 1;
|
|
}
|
|
|
|
// Reallokácia pamäte pre ďalšieho študenta
|
|
students = realloc(students, (num_students + 1) * sizeof(Student));
|
|
if (students == NULL) {
|
|
printf("CHYBA: Nepodarilo sa alokovať pamäť.\n");
|
|
return 1;
|
|
}
|
|
|
|
// Alokácia pamäte pre meno študenta a kópia mena
|
|
students[num_students].meno = malloc(strlen(meno) + 1);
|
|
if (students[num_students].meno == NULL) {
|
|
printf("CHYBA: Nepodarilo sa alokovať pamäť.\n");
|
|
return 1;
|
|
}
|
|
strcpy(students[num_students].meno, meno);
|
|
students[num_students].hlasov = hlasov;
|
|
|
|
num_students++;
|
|
}
|
|
|
|
// Výpis výsledkov
|
|
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);
|
|
}
|
|
free(students);
|
|
|
|
return 0;
|
|
}
|
|
|