zmeny
This commit is contained in:
parent
8b0fcc38cf
commit
19d91f2d97
@ -8,70 +8,84 @@
|
|||||||
#define NAME_LENGTH 100
|
#define NAME_LENGTH 100
|
||||||
#define LINE_SIZE 250
|
#define LINE_SIZE 250
|
||||||
|
|
||||||
|
// Struktúra na uloženie informácií o študentovi (meno a počet hlasov)
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char name[NAME_LENGTH];
|
char name[NAME_LENGTH];
|
||||||
int votes;
|
int votes;
|
||||||
} Student;
|
} Student;
|
||||||
|
|
||||||
|
// Funkcia na vytvorenie študenta a inicializáciu jeho údajov
|
||||||
Student createStudent(const char name[], int votes) {
|
Student createStudent(const char name[], int votes) {
|
||||||
Student student;
|
Student student;
|
||||||
strncpy(student.name, name, NAME_LENGTH - 1);
|
strncpy(student.name, name, NAME_LENGTH - 1); // Kopíruje meno do štruktúry
|
||||||
student.name[NAME_LENGTH - 1] = '\0';
|
student.name[NAME_LENGTH - 1] = '\0'; // Pridáva koncovú nulovú hodnotu pre bezpečnosť
|
||||||
student.votes = votes;
|
student.votes = votes; // Ukladá počet hlasov
|
||||||
return student;
|
return student;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Porovnávacia funkcia pre qsort na zoradenie študentov
|
||||||
int compare_students(const void *a, const void *b) {
|
int compare_students(const void *a, const void *b) {
|
||||||
const Student *studentA = (const Student *)a;
|
const Student *studentA = (const Student *)a;
|
||||||
const Student *studentB = (const Student *)b;
|
const Student *studentB = (const Student *)b;
|
||||||
|
|
||||||
|
// Najskôr porovnáme počet hlasov (zoradené zostupne)
|
||||||
if (studentA->votes != studentB->votes) {
|
if (studentA->votes != studentB->votes) {
|
||||||
return studentB->votes - studentA->votes;
|
return studentB->votes - studentA->votes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ak majú rovnaký počet hlasov, zoradíme podľa mena abecedne
|
||||||
return strcmp(studentA->name, studentB->name);
|
return strcmp(studentA->name, studentB->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
char line[LINE_SIZE];
|
char line[LINE_SIZE]; // Premenná na čítanie riadka vstupu
|
||||||
Student students[MAX_COUNT_OF_STUDENTS];
|
Student students[MAX_COUNT_OF_STUDENTS]; // Pole študentov
|
||||||
int count = 0;
|
int count = 0; // Počet študentov v poli
|
||||||
|
|
||||||
|
// Čítanie vstupných údajov
|
||||||
while (fgets(line, LINE_SIZE, stdin) != NULL && line[0] != '\n') {
|
while (fgets(line, LINE_SIZE, stdin) != NULL && line[0] != '\n') {
|
||||||
int count_of_vote = 0;
|
int count_of_vote = 0; // Počet hlasov zo vstupu
|
||||||
char name[NAME_LENGTH] = {0};
|
char name[NAME_LENGTH] = {0}; // Premenná na meno študenta
|
||||||
|
|
||||||
line[strcspn(line, "\n")] = '\0';
|
line[strcspn(line, "\n")] = '\0'; // Odstráni znak nového riadka na konci
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
// Spracovanie číselnej časti (počet hlasov)
|
||||||
while (isdigit(line[i])) {
|
while (isdigit(line[i])) {
|
||||||
count_of_vote = count_of_vote * 10 + (line[i] - '0');
|
count_of_vote = count_of_vote * 10 + (line[i] - '0');
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Skontroluje, či po čísle je medzera
|
||||||
if (line[i] != ' ') {
|
if (line[i] != ' ') {
|
||||||
return 0;
|
break;
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
|
|
||||||
|
// Skopíruje meno študenta z riadka
|
||||||
strncpy(name, line + i, NAME_LENGTH - 1);
|
strncpy(name, line + i, NAME_LENGTH - 1);
|
||||||
name[NAME_LENGTH - 1] = '\0';
|
name[NAME_LENGTH - 1] = '\0';
|
||||||
|
|
||||||
|
// Hľadanie, či už daný študent existuje v poli
|
||||||
bool found = false;
|
bool found = false;
|
||||||
for (int j = 0; j < count; ++j) {
|
for (int j = 0; j < count; ++j) {
|
||||||
if (strcmp(name, students[j].name) == 0) {
|
if (strcmp(name, students[j].name) == 0) {
|
||||||
students[j].votes += count_of_vote;
|
students[j].votes += count_of_vote; // Ak existuje, pripočítame hlasy
|
||||||
found = true;
|
found = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ak študent neexistuje, pridáme ho do poľa
|
||||||
if (!found) {
|
if (!found) {
|
||||||
students[count++] = createStudent(name, count_of_vote);
|
students[count++] = createStudent(name, count_of_vote);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Zoradenie študentov podľa počtu hlasov a mena
|
||||||
qsort(students, count, sizeof(Student), compare_students);
|
qsort(students, count, sizeof(Student), compare_students);
|
||||||
|
|
||||||
|
// Výpis výsledkov
|
||||||
printf("Vysledky:\n");
|
printf("Vysledky:\n");
|
||||||
for (int i = 0; i < count; 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