109 lines
1.8 KiB
C
109 lines
1.8 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#define SIZE 100
|
|
struct student
|
|
{
|
|
int votes;
|
|
char name[SIZE];
|
|
|
|
};
|
|
|
|
int find_student(struct student* students,int size, const char* name)
|
|
{
|
|
int index = -1;
|
|
|
|
for(int i = 0; i < size; i++)
|
|
{
|
|
if(strcmp(students[i].name, name) == 0)
|
|
{
|
|
index = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
return index;
|
|
}
|
|
|
|
int compare(const void* p1, const void* p2)
|
|
{
|
|
struct student* s1 = (struct student*)p1;
|
|
struct student* s2 = (struct student*)p2;
|
|
|
|
if(s2->votes != s1->votes)
|
|
{
|
|
return s2->votes - s1->votes;
|
|
}
|
|
|
|
else
|
|
{
|
|
return strcmp(s1->name, s2->name);
|
|
}
|
|
}
|
|
|
|
int main()
|
|
{
|
|
struct student database[SIZE];
|
|
memset(database,0, SIZE*sizeof(struct student));
|
|
|
|
|
|
char string[SIZE];
|
|
|
|
int k = 0;
|
|
int index = 0;
|
|
|
|
int size_name = 0;
|
|
|
|
while(fgets(string, SIZE, stdin))
|
|
{
|
|
string[strcspn(string, "\n")] = '\0';
|
|
char *end = NULL;
|
|
|
|
if(string[0] == '\0')
|
|
{
|
|
break;
|
|
}
|
|
|
|
int value = strtol(string, &end, 10);
|
|
char *name = end + 1;
|
|
|
|
size_name = strlen(name);
|
|
|
|
if(value == 0 || name == NULL || size_name <= 0)
|
|
{
|
|
break;
|
|
}
|
|
|
|
|
|
index = find_student(database, k, name);
|
|
|
|
if(index == -1)
|
|
{
|
|
database[k].votes = value;
|
|
memcpy(database[k].name,name,size_name+1);
|
|
k++;
|
|
}
|
|
else
|
|
{
|
|
database[index].votes = database[index].votes + value;
|
|
}
|
|
|
|
}
|
|
|
|
qsort(database, k, sizeof(struct student), compare);
|
|
|
|
if( k == 0)
|
|
{
|
|
printf("Nepodarilo nacitat nic\n");
|
|
return 0;
|
|
}
|
|
|
|
printf("Vysledky:\n");
|
|
for (int i = 0; i < k; i++)
|
|
{
|
|
printf("%d %s\n", database[i].votes, database[i].name);
|
|
}
|
|
|
|
return 0;
|
|
}
|