funguje
This commit is contained in:
parent
5423e79f36
commit
3ec0e85fbd
@ -2,73 +2,55 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define MAX_SIZE 100
|
|
||||||
#define MAX_STUDENTS 100
|
#define MAX_STUDENTS 100
|
||||||
|
#define MAX_NAME_LENGTH 50
|
||||||
|
|
||||||
// Štruktúra pre reprezentáciu študenta
|
|
||||||
struct Student {
|
struct Student {
|
||||||
char name[MAX_SIZE];
|
char name[MAX_NAME_LENGTH];
|
||||||
int votes;
|
int votes;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Porovnávacia funkcia pre triedenie študentov
|
int read_students(struct Student students[], int max_students) {
|
||||||
int compare(const void *p1, const void *p2) {
|
int count = 0;
|
||||||
const struct Student *s1 = (const struct Student *)p1;
|
while (count < max_students) {
|
||||||
const struct Student *s2 = (const struct Student *)p2;
|
int votes;
|
||||||
// Porovnanie počtu hlasov
|
char name[MAX_NAME_LENGTH];
|
||||||
if (s1->votes != s2->votes) {
|
if (scanf("%d %[^\n]", &votes, name) != 2) {
|
||||||
return s2->votes - s1->votes; // Zoradenie zostupne podľa hlasov
|
break; // Koniec vstupu alebo chyba
|
||||||
}
|
}
|
||||||
// Ak majú rovnaký počet hlasov, zoradíme lexikograficky podľa mena
|
strcpy(students[count].name, name);
|
||||||
return strcmp(s1->name, s2->name);
|
students[count].votes = votes;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Načíta študentov zo štandardného vstupu do pola
|
int compare(const void *p1, const void *p2) {
|
||||||
int read_students(struct Student students[], int max_students) {
|
struct Student *s1 = (struct Student *)p1;
|
||||||
int num_students = 0;
|
struct Student *s2 = (struct Student *)p2;
|
||||||
char line[MAX_SIZE];
|
// Porovnanie podľa počtu hlasov, v prípade rovnakého počtu podľa mena
|
||||||
while (fgets(line, sizeof(line), stdin) != NULL) {
|
if (s1->votes != s2->votes) {
|
||||||
char name[MAX_SIZE];
|
return s2->votes - s1->votes;
|
||||||
int votes;
|
} else {
|
||||||
// Načítame počet hlasov
|
return strcmp(s1->name, s2->name);
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return num_students;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
struct Student students[MAX_STUDENTS];
|
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
|
if (count == 0) {
|
||||||
qsort(students, num_students, sizeof(struct Student), compare);
|
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");
|
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);
|
printf("%d %s\n", students[i].votes, students[i].name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user