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