From 7a7f79a00d826b3f871ea4dec3f5a6f10c5d55cc Mon Sep 17 00:00:00 2001 From: Vladyslav Korzun Date: Fri, 17 Mar 2023 14:18:29 +0000 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB(?= =?UTF-8?q?=D0=B0)=20=D0=BD=D0=B0=20'du5/program.c'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- du5/program.c | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/du5/program.c b/du5/program.c index 59fd6d8..81e092d 100644 --- a/du5/program.c +++ b/du5/program.c @@ -4,6 +4,7 @@ #include #include #include + #define SIZE 100 struct student { @@ -14,7 +15,7 @@ struct student { int find_student(struct student* students, int size, const char* nameo){ int con = 0; for(int i = 0; i < size; i++){ - if(strcmp (nameo, students[i].name) == 0){ // students[i].name вместо students.name[SIZE] + if(strcmp (nameo, students[i].name) == 0){ con++; return i; } @@ -25,6 +26,21 @@ int find_student(struct student* students, int size, const char* nameo){ con = 0; } +int compare_students(const void* a, const void* b) { + const struct student* student_a = (const struct student*) a; + const struct student* student_b = (const struct student*) b; + + if (student_a->votes < student_b->votes) { + return 1; + } + else if (student_a->votes > student_b->votes) { + return -1; + } + else { + return 0; + } +} + int main(){ int STOP = 0; struct student databaza[SIZE]; @@ -43,41 +59,36 @@ int main(){ if (r == NULL){ STOP = 1; break; - } + } value = strtol(line, &end, 10); - //printf("value==>%d\n", value); - //printf("end====>%s\n", end); char text[SIZE]; memset(text, 0, SIZE); char* zaciatok_mena = end + 1; int velkost_mena = strlen(zaciatok_mena) - 1; - //printf("zaciatok===>%s\n", zaciatok_mena); - //printf("velkost====>%d\n", velkost_mena); if (velkost_mena > 0){ memcpy(text, zaciatok_mena, velkost_mena); - //printf("text===>%s\n", text); - } + } else { STOP = 1; - break; + break; } int id = find_student(databaza, size, text); if (id < 0){ memcpy(databaza[size].name, text, velkost_mena); databaza[size].votes = value; size += 1; - //printf("value===>%d\n",value); } else { databaza[id].votes += value; - //printf("data===>%d\n",databaza[id].votes); - } - printf("Vysledky:\n"); - for (int i = 0; i < size; i++) { - printf("%d", databaza[i].votes); - printf(" %s\n", databaza[i].name); -} + } + } + qsort(databaza, size, sizeof(struct student), compare_students); + + printf("Vysledky:\n"); + for (int i = 0; i < size; i++) { + printf("%d", databaza[i].votes); + printf(" %s\n", databaza[i].name); } return 0; }