This commit is contained in:
Kamil Gejdoš 2026-03-11 12:46:50 +00:00
parent 486e393121
commit fb6aef1e0f

View File

@ -4,6 +4,17 @@
#define SIZE 100
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 retutn strcmp (s1->name, s2->name);
//ak su hlasy rovnake, zistim, ktore meno je hore alebo dole abecedne
}
struct student {
char name[SIZE];
int votes;
@ -15,11 +26,11 @@ int main() {
struct student studenti[SIZE];
int hlasy = 0;
int existuje = 0;
char vstup[LINE_SIZE];
char name[LINE_SIZE];
char vstup[SIZE];
char name[SIZE];
int velkost_zoznamu = 0; //pocet studentov
while (fgets (vstup, LINE_SIZE, stdin) != NULL) {
while (fgets (vstup, SIZE, stdin) != NULL) {
//citat input, kontrola ci existuje, pridat hlasy existujucemu zaznamu
if (sscanf(vstup, "%d %[^\n]", &hlasy, name) != 2) break;
existuje = 0;
@ -36,10 +47,15 @@ int main() {
velkost_zoznamu++;
}
}
//zoradit abecedne, potom zoradit mena podla poctov hlasov, poposuvat cisla hore
//vystup vo formate printf("Vysledky:\n"); for i=0 to pocet_studentov printf("%d %c\n"student[i].votes, student[i].name[j]);
qsort(studenti, velkost_zoznamu, velkost_zoznamu - 1, 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;
}