Initialization
This commit is contained in:
parent
851703535e
commit
24629b5422
@ -9,7 +9,7 @@ struct student {
|
|||||||
int votes;
|
int votes;
|
||||||
};
|
};
|
||||||
|
|
||||||
// porovnávacia funkcia pre zoradenie študentov
|
// comparison function for sorting students
|
||||||
int compare(const void *a, const void *b) {
|
int compare(const void *a, const void *b) {
|
||||||
struct student *student_a = (struct student *) a;
|
struct student *student_a = (struct student *) a;
|
||||||
struct student *student_b = (struct student *) b;
|
struct student *student_b = (struct student *) b;
|
||||||
@ -19,47 +19,44 @@ int compare(const void *a, const void *b) {
|
|||||||
} else if (student_a->votes < student_b->votes) {
|
} else if (student_a->votes < student_b->votes) {
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
// ak majú rovnaký počet hlasov, porovnajte mená
|
// if votes are the same, compare names
|
||||||
return strcmp(student_a->name, student_b->name);
|
return strcmp(student_a->name, student_b->name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// definícia poľa študentov s veľkosťou SIZE
|
|
||||||
struct student students[SIZE];
|
struct student students[SIZE];
|
||||||
|
|
||||||
// inicializácia premennej na uchovávanie veľkosti poľa
|
|
||||||
int size = 0;
|
int size = 0;
|
||||||
|
|
||||||
// načítanie údajov o študentoch zo štandardného vstupu
|
// read student data from standard input
|
||||||
while (1) {
|
while (1) {
|
||||||
char line[SIZE];
|
char line[SIZE];
|
||||||
memset(line, 0, SIZE);
|
memset(line, 0, SIZE);
|
||||||
char *r = fgets(line, SIZE, stdin);
|
char *r = fgets(line, SIZE, stdin);
|
||||||
|
|
||||||
// kontrola, či je riadok prázdny
|
// check if the line is empty
|
||||||
if (r == NULL || (r[0] == '\n' && strlen(r) == 1)) {
|
if (r == NULL || (r[0] == '\n' && strlen(r) == 1)) {
|
||||||
// koniec vstupu
|
// end of input
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// rozparsovanie riadku a extrakcia počtu hlasov a mena študenta
|
// parse the string and extract number of votes and student name
|
||||||
int votes;
|
int votes;
|
||||||
if (sscanf(line, "%d %s", &votes, students[size].name) != 2) {
|
if (sscanf(line, "%d %[^\n]", &votes, students[size].name) != 2) {
|
||||||
// chyba, vstup nebol v očakávanom formáte
|
// error, input was not in expected format
|
||||||
// vypíšte chybovú správu a ukončite program
|
// print error message and exit
|
||||||
fprintf(stderr, "Chyba: neplatný formát vstupu\n");
|
fprintf(stderr, "Error: invalid input format\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
students[size].votes = votes;
|
students[size].votes = votes;
|
||||||
size++;
|
size++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// zoradenie študentov podľa počtu hlasov
|
// sort students based on their votes
|
||||||
qsort(students, size, sizeof(struct student), compare);
|
qsort(students, size, sizeof(struct student), compare);
|
||||||
|
|
||||||
// výpis výsledkov
|
// print the results
|
||||||
printf("Výsledky:\n");
|
printf("Results:\n");
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; 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