2024-03-21 22:55:24 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2024-03-21 23:04:31 +00:00
|
|
|
#define SIZE 100
|
2024-03-21 22:55:24 +00:00
|
|
|
|
2024-03-21 23:04:31 +00:00
|
|
|
struct student {
|
|
|
|
char name[SIZE];
|
|
|
|
int votes;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Funkcia pre porovnanie dvoch študentov pri triedení
|
|
|
|
int compare(const void* p1, const void* p2) {
|
|
|
|
const struct student* s1 = (const struct student*)p1;
|
|
|
|
const struct student* s2 = (const struct student*)p2;
|
2024-03-21 22:55:24 +00:00
|
|
|
|
2024-03-21 23:04:31 +00:00
|
|
|
// Porovnanie podľa počtu hlasov
|
2024-03-21 23:13:18 +00:00
|
|
|
int result = s2->votes - s1->votes;
|
|
|
|
if (result == 0) {
|
|
|
|
// Ak majú rovnaký počet hlasov, zoradíme lexikograficky podľa mena
|
|
|
|
return strcmp(s1->name, s2->name);
|
2024-03-21 22:55:24 +00:00
|
|
|
}
|
2024-03-21 23:13:18 +00:00
|
|
|
return result;
|
2024-03-21 22:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2024-03-21 23:13:18 +00:00
|
|
|
struct student students[SIZE];
|
|
|
|
memset(students, 0, SIZE * sizeof(struct student));
|
2024-03-21 23:04:31 +00:00
|
|
|
int size = 0;
|
|
|
|
|
|
|
|
char line[SIZE];
|
|
|
|
char name[SIZE];
|
|
|
|
int votes;
|
|
|
|
|
2024-03-21 23:15:09 +00:00
|
|
|
//printf("Enter votes for 'Student of the Year' contest (or 'q' to quit):\n");
|
2024-03-21 23:13:18 +00:00
|
|
|
|
|
|
|
while (fgets(line, SIZE, stdin) != NULL) {
|
2024-03-21 23:04:31 +00:00
|
|
|
// Kontrola ukončenia vstupu
|
|
|
|
if (strcmp(line, "q\n") == 0 || strcmp(line, "Q\n") == 0) {
|
|
|
|
break;
|
2024-03-21 22:55:24 +00:00
|
|
|
}
|
|
|
|
|
2024-03-21 23:07:54 +00:00
|
|
|
// Načítanie počtu hlasov a mena zo vstupu
|
2024-03-21 23:11:14 +00:00
|
|
|
if (sscanf(line, "%d %[^\n]", &votes, name) != 2) {
|
2024-03-21 23:15:09 +00:00
|
|
|
//printf("Invalid input format. Exiting.\n");
|
2024-03-21 23:13:18 +00:00
|
|
|
return 1;
|
2024-03-21 22:55:24 +00:00
|
|
|
}
|
|
|
|
|
2024-03-21 23:04:31 +00:00
|
|
|
// Vyhľadanie študenta v databáze
|
2024-03-21 23:13:18 +00:00
|
|
|
int index = -1;
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
if (strcmp(students[i].name, name) == 0) {
|
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ak študent nie je v databáze, pridáme ho
|
2024-03-21 23:04:31 +00:00
|
|
|
if (index == -1) {
|
|
|
|
if (size < SIZE) {
|
2024-03-21 23:13:18 +00:00
|
|
|
strcpy(students[size].name, name);
|
|
|
|
students[size].votes = votes;
|
2024-03-21 23:04:31 +00:00
|
|
|
size++;
|
|
|
|
} else {
|
2024-03-21 23:13:18 +00:00
|
|
|
printf("Database is full. Exiting.\n");
|
|
|
|
return 1;
|
2024-03-21 23:04:31 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-03-21 23:13:18 +00:00
|
|
|
// Ak študent už existuje, zvýšime počet hlasov
|
|
|
|
students[index].votes += votes;
|
2024-03-21 22:55:24 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-21 23:04:31 +00:00
|
|
|
|
2024-03-21 23:13:18 +00:00
|
|
|
// Triedenie databázy podľa počtu hlasov a mena
|
|
|
|
qsort(students, size, sizeof(struct student), compare);
|
2024-03-21 23:04:31 +00:00
|
|
|
|
2024-03-21 23:13:18 +00:00
|
|
|
// Výpis výsledkov
|
2024-03-21 23:11:14 +00:00
|
|
|
printf("\nResults:\n");
|
2024-03-21 23:04:31 +00:00
|
|
|
for (int i = 0; i < size; i++) {
|
2024-03-21 23:13:18 +00:00
|
|
|
printf("%d %s\n", students[i].votes, students[i].name);
|
2024-03-21 22:55:24 +00:00
|
|
|
}
|
2024-03-21 23:04:31 +00:00
|
|
|
|
2024-03-21 22:55:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|