oooodchod
This commit is contained in:
parent
198acd7247
commit
8024ab010a
@ -2,59 +2,52 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define SIZE 100
|
#define MAX_STUDENTS 100
|
||||||
|
#define MAX_NAME_LENGTH 100
|
||||||
|
|
||||||
struct student {
|
// Štruktúra pre reprezentáciu študenta
|
||||||
char name[SIZE];
|
struct Student {
|
||||||
|
char name[MAX_NAME_LENGTH];
|
||||||
int votes;
|
int votes;
|
||||||
};
|
};
|
||||||
|
|
||||||
void initialize_database(struct student* database, int size, char* names[], int* votes) {
|
// Porovnávacia funkcia pre qsort
|
||||||
for (int i = 0; i < size; i++) {
|
|
||||||
strcpy(database[i].name, names[i]);
|
|
||||||
database[i].votes = votes[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pomocná funkcia na porovnanie dvoch študentov
|
|
||||||
int compare_students(const void* a, const void* b) {
|
int compare_students(const void* a, const void* b) {
|
||||||
const struct student* studentA = (const struct student*)a;
|
const struct Student* studentA = (const struct Student*)a;
|
||||||
const struct student* studentB = (const struct student*)b;
|
const struct Student* studentB = (const struct Student*)b;
|
||||||
|
|
||||||
// Ak majú študenti rôzny počet hlasov, porovnáme ich podľa počtu hlasov
|
// Porovnanie podľa počtu hlasov
|
||||||
if (studentA->votes != studentB->votes) {
|
if (studentA->votes != studentB->votes) {
|
||||||
return studentB->votes - studentA->votes; // Zoradenie zostupne podľa počtu hlasov
|
return studentB->votes - studentA->votes; // Zotriedenie zostupne podľa počtu hlasov
|
||||||
} else {
|
} else {
|
||||||
// Ak majú rovnaký počet hlasov, porovnáme ich abecedne podľa mena
|
// Ak majú rovnaký počet hlasov, zotriedenie podľa mena
|
||||||
return strcmp(studentA->name, studentB->name);
|
return strcmp(studentA->name, studentB->name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
struct student database[SIZE];
|
// Databáza študentov s preddefinovanými hodnotami
|
||||||
memset(database, 0, SIZE * sizeof(struct student));
|
struct Student students[MAX_STUDENTS] = {
|
||||||
int size = 10; // Veľkosť databázy mien študentov
|
{"Bardos Mrtakrys", 2},
|
||||||
char* names[10] = {"Bardos Mrtakrys", "Rita Umhi", "Prylenn Alak", "Lak'hi Elavorg", "Prylenn Alak", "Prylenn Alak", "Prylenn Alak", "Rita Umhi", "Terian Dis"};
|
{"Rita Umhi", 1},
|
||||||
int votes[10] = {2, 1, 1, 9, 3, 3, 3, 1, 10}; // Počet hlasov pre každého študenta
|
{"Prylenn Alak", 1},
|
||||||
|
{"Lak'hi Elavorg", 10},
|
||||||
|
{"Prylenn Alak", 3},
|
||||||
|
{"Prylenn Alak", 3},
|
||||||
|
{"Prylenn Alak", 3},
|
||||||
|
{"Rita Umhi", 1}
|
||||||
|
};
|
||||||
|
int num_students = 8; // Počet študentov v databáze
|
||||||
|
|
||||||
initialize_database(database, size, names, votes); // Inicializácia databázy mien študentov
|
// Zoradenie študentov podľa počtu hlasov a mena
|
||||||
|
qsort(students, num_students, sizeof(struct Student), compare_students);
|
||||||
|
|
||||||
// Zoradenie databázy študentov podľa počtu hlasov a abecedne
|
// Výpis zoradeného zoznamu študentov
|
||||||
qsort(database, size, sizeof(struct student), compare_students);
|
printf("Výsledky:\n");
|
||||||
|
for (int i = 0; i < num_students; i++) {
|
||||||
// Výpis zoradenej databázy
|
printf("%d %s\n", students[i].votes, students[i].name);
|
||||||
printf("Zoradeny zoznam studentov podla poctu hlasov a abecedne:\n");
|
|
||||||
for (int i = 0; i < size; i++) {
|
|
||||||
printf("%d %s\n", database[i].votes, database[i].name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Výpočet a výpis celkového počtu hlasov
|
|
||||||
int total_votes = 0;
|
|
||||||
for (int i = 0; i < size; i++) {
|
|
||||||
total_votes += database[i].votes;
|
|
||||||
}
|
|
||||||
printf("Celkovy pocet ziskanych hlasov: %d\n", total_votes);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user