funguje
This commit is contained in:
parent
5423e79f36
commit
3ec0e85fbd
@ -2,73 +2,55 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_SIZE 100
|
||||
#define MAX_STUDENTS 100
|
||||
#define MAX_NAME_LENGTH 50
|
||||
|
||||
// Štruktúra pre reprezentáciu študenta
|
||||
struct Student {
|
||||
char name[MAX_SIZE];
|
||||
char name[MAX_NAME_LENGTH];
|
||||
int votes;
|
||||
};
|
||||
|
||||
// Porovnávacia funkcia pre triedenie študentov
|
||||
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) {
|
||||
return s2->votes - s1->votes; // Zoradenie zostupne podľa hlasov
|
||||
int read_students(struct Student students[], int max_students) {
|
||||
int count = 0;
|
||||
while (count < max_students) {
|
||||
int votes;
|
||||
char name[MAX_NAME_LENGTH];
|
||||
if (scanf("%d %[^\n]", &votes, name) != 2) {
|
||||
break; // Koniec vstupu alebo chyba
|
||||
}
|
||||
strcpy(students[count].name, name);
|
||||
students[count].votes = votes;
|
||||
count++;
|
||||
}
|
||||
// Ak majú rovnaký počet hlasov, zoradíme lexikograficky podľa mena
|
||||
return strcmp(s1->name, s2->name);
|
||||
return count;
|
||||
}
|
||||
|
||||
// Načíta študentov zo štandardného vstupu do pola
|
||||
int read_students(struct Student students[], int max_students) {
|
||||
int num_students = 0;
|
||||
char line[MAX_SIZE];
|
||||
while (fgets(line, sizeof(line), stdin) != NULL) {
|
||||
char name[MAX_SIZE];
|
||||
int votes;
|
||||
// Načítame počet hlasov
|
||||
char *endptr;
|
||||
votes = (int)strtol(line, &endptr, 10);
|
||||
// Preskočíme medzeru
|
||||
endptr++;
|
||||
// Načítame meno študenta
|
||||
strcpy(name, endptr);
|
||||
// Prejdeme všetkých študentov a ak nájdeme rovnaké meno, pripočítame hlasy
|
||||
int found = 0;
|
||||
for (int i = 0; i < num_students; i++) {
|
||||
if (strcmp(students[i].name, name) == 0) {
|
||||
students[i].votes += votes;
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Ak sme nenašli rovnaké meno, pridáme nového študenta
|
||||
if (!found) {
|
||||
strcpy(students[num_students].name, name);
|
||||
students[num_students].votes = votes;
|
||||
num_students++;
|
||||
if (num_students >= max_students) {
|
||||
break; // Prekročený maximálny počet študentov
|
||||
}
|
||||
}
|
||||
int compare(const void *p1, const void *p2) {
|
||||
struct Student *s1 = (struct Student *)p1;
|
||||
struct Student *s2 = (struct Student *)p2;
|
||||
// Porovnanie podľa počtu hlasov, v prípade rovnakého počtu podľa mena
|
||||
if (s1->votes != s2->votes) {
|
||||
return s2->votes - s1->votes;
|
||||
} else {
|
||||
return strcmp(s1->name, s2->name);
|
||||
}
|
||||
return num_students;
|
||||
}
|
||||
|
||||
int main() {
|
||||
struct Student students[MAX_STUDENTS];
|
||||
int num_students = read_students(students, MAX_STUDENTS);
|
||||
int count = read_students(students, MAX_STUDENTS);
|
||||
|
||||
// Zoradíme študentov podľa počtu hlasov a mena
|
||||
qsort(students, num_students, sizeof(struct Student), compare);
|
||||
if (count == 0) {
|
||||
printf("Chyba: Nepodarilo sa načítať žiadne dáta.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Vypíšeme výsledky
|
||||
// Triedenie študentov
|
||||
qsort(students, count, sizeof(struct Student), compare);
|
||||
|
||||
// Výpis výsledkov
|
||||
printf("Výsledky:\n");
|
||||
for (int i = 0; i < num_students; i++) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
printf("%d %s\n", students[i].votes, students[i].name);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user