pvjc25/du4/program.c
2025-03-11 12:49:16 +01:00

107 lines
2.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_M 100
#define MAX_S 100
typedef struct {
char meno[MAX_M];
int hlasy;
} Student;
int najdi_studenta(Student studenti[], int pocet, char *meno) {
for (int i = 0; i < pocet; i++) {
if (strcmp(studenti[i].meno, meno) == 0) {
return i;
}
}
return -1;
}
int porovnaj(const void *a, const void *b) {
Student *s1 = (Student *)a;
Student *s2 = (Student *)b;
if (s2->hlasy != s1->hlasy) {
return s2->hlasy - s1->hlasy;
}
return strcmp(s1->meno, s2->meno);
}
int je_cislo(const char *retazec) {
while (*retazec) {
if (!isdigit(*retazec)) {
return 0;
}
retazec++;
}
return 1;
}
int main() {
Student studenti[MAX_S];
int pocet_studentov = 0;
char meno[MAX_M];
char vstup[MAX_M + 10];
int hlasy;
int nacitane = 0;
while (fgets(vstup, sizeof(vstup), stdin) != NULL){
vstup[strcspn(vstup, "\n")] = 0;
if (strlen(vstup) == 0) {
break;
}
if (sscanf(vstup, "%d %[^\n]", &hlasy, meno) != 2 || hlasy <= 0) {
printf("Nepodarilo nacitat nic\n");
return 1;
}
nacitane = 1;
int index = najdi_studenta(studenti, pocet_studentov, meno);
if (index != -1) {
studenti[index].hlasy += hlasy;
} else {
if (pocet_studentov >= MAX_S) {
printf("Nepodarilo nacitat nic\n");
return 1;
}
strcpy(studenti[pocet_studentov].meno, meno);
studenti[pocet_studentov].hlasy = hlasy;
pocet_studentov++;
}
}
if (!nacitane) {
printf("Nepodarilo nacitat nic\n");
return 1;
}
qsort(studenti, pocet_studentov, sizeof(Student), porovnaj);
printf("Vysledky:\n");
for (int i = 0; i < pocet_studentov; i++) {
printf("%d %s\n", studenti[i].hlasy, studenti[i].meno);
}
return 0;
}