pvjc26/du2/program.c
2026-03-11 12:58:14 +00:00

68 lines
2.0 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
struct student {
char name[SIZE];
int votes;
};
int compare(const void* p1, const void* p2) { //bolo v zadani na webe
struct student* s1 = (struct student*)p1;
struct student* s2 = (struct student*)p2;
// s1->votes je počet hlasov
// s1->name je meno študenta
if (s1->votes != s2->votes) {
return s2->votes - s1->votes;
}
//ak su rozne hlasy, tak vratim indikator ako ich zoradit podla hodnoty
else {
return strcmp (s1->name, s2->name);
}
//ak su hlasy rovnake, zistim, ktore meno je hore alebo dole abecedne
}
int main() {
//premenne
struct student studenti[SIZE];
int hlasy = 0;
int existuje = 0;
char vstup[SIZE];
char name[SIZE];
int velkost_zoznamu = 0; //pocet studentov
while (fgets (vstup, SIZE, stdin) != NULL) {
//citat input, kontrola ci existuje, pridat hlasy existujucemu zaznamu
if (sscanf(vstup, "%d %[^\n]", &hlasy, name) != 2) {
printf("Nepodarilo sa nacitat nic\n");
break;
}
existuje = 0;
for (int i = 0; i < velkost_zoznamu; i++) {
if (strcmp (name, studenti[i].name) == 0) {
existuje = 1;
studenti[i].votes += hlasy;
break;
}
}
if (!existuje) {
strcpy (studenti[velkost_zoznamu].name, name);
studenti[velkost_zoznamu].votes = hlasy;
velkost_zoznamu++;
}
}
//zoradit abecedne, potom zoradit mena podla poctov hlasov, poposuvat cisla hore
qsort(studenti, velkost_zoznamu, sizeof (struct student), compare);
//vystup vo formate printf("Vysledky:\n"); for i=0 to pocet_studentov
//printf("%d %s\n"student[i].votes, student[i].name[i]);
printf("Vysledky:\n");
for (int i = 0; i < velkost_zoznamu; i++) printf ("%d %s\n", studenti[i].votes, studenti[i].name);
return 0;
}