pvjc25/du4/program.c

107 lines
2.1 KiB
C
Raw Normal View History

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