This commit is contained in:
Denis Landa 2025-03-21 15:36:52 +01:00
parent c15f8256ff
commit efec7c9f44

View File

@ -9,20 +9,21 @@ struct student {
int votes; int votes;
}; };
int find_student(struct student* students, int size, const char* name) { int najdi_studenta(struct student databaza[], int pocet, char meno[]) {
for (int i = 0; i < size; i++) { int i;
if (strcmp(students[i].name, name) == 0) { for (i = 0; i < pocet; i++) {
if (strcmp(databaza[i].name, meno) == 0) {
return i; return i;
} }
} }
return -1; return -1;
} }
int compare(const void* p1, const void* p2) { int porovnaj(const void *a, const void *b) {
struct student* s1 = (struct student*)p1; struct student *s1 = (struct student *)a;
struct student* s2 = (struct student*)p2; struct student *s2 = (struct student *)b;
if (s2->votes != s1->votes) { if (s1->votes != s2->votes) {
return s2->votes - s1->votes; return s2->votes - s1->votes;
} }
@ -31,59 +32,62 @@ int compare(const void* p1, const void* p2) {
int main() { int main() {
struct student databaza[SIZE]; struct student databaza[SIZE];
int pocet_studentov = 0;
int valid_input = 0;
memset(databaza, 0, SIZE * sizeof(struct student)); memset(databaza, 0, SIZE * sizeof(struct student));
int size = 0;
int any_valid_input = 0;
char line[SIZE]; char riadok[SIZE];
char meno[SIZE];
int hlasy;
while (fgets(line, SIZE, stdin) != NULL); while (fgets(riadok, SIZE, stdin) != NULL) {
char* newline = strchr(line, '\n'); int dlzka = strlen(riadok);
if (newline) *newline = '\0'; if (dlzka > 0 && riadok[dlzka-1] == '\n') {
riadok[dlzka-1] = '\0';
}
char* end = NULL; char *koniec;
int value = strtol(line, &end, 10); hlasy = strtol(riadok, &koniec, 10);
if (value <= 0) { if (hlasy <= 0) {
break; break;
} }
if (*end != ' ') { if (*koniec != ' ') {
break; break;
} }
char* zaciatok_mena = end + 1; char *meno_start = koniec + 1;
int velkost_mena = strlen(zaciatok_mena);
if (velkost_mena <= 0) { if (strlen(meno_start) == 0) {
break; break;
} }
char name[SIZE]; strcpy(meno, meno_start);
memset(name, 0, SIZE);
memcpy(name, zaciatok_mena, velkost_mena);
int id = find_student(databaza, size, name); int pozicia = najdi_studenta(databaza, pocet_studentov, meno);
if (id < 0) { if (pozicia == -1) {
strcpy(databaza[size].name, name); strcpy(databaza[pocet_studentov].name, meno);
databaza[size].votes = value; databaza[pocet_studentov].votes = hlasy;
size++; pocet_studentov++;
} else { } else {
databaza[id].votes += value; databaza[pozicia].votes += hlasy;
} }
any_valid_input = 1; valid_input = 1;
} }
if (!any_valid_input) { if (!valid_input) {
printf("Chyba: Nepodarilo sa načítať žiadny platný záznam.\n"); printf("Chyba: Nepodarilo sa načítať žiadny platný záznam.\n");
return 1; return 1;
} }
qsort(databaza, size, sizeof(struct student), compare); qsort(databaza, pocet_studentov, sizeof(struct student), porovnaj);
printf("Vysledky:\n"); printf("Vysledky:\n");
for (int i = 0; i < size; i++) { int i;
for (i = 0; i < pocet_studentov; i++) {
printf("%d %s\n", databaza[i].votes, databaza[i].name); printf("%d %s\n", databaza[i].votes, databaza[i].name);
} }