Aktualizovat du4/program.c
This commit is contained in:
parent
667f04f70e
commit
afea69cade
@ -5,40 +5,33 @@
|
|||||||
#define MAX_LEN 256
|
#define MAX_LEN 256
|
||||||
#define MAX_NAME_LEN 100
|
#define MAX_NAME_LEN 100
|
||||||
|
|
||||||
// Структура для хранения информации о студенте
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char name[MAX_NAME_LEN]; // имя студента
|
char name[MAX_NAME_LEN];
|
||||||
int votes; // количество голосов
|
int votes;
|
||||||
} Student;
|
} Student;
|
||||||
|
|
||||||
// Функция для сравнения студентов по количеству голосов (сначала по количеству голосов, затем по имени)
|
|
||||||
int compare_students(const void *a, const void *b) {
|
int compare_students(const void *a, const void *b) {
|
||||||
Student *student_a = (Student *)a;
|
Student *student_a = (Student *)a;
|
||||||
Student *student_b = (Student *)b;
|
Student *student_b = (Student *)b;
|
||||||
|
|
||||||
// Сначала сортируем по количеству голосов (по убыванию)
|
|
||||||
if (student_b->votes != student_a->votes) {
|
if (student_b->votes != student_a->votes) {
|
||||||
return student_b->votes - student_a->votes;
|
return student_b->votes - student_a->votes;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Если количество голосов одинаковое, сортируем по имени (по алфавиту)
|
|
||||||
return strcmp(student_a->name, student_b->name);
|
return strcmp(student_a->name, student_b->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Student students[MAX_LEN]; // Массив для хранения информации о студентах
|
Student students[MAX_LEN];
|
||||||
int student_count = 0; // Количество студентов, проголосовавших
|
int student_count = 0;
|
||||||
|
|
||||||
char line[MAX_LEN];
|
char line[MAX_LEN];
|
||||||
|
|
||||||
// Считываем ввод до конца
|
|
||||||
while (fgets(line, MAX_LEN, stdin)) {
|
while (fgets(line, MAX_LEN, stdin)) {
|
||||||
int votes;
|
int votes;
|
||||||
char name[MAX_NAME_LEN];
|
char name[MAX_NAME_LEN];
|
||||||
|
|
||||||
// Проверяем формат строки (первое число - количество голосов, затем имя)
|
|
||||||
if (sscanf(line, "%d %[^\n]", &votes, name) != 2) {
|
if (sscanf(line, "%d %[^\n]", &votes, name) != 2) {
|
||||||
// Если формат неправильный, выводим ошибку и завершаем программу
|
|
||||||
if (student_count == 0) {
|
if (student_count == 0) {
|
||||||
printf("CHYBA\n");
|
printf("CHYBA\n");
|
||||||
return 0;
|
return 0;
|
||||||
@ -46,7 +39,6 @@ int main() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ищем студента в списке
|
|
||||||
int found = 0;
|
int found = 0;
|
||||||
for (int i = 0; i < student_count; i++) {
|
for (int i = 0; i < student_count; i++) {
|
||||||
if (strcmp(students[i].name, name) == 0) {
|
if (strcmp(students[i].name, name) == 0) {
|
||||||
@ -56,24 +48,18 @@ int main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Если студент не найден, добавляем его в список
|
|
||||||
if (!found) {
|
if (!found) {
|
||||||
if (student_count < MAX_LEN) {
|
if (student_count < MAX_LEN) {
|
||||||
strcpy(students[student_count].name, name);
|
strcpy(students[student_count].name, name);
|
||||||
students[student_count].votes = votes;
|
students[student_count].votes = votes;
|
||||||
student_count++;
|
student_count++;
|
||||||
} else {
|
} else {
|
||||||
// Если достигнут предел студентов, прекращаем ввод
|
|
||||||
printf("CHYBA\n");
|
printf("CHYBA\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Недостающая часть: сортировка и вывод результатов (не завершено)
|
|
||||||
// qsort(students, student_count, sizeof(Student), compare_students);
|
|
||||||
|
|
||||||
// Примерный вывод без сортировки
|
|
||||||
printf("Vysledky:\n");
|
printf("Vysledky:\n");
|
||||||
for (int i = 0; i < student_count; i++) {
|
for (int i = 0; i < student_count; i++) {
|
||||||
printf("%d %s\n", students[i].votes, students[i].name);
|
printf("%d %s\n", students[i].votes, students[i].name);
|
||||||
|
Loading…
Reference in New Issue
Block a user