From fb2937528842b72714376d3b69a3bc0507f3da81 Mon Sep 17 00:00:00 2001 From: Oleksandr Hryshchenko Date: Tue, 6 Apr 2021 14:45:56 +0000 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20'du5/program.c'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- du5/program.c | 88 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 74 insertions(+), 14 deletions(-) diff --git a/du5/program.c b/du5/program.c index ab45005..4a69940 100644 --- a/du5/program.c +++ b/du5/program.c @@ -1,40 +1,100 @@ #include #include #include +#include +#include struct entry{ char* name; int votes; -} +}; struct entry* createEntry(char* name, int votes){ - struct entry* newEntry = (struct entry*) calloc(1, sizeof(struct entry)); + struct entry* newEntry = (struct entry*)calloc(1, sizeof(struct entry)); + newEntry->name = (char*)calloc(25, sizeof(char)); strcpy(newEntry->name, name); newEntry->votes = votes; return newEntry; } -int main(){ - struct entry myEntries[100]; - char inputs[100][50]; - int counter = 0; - char symbol; - char* number1 = (char*) calloc (25, sizeof(char)); +bool addPoints(struct entry* myEntries, int votes, char* name){ + if(!myEntries) + return false; + if(!strcmp(myEntries->name, name)) { + myEntries->votes += votes; + return true; + } - for(int i = 0; fgets(inputs[1], 50, stdin) != NULL; i++){ + return false; +} + +void bsortDesc(struct entry* list[80], int s){ + int i, j; + struct entry* temp; + + for (i = 0; i < s - 1; i++) + { + for (j = 0; j < (s - 1-i); j++) + { + if (list[j]->votes < list[j + 1]->votes) + { + temp = list[j]; + list[j] = list[j + 1]; + list[j + 1] = temp; + } + } + } +} + +int main() { + struct entry* myEntries[100]; + for(int i = 0; i < 100; i++) + myEntries[i] = (struct entry*) calloc(1, sizeof(struct entry)); + char inputs[100][50]; //input + int counter; //counter + char symbol; //current symbol + char* number1 = (char*) calloc(25, sizeof(char)); //temporary points + char* name = (char*) calloc(25, sizeof(char)); //temporary name + char* ptr; + bool checked = false; + int position = 0; + + for(int i = 0; fgets(inputs[i], 50, stdin) != NULL; i++){ counter = 0; + memset(number1, '\0', 25); + memset(name, '\0', 25); symbol = inputs[i][counter++]; while(symbol != '\0' && symbol != '\n'){ + if(symbol == '|') goto LABEL; while(isspace(symbol)) symbol = inputs[i][counter++]; - if(isdigit(symbol)) - number1 += symbol; - else if(isalpha) + if(isdigit(symbol)) { + number1[strlen(number1)] = symbol; + number1[strlen(number1)] = '\0'; + } + else{ + name[strlen(name)] = symbol; + name[strlen(name)] = '\0'; + } + symbol = inputs[i][counter++]; } + + for(int j = 0; myEntries[j]->name != NULL; j++) { + if(addPoints(myEntries[j], (int)strtol(number1, &ptr, 10), name)) + checked = true; + position = j+1; + } + + if(!checked) + myEntries[position] = createEntry(name, (int)strtol(number1, &ptr, 10)); } - + LABEL: + bsortDesc(myEntries, position+1); + for(int i = 0; myEntries[i]->name != NULL; i++) { + printf("%d %s\n", myEntries[i]->votes, myEntries[i]->name); + } return 0; -} +} \ No newline at end of file