pvjc24/cv5/program.c

74 lines
2.1 KiB
C
Raw Normal View History

2024-03-13 09:55:11 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2024-03-20 12:59:55 +00:00
#define MAX_NAME_LENGTH 100
2024-03-20 14:08:46 +00:00
#define MAX_BUFFER_LENGTH 256
2024-03-20 12:59:55 +00:00
2024-03-20 14:45:47 +00:00
// Structure to store student information
2024-03-13 09:55:11 +00:00
typedef struct {
2024-03-20 14:08:46 +00:00
char name[MAX_NAME_LENGTH];
2024-03-13 09:55:11 +00:00
int votes;
2024-03-20 12:33:29 +00:00
} Student;
2024-03-13 09:55:11 +00:00
2024-03-20 14:45:47 +00:00
// Comparison function for sorting students by votes and name
2024-03-20 12:33:29 +00:00
int compare_students(const void *a, const void *b) {
2024-03-20 14:08:46 +00:00
const Student *student_a = (const Student *)a;
const Student *student_b = (const Student *)b;
2024-03-20 14:45:47 +00:00
// First compare by number of votes
2024-03-20 14:08:46 +00:00
if (student_a->votes != student_b->votes) {
2024-03-20 14:45:47 +00:00
return student_b->votes - student_a->votes; // Sort in descending order
2024-03-13 09:55:11 +00:00
}
2024-03-20 14:08:46 +00:00
2024-03-20 14:45:47 +00:00
// If votes are tied, compare lexicographically by name
2024-03-20 14:08:46 +00:00
return strcmp(student_a->name, student_b->name);
2024-03-13 09:55:11 +00:00
}
int main() {
2024-03-20 14:45:47 +00:00
// Initialize array to store students
2024-03-20 14:08:46 +00:00
Student students[MAX_BUFFER_LENGTH];
int num_students = 0;
2024-03-13 09:55:11 +00:00
2024-03-20 14:45:47 +00:00
// Read votes from standard input
2024-03-20 14:08:46 +00:00
char buffer[MAX_BUFFER_LENGTH];
while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
2024-03-20 14:45:47 +00:00
// Read number of votes and student's name
int votes;
2024-03-20 14:28:37 +00:00
char name[MAX_NAME_LENGTH];
2024-03-20 14:45:47 +00:00
if (sscanf(buffer, "%d %99[^\n]", &votes, name) != 2) {
fprintf(stderr, "Error: Invalid input format.\n");
return 1;
}
2024-03-20 14:08:46 +00:00
2024-03-20 14:45:47 +00:00
// Store student in array
2024-03-20 14:08:46 +00:00
strncpy(students[num_students].name, name, MAX_NAME_LENGTH);
students[num_students].votes = votes;
num_students++;
2024-03-20 14:45:47 +00:00
// Check for exceeding maximum number of students
2024-03-20 14:08:46 +00:00
if (num_students >= MAX_BUFFER_LENGTH) {
2024-03-20 14:45:47 +00:00
fprintf(stderr, "Error: Too many students.\n");
return 1;
2024-03-20 13:52:38 +00:00
}
2024-03-13 09:55:11 +00:00
}
2024-03-20 14:45:47 +00:00
// Print error message if no records were retrieved
2024-03-20 14:08:46 +00:00
if (num_students == 0) {
2024-03-20 14:45:47 +00:00
fprintf(stderr, "Error: No records retrieved.\n");
return 1;
2024-03-13 09:55:11 +00:00
}
2024-03-20 14:45:47 +00:00
// Sort students by number of votes and name
2024-03-20 14:08:46 +00:00
qsort(students, num_students, sizeof(Student), compare_students);
2024-03-20 14:45:47 +00:00
// Print results
printf("Results:\n");
2024-03-20 14:08:46 +00:00
for (int i = 0; i < num_students; i++) {
printf("%d %s\n", students[i].votes, students[i].name);
}
2024-03-20 13:52:38 +00:00
2024-03-13 09:55:11 +00:00
return 0;
}