From 124c79083553b2f73dd34e54fbb2cd6d08cfbf15 Mon Sep 17 00:00:00 2001 From: Andrii Hermaniuk Date: Thu, 7 Apr 2022 15:40:40 +0200 Subject: [PATCH] add --- du5/program.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/du5/program.c b/du5/program.c index aa22f4a..7281425 100644 --- a/du5/program.c +++ b/du5/program.c @@ -1,7 +1,74 @@ #include +#include #include +#define SIZE 100 + +struct student { + char name[SIZE]; + int votes; +}attribute((packed)); + + +int find_student(struct student* students,int size, const char* name); + +int compare(const void* p1, const void* p2); int main(){ - return 0; + struct student databaza[SIZE]; + memset(databaza,0,SIZE*sizeof(struct student)); + int size = 0; + + while(1){ + char line[SIZE]; + memset(line,0,SIZE); + char* r = fgets(line,SIZE,stdin); + if (r == NULL){ + // Nastal koniec načítania + break; + } + + char* end = NULL; + int value = strtol(line,&end,10); + if (value == 0){ + // Premena sa nepodarila + break; + } + char *name=end+1; + + int id = find_student(databaza,size,name); + if (id< 0){ + // Skopirujte zaznam na posledne miesto v poli + memcpy(databaza[size].name,name,strlen(name)+1); + databaza[size].votes=value; + // Zvacsite pocet zaznamov + size+=1; + } + else { + // pripocitajte pocet hlasov + databaza[id].votes+=value; + } + if(size>1) + qsort(databaza, size, sizeof(struct student), compare); + } + for(int idx=0;databaza[idx].name[0]!='\0';idx++) + printf("%d %s", databaza[idx].votes, databaza[idx].name); + + return EXIT_SUCCESS; } + +int find_student(struct student* students,int size, const char* name){ + // Dopíšte cyklus, ktorý prejde všetky položky v databáze + // a ak nájde zhodné meno tak vráti jeho index. + for(int idx=0; idxvotes je počet hlasov + // s1->name je meno študenta + return (s2->votes)-(s1->votes); +} +