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