From de5366c929860dcf52052134f0bd3a691a28e19b Mon Sep 17 00:00:00 2001 From: st529yr Date: Wed, 20 Mar 2024 13:33:29 +0100 Subject: [PATCH] funguje --- cv5/program.c | 93 +++++++++++++++++++++------------------------------ 1 file changed, 38 insertions(+), 55 deletions(-) diff --git a/cv5/program.c b/cv5/program.c index 8da626a..8dcaaca 100644 --- a/cv5/program.c +++ b/cv5/program.c @@ -2,87 +2,70 @@ #include #include -#define MAX_NAME_LENGTH 100 - -// Structure to hold candidate information +// Structure to hold student information (name and votes) typedef struct { - char name[MAX_NAME_LENGTH]; + char name[100]; int votes; -} Candidate; +} Student; -// Comparator function for qsort -int compare_candidates(const void *a, const void *b) { - const Candidate *candidate_a = (const Candidate *)a; - const Candidate *candidate_b = (const Candidate *)b; - - // Sort by total votes in descending order - if (candidate_a->votes != candidate_b->votes) { - return candidate_b->votes - candidate_a->votes; +// Function to compare two students based on their votes and names +int compare_students(const void *a, const void *b) { + const Student *student1 = (const Student *)a; + const Student *student2 = (const Student *)b; + + // Sort by number of votes in descending order + if (student1->votes != student2->votes) { + return student2->votes - student1->votes; } else { - // If votes are tied, sort lexicographically by name - return strcmp(candidate_a->name, candidate_b->name); + // If votes are equal, sort lexicographically by name + return strcmp(student1->name, student2->name); } } int main() { - Candidate candidates[100]; // Assuming no more than 100 candidates - int num_candidates = 0; + char line[256]; + Student students[100]; + int total_students = 0; // Read votes from standard input - while (1) { + while (fgets(line, sizeof(line), stdin)) { int votes; - char name[MAX_NAME_LENGTH]; - - // Try to read votes and name - if (scanf("%d ", &votes) != 1) { - if (feof(stdin)) { - break; // End of input - } else { - printf("Error: Failed to read vote.\n"); - return 1; - } - } - - // Read candidate name - if (fgets(name, MAX_NAME_LENGTH, stdin) == NULL) { - printf("Error: Failed to read candidate name.\n"); + char name[100]; + + // Parse votes and name from input line + if (sscanf(line, "%d %99[^\n]", &votes, name) != 2) { + fprintf(stderr, "Error: Invalid input format.\n"); return 1; } - - // Remove trailing newline character - name[strcspn(name, "\n")] = '\0'; - - // Check if the candidate already exists + + // Find if the student is already in the list int found = 0; - for (int i = 0; i < num_candidates; i++) { - if (strcmp(candidates[i].name, name) == 0) { - candidates[i].votes += votes; + for (int i = 0; i < total_students; i++) { + if (strcmp(students[i].name, name) == 0) { + students[i].votes += votes; found = 1; break; } } - - // If the candidate is not found, add a new candidate + + // If student not found, add to the list if (!found) { - if (num_candidates >= 100) { - printf("Error: Too many candidates.\n"); - return 1; - } - strcpy(candidates[num_candidates].name, name); - candidates[num_candidates].votes = votes; - num_candidates++; + strcpy(students[total_students].name, name); + students[total_students].votes = votes; + total_students++; } } - // Sort the candidates - qsort(candidates, num_candidates, sizeof(Candidate), compare_candidates); - + // Sort the list of students based on votes and names + qsort(students, total_students, sizeof(Student), compare_students); + // Print the outcome printf("Outcome:\n"); - for (int i = 0; i < num_candidates; i++) { - printf("%d %s\n", candidates[i].votes, candidates[i].name); + for (int i = 0; i < total_students; i++) { + printf("%d %s\n", students[i].votes, students[i].name); } return 0; } +