This commit is contained in:
Tančáková 2024-03-20 13:33:29 +01:00
parent ea0f234a7f
commit de5366c929

View File

@ -2,87 +2,70 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define MAX_NAME_LENGTH 100 // Structure to hold student information (name and votes)
// Structure to hold candidate information
typedef struct { typedef struct {
char name[MAX_NAME_LENGTH]; char name[100];
int votes; int votes;
} Candidate; } Student;
// Comparator function for qsort // Function to compare two students based on their votes and names
int compare_candidates(const void *a, const void *b) { int compare_students(const void *a, const void *b) {
const Candidate *candidate_a = (const Candidate *)a; const Student *student1 = (const Student *)a;
const Candidate *candidate_b = (const Candidate *)b; const Student *student2 = (const Student *)b;
// Sort by total votes in descending order // Sort by number of votes in descending order
if (candidate_a->votes != candidate_b->votes) { if (student1->votes != student2->votes) {
return candidate_b->votes - candidate_a->votes; return student2->votes - student1->votes;
} else { } else {
// If votes are tied, sort lexicographically by name // If votes are equal, sort lexicographically by name
return strcmp(candidate_a->name, candidate_b->name); return strcmp(student1->name, student2->name);
} }
} }
int main() { int main() {
Candidate candidates[100]; // Assuming no more than 100 candidates char line[256];
int num_candidates = 0; Student students[100];
int total_students = 0;
// Read votes from standard input // Read votes from standard input
while (1) { while (fgets(line, sizeof(line), stdin)) {
int votes; int votes;
char name[MAX_NAME_LENGTH]; char name[100];
// Try to read votes and name // Parse votes and name from input line
if (scanf("%d ", &votes) != 1) { if (sscanf(line, "%d %99[^\n]", &votes, name) != 2) {
if (feof(stdin)) { fprintf(stderr, "Error: Invalid input format.\n");
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");
return 1; return 1;
} }
// Remove trailing newline character // Find if the student is already in the list
name[strcspn(name, "\n")] = '\0';
// Check if the candidate already exists
int found = 0; int found = 0;
for (int i = 0; i < num_candidates; i++) { for (int i = 0; i < total_students; i++) {
if (strcmp(candidates[i].name, name) == 0) { if (strcmp(students[i].name, name) == 0) {
candidates[i].votes += votes; students[i].votes += votes;
found = 1; found = 1;
break; break;
} }
} }
// If the candidate is not found, add a new candidate // If student not found, add to the list
if (!found) { if (!found) {
if (num_candidates >= 100) { strcpy(students[total_students].name, name);
printf("Error: Too many candidates.\n"); students[total_students].votes = votes;
return 1; total_students++;
}
strcpy(candidates[num_candidates].name, name);
candidates[num_candidates].votes = votes;
num_candidates++;
} }
} }
// Sort the candidates // Sort the list of students based on votes and names
qsort(candidates, num_candidates, sizeof(Candidate), compare_candidates); qsort(students, total_students, sizeof(Student), compare_students);
// Print the outcome // Print the outcome
printf("Outcome:\n"); printf("Outcome:\n");
for (int i = 0; i < num_candidates; i++) { for (int i = 0; i < total_students; i++) {
printf("%d %s\n", candidates[i].votes, candidates[i].name); printf("%d %s\n", students[i].votes, students[i].name);
} }
return 0; return 0;
} }