105 lines
2.9 KiB
C
105 lines
2.9 KiB
C
#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));
|
|
newEntry->name = (char*)calloc(25, sizeof(char));
|
|
strcpy(newEntry->name, name);
|
|
newEntry->votes = votes;
|
|
return newEntry;
|
|
}
|
|
|
|
bool addPoints(struct entry* myEntries, int votes, char* name){
|
|
if(!myEntries)
|
|
return false;
|
|
if(!strcmp(myEntries->name, name)) {
|
|
myEntries->votes += votes;
|
|
return true;
|
|
}
|
|
|
|
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;
|
|
}
|
|
else if ((list[j]->votes == list[j + 1]->votes) && (list[j]->name[0] > list[j + 1]->name[0])){
|
|
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++];
|
|
if(symbol == '\n') break;
|
|
while(symbol != '\0' && symbol != '\n'){
|
|
if(isspace(symbol) && isdigit(inputs[i][counter-2]))
|
|
symbol = inputs[i][counter++];
|
|
|
|
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));
|
|
}
|
|
|
|
bsortDesc(myEntries, position+1);
|
|
printf("Vysledky:\n");
|
|
for(int i = 0; myEntries[i]->name != NULL; i++) {
|
|
printf("%d %s\n", myEntries[i]->votes, myEntries[i]->name);
|
|
}
|
|
|
|
return 0;
|
|
} |