diff --git a/a1/program.exe b/a1/program.exe index 332f2cf..df14866 100644 Binary files a/a1/program.exe and b/a1/program.exe differ diff --git a/du1/program.c b/du1/program.c index 7966dc8..5661b32 100644 --- a/du1/program.c +++ b/du1/program.c @@ -14,10 +14,10 @@ int main() { count++; //нумерация чисел } - if (count == 0) { //если не было введено никакое число - printf("Chyba: Malo platnych hodnot.\n"); - return 0; - } + if (count == 0) { //если не было введено никакое число + printf("Chyba: Malo platnych hodnot.\n"); + return 0; + } for (int i = 0; i < count; i++) { //выписывание всех чисел printf("Sutaziaci c. %d vypil %d poharov.\n",i+1,results[i]); diff --git a/du1/program.exe b/du1/program.exe index 0005b45..c60d3c0 100644 Binary files a/du1/program.exe and b/du1/program.exe differ diff --git a/du2/program.exe b/du2/program.exe index bf1103b..64bf57d 100644 Binary files a/du2/program.exe and b/du2/program.exe differ diff --git a/du4/program.c b/du4/program.c index f982179..a20528d 100644 --- a/du4/program.c +++ b/du4/program.c @@ -1,36 +1,69 @@ #include -#include #include #include +#define MAX_NAME_LENGTH 100 + typedef struct { - char name[100]; - int golosa; -} nameandgolosa; + char name[MAX_NAME_LENGTH]; + int votes; +} Student; -#define MAX_STUDENTS 1000; +int compare_students(const void *a, const void *b) { + const Student *studentA = (const Student *)a; + const Student *studentB = (const Student *)b; -int najtistudenta (nameandgolosa students[], int count, const char* name) { - for (int i = 0; i < count; i++) { - if (strcmp(students[i].name, name) == 0) { - return i; - } + if (studentA->votes != studentB->votes) { + return studentB->votes - studentA->votes; // Сортировка по убыванию голосов + } else { + return strcmp(studentA->name, studentB->name); // Лексикографическая сортировка по имени } - return -1; } -int compare (const void* a, const void* b) { - nameandgolosa* studentA = (nameandgolosa*)a; - nameandgolosa* studentB = (nameandgolosa*)b; - - if (studentA->golosa != studentB->golosa) { - return studentB->golosa - studentA->golosa; - } - return strcmp(studentA->name, studentB->name); -} - - int main() { + Student students[100]; + int student_count = 0; + char line[256]; + + while (fgets(line, sizeof(line), stdin)) { + int votes; + char name[MAX_NAME_LENGTH]; + + if (sscanf(line, "%d %[^\n]", &votes, name) != 2) { + 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) { + strcpy(students[student_count].name, name); + students[student_count].votes = votes; + student_count++; + } + } + + if (student_count == 0) { + printf("Chyba: nepodarilo sa nacitat ziadny zaznam.\n"); + return 1; + } + + // Сортировка студентов + qsort(students, student_count, sizeof(Student), compare_students); + + // Вывод результатов + printf("Vysledky:\n"); + for (int i = 0; i < student_count; i++) { + printf("%d %s\n", students[i].votes, students[i].name); + } + return 0; } - diff --git a/du4/program.exe b/du4/program.exe index 868e0a8..6ac905f 100644 Binary files a/du4/program.exe and b/du4/program.exe differ