Изменить 'du5/program.c'

This commit is contained in:
Oleksandr Hryshchenko 2021-04-06 14:45:56 +00:00
parent 208655149d
commit fb29375288

View File

@ -1,40 +1,100 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h>
#include <stdbool.h>
struct entry{ struct entry{
char* name; char* name;
int votes; int votes;
} };
struct entry* createEntry(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); strcpy(newEntry->name, name);
newEntry->votes = votes; newEntry->votes = votes;
return newEntry; return newEntry;
} }
int main(){ bool addPoints(struct entry* myEntries, int votes, char* name){
struct entry myEntries[100]; if(!myEntries)
char inputs[100][50]; return false;
int counter = 0; if(!strcmp(myEntries->name, name)) {
char symbol; myEntries->votes += votes;
char* number1 = (char*) calloc (25, sizeof(char)); 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; counter = 0;
memset(number1, '\0', 25);
memset(name, '\0', 25);
symbol = inputs[i][counter++]; symbol = inputs[i][counter++];
while(symbol != '\0' && symbol != '\n'){ while(symbol != '\0' && symbol != '\n'){
if(symbol == '|') goto LABEL;
while(isspace(symbol)) while(isspace(symbol))
symbol = inputs[i][counter++]; symbol = inputs[i][counter++];
if(isdigit(symbol)) if(isdigit(symbol)) {
number1 += symbol; number1[strlen(number1)] = symbol;
else if(isalpha) 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; return 0;
} }