Изменить 'du5/program.c'
This commit is contained in:
parent
208655149d
commit
fb29375288
@ -1,40 +1,100 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user