pvjc24/cv5/program.c

89 lines
2.4 KiB
C
Raw Normal View History

2024-03-13 09:55:11 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2024-03-20 14:57:31 +00:00
#define SIZE 100
2024-03-20 15:27:29 +00:00
#define MAX_NAME_LENGTH 20
2024-03-20 14:08:46 +00:00
2024-03-20 15:27:29 +00:00
// Definícia štruktúry pre uloženie jednej položky databázy
2024-03-20 14:57:31 +00:00
struct student {
2024-03-20 15:27:29 +00:00
char name[MAX_NAME_LENGTH];
2024-03-20 14:57:31 +00:00
int votes;
};
2024-03-13 09:55:11 +00:00
2024-03-20 15:27:29 +00:00
// Funkcia pre porovnanie dvoch záznamov študentov
2024-03-20 15:22:08 +00:00
int compare(const void *p1, const void *p2) {
const struct student *s1 = (const struct student *)p1;
const struct student *s2 = (const struct student *)p2;
// Porovnanie počtu hlasov
if (s1->votes != s2->votes) {
2024-03-20 15:27:29 +00:00
return s2->votes - s1->votes; // Zoradenie zostupne podľa počtu hlasov
2024-03-20 15:22:08 +00:00
} else {
2024-03-20 15:27:29 +00:00
// Ak majú rovnaký počet hlasov, zoradíme lexikograficky podľa mena
2024-03-20 15:22:08 +00:00
return strcmp(s1->name, s2->name);
}
2024-03-20 14:57:31 +00:00
}
2024-03-20 15:22:08 +00:00
2024-03-20 15:27:29 +00:00
// Funkcia na vyhľadanie študenta v databáze
2024-03-20 14:57:31 +00:00
int find_student(struct student *students, int size, const char *name) {
for (int i = 0; i < size; i++) {
if (strcmp(students[i].name, name) == 0) {
2024-03-20 15:27:29 +00:00
return i; // Študent nájdený
2024-03-20 13:52:38 +00:00
}
2024-03-13 09:55:11 +00:00
}
2024-03-20 15:27:29 +00:00
return -1; // Študent nenájdený
}
2024-03-20 15:22:08 +00:00
int main() {
struct student database[SIZE];
memset(database, 0, SIZE * sizeof(struct student));
int size = 0;
2024-03-20 15:29:47 +00:00
// Zadefinovaný vstup mien a počtu hlasov
char input[][MAX_NAME_LENGTH] = {
"2 Bardos Mrtakrys",
"1 Rita Umhi",
"1 Prylenn Alak",
"10 Lak'hi Elavorg",
"3 Prylenn Alak",
"3 Prylenn Alak",
"3 Prylenn Alak",
"1 Rita Umhi"
};
int input_size = sizeof(input) / sizeof(input[0]);
2024-03-20 15:27:29 +00:00
2024-03-20 15:29:47 +00:00
// Spracovanie vstupu
for (int i = 0; i < input_size; i++) {
2024-03-20 15:27:29 +00:00
char name[MAX_NAME_LENGTH];
2024-03-20 15:29:47 +00:00
int votes;
sscanf(input[i], "%d %s", &votes, name);
2024-03-20 15:22:08 +00:00
int idx = find_student(database, size, name);
if (idx == -1) {
2024-03-20 15:27:29 +00:00
// Študent nie je v databáze, pridáme ho
2024-03-20 15:22:08 +00:00
if (size >= SIZE) {
2024-03-20 15:27:29 +00:00
fprintf(stderr, "Prekročený limit databázy\n");
2024-03-20 15:22:08 +00:00
return 1;
}
strcpy(database[size].name, name);
database[size].votes = votes;
size++;
} else {
2024-03-20 15:27:29 +00:00
// Študent je v databáze, pripočítame hlasy
2024-03-20 15:22:08 +00:00
database[idx].votes += votes;
}
}
2024-03-20 15:27:29 +00:00
// Zoradenie databázy
2024-03-20 15:22:08 +00:00
qsort(database, size, sizeof(struct student), compare);
2024-03-20 15:27:29 +00:00
// Výpis databázy
2024-03-20 15:22:08 +00:00
for (int i = 0; i < size; i++) {
printf("%d %s\n", database[i].votes, database[i].name);
}
return 0;
2024-03-13 09:55:11 +00:00
}