pvjc25/du4/program.c

72 lines
1.8 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 256
#define MAX_NAME_LEN 100
// Структура для хранения информации о студенте
typedef struct {
char name[MAX_NAME_LEN]; // имя студента
int votes; // количество голосов
} Student;
int compare_students(const void *a, const void *b) {
Student *student_a = (Student *)a;
Student *student_b = (Student *)b;
if (student_b->votes != student_a->votes) {
return student_b->votes - student_a->votes;
}
return strcmp(student_a->name, student_b->name);
}
int main() {
Student students[MAX_LEN];
int student_count = 0;
char line[MAX_LEN];
while (fgets(line, MAX_LEN, stdin)) {
int votes;
char name[MAX_NAME_LEN];
if (sscanf(line, "%d %[^\n]", &votes, name) != 2) {
if (student_count == 0) {
printf("CHYBA\n");
return 0;
}
break;
}
int found = 0;
for (int i = 0; i < student_count; i++) {
if (strcmp(students[i].name, name) == 0) {
students[i].votes += votes;
found = 1;
break;
}
}
if (!found) {
if (student_count < MAX_LEN) {
strcpy(students[student_count].name, name);
students[student_count].votes = votes;
student_count++;
} else {
printf("CHYBA\n");
return 0;
}
}
}
qsort(students, student_count, sizeof(Student), compare_students);
printf("Результаты:\n");
for (int i = 0; i < student_count; i++) {
printf("%d %s\n", students[i].votes, students[i].name);
}
return 0;
}