83 lines
2.2 KiB
C
83 lines
2.2 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define SIZE 100
|
|
|
|
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;
|
|
|
|
// Porovnanie podľa počtu hlasov
|
|
return s2->votes - s1->votes;
|
|
}
|
|
|
|
// Funkcia na vyhľadanie študenta v databáze
|
|
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) {
|
|
return i; // Nájdený študent
|
|
}
|
|
}
|
|
return -1; // Študent nenájdený
|
|
}
|
|
|
|
int main() {
|
|
struct student database[SIZE];
|
|
memset(database, 0, SIZE * sizeof(struct student));
|
|
int size = 0;
|
|
|
|
char line[SIZE];
|
|
char name[SIZE];
|
|
int votes;
|
|
|
|
while (1) {
|
|
printf("Enter votes and student name (or 'q' to quit): ");
|
|
fgets(line, SIZE, stdin);
|
|
|
|
// Kontrola ukončenia vstupu
|
|
if (strcmp(line, "q\n") == 0 || strcmp(line, "Q\n") == 0) {
|
|
break;
|
|
}
|
|
|
|
// Načítanie počtu hlasov a mena zo vstupu
|
|
if (sscanf(line, "%d %s", &votes, name) != 2) {
|
|
printf("Invalid input. Please try again.\n");
|
|
continue;
|
|
}
|
|
|
|
// Vyhľadanie študenta v databáze
|
|
int index = find_student(database, size, name);
|
|
if (index == -1) {
|
|
// Študent nenájdený, pridáme ho do databázy
|
|
if (size < SIZE) {
|
|
strcpy(database[size].name, name);
|
|
database[size].votes = votes;
|
|
size++;
|
|
} else {
|
|
printf("Database is full.\n");
|
|
}
|
|
} else {
|
|
// Študent nájdený, zvýšime počet hlasov
|
|
database[index].votes += votes;
|
|
}
|
|
}
|
|
|
|
// Triedenie databázy podľa počtu hlasov
|
|
qsort(database, size, sizeof(struct student), compare);
|
|
|
|
// Výpis triedenej databázy
|
|
printf("\nSorted database:\n");
|
|
for (int i = 0; i < size; i++) {
|
|
printf("%s: %d votes\n", database[i].name, database[i].votes);
|
|
}
|
|
|
|
return 0;
|
|
}
|