2024-03-13 09:55:11 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2024-03-20 15:27:29 +00:00
|
|
|
#include <time.h>
|
2024-03-13 09:55:11 +00:00
|
|
|
|
2024-03-20 14:57:31 +00:00
|
|
|
#define SIZE 100
|
2024-03-20 15:27:29 +00:00
|
|
|
#define MAX_VOTES 50
|
|
|
|
#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ý
|
|
|
|
}
|
|
|
|
|
|
|
|
// Funkcia na generovanie náhodného mena
|
|
|
|
void generate_random_name(char *name) {
|
|
|
|
static const char charset[] = "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
int len = rand() % (MAX_NAME_LENGTH - 1) + 1; // Dĺžka mena (1 až MAX_NAME_LENGTH)
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
name[i] = charset[rand() % (sizeof(charset) - 1)];
|
|
|
|
}
|
|
|
|
name[len] = '\0';
|
2024-03-20 14:57:31 +00:00
|
|
|
}
|
2024-03-20 13:52:38 +00:00
|
|
|
|
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:27:29 +00:00
|
|
|
srand(time(NULL)); // Inicializácia generátora náhodných čísel
|
|
|
|
|
|
|
|
// Generovanie náhodného vstupu mien a počtu hlasov
|
|
|
|
for (int i = 0; i < SIZE; i++) {
|
|
|
|
int votes = rand() % (MAX_VOTES + 1); // Náhodný počet hlasov (0 až MAX_VOTES)
|
|
|
|
char name[MAX_NAME_LENGTH];
|
|
|
|
generate_random_name(name); // Generovanie náhodného mena
|
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
|
|
|
}
|
|
|
|
|