From 957fbf894d4b37bf7b26ac070e354e4aea84d20d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Szab=C3=B3?= Date: Fri, 10 Apr 2020 08:59:49 +0200 Subject: [PATCH] c1 --- du5/program.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 du5/program.c diff --git a/du5/program.c b/du5/program.c new file mode 100644 index 0000000..1ca06a8 --- /dev/null +++ b/du5/program.c @@ -0,0 +1,71 @@ +#include +#include +#include + +#define SIZE 100 + +struct student { + char name[SIZE]; + int votes; +}; + +int find_student(struct student* students,int size, const char* name){ + for(int i=0; ivotes < ((struct student*)b)->votes) + return 1; + if(((struct student*)a)->votes == ((struct student*)b)->votes) + return strcmp(((struct student*)a)->name, ((struct student*)b)->name); + if(((struct student*)a)->votes > ((struct student*)b)->votes) + return -1; +} + + +int main(int argc, char const *argv[]){ + struct student databaza[SIZE]; + memset(databaza,0,SIZE*sizeof(struct student)); + int size = 0; + + while(size!=SIZE){ + char line[SIZE]; + memset(line,0,SIZE); + char* r = fgets(line,SIZE,stdin); + if (r == NULL){ + break; + } + + char* end = NULL; + int value = strtol(line,&end,10); + if (value == 0){ + break; + } + char tmp[SIZE]; + memset(tmp, 0, SIZE); + memcpy(tmp, end+1, strlen(end+1)-1); + int idx = find_student(databaza, size, tmp); + if(idx!=-1){ + memcpy(databaza[idx].name, tmp, strlen(tmp)); + databaza[idx].votes += value; + } + else{ + memcpy(databaza[size].name, tmp, strlen(tmp)); + databaza[size].votes = value; + size++; + } + } + + qsort(databaza, size, sizeof(struct student), compare_votes); + + printf("Vysledky:\n"); + for(int i=0; i