#include #include #include #include #define SIZE 100 struct student { char name[SIZE]; int votes; }; int find_student(struct student* students,int size, const char* name){ for(int i = 0;i < size; i++){ if (strcmp(students[i].name, name) == 0) { return i; } } return -1; } int compare_votes (const void* p1, const void* p2) { struct student* s1 = (struct student*)p1; struct student* s2 = (struct student*)p2; return (s2->votes - s1->votes); } int compare_names (const void* p1, const void* p2) { struct student* s1 = (struct student*)p1; struct student* s2 = (struct student*)p2; return strcmp(s1->name,s2->name); } int main(){ int counter = 0; char name[SIZE]; memset(name,0,SIZE); char line[SIZE]; memset(line,0,SIZE); struct student databaza[SIZE]; memset(databaza,0,SIZE*sizeof(struct student)); int size = 0; while(fgets(line,SIZE,stdin) && line[0] != '\n'){ memset(name,0,SIZE); if(line == NULL){ break; } char* end = NULL; int value = strtol(line,&end,10); char* zaciatok_mena = end + 1; if (value == 0){ break; } int velkost_mena = strlen(zaciatok_mena) - 1; if (velkost_mena > 0){ memcpy(name,zaciatok_mena,velkost_mena); } else{ printf("nepodarilo sa nacitat meno\n"); return 0; } int id = find_student(databaza,size,name); if (id< 0){ memcpy(databaza[counter].name,name,velkost_mena); databaza[counter].votes = value; size++; } else { memset(databaza[counter].name,0,velkost_mena); databaza[id].votes = databaza[id].votes + value; size++; } counter++; } counter--; qsort(databaza, size, sizeof(struct student), compare_names); qsort(databaza, size, sizeof(struct student), compare_votes); if(databaza[0].votes == 0) printf("Nepodarilo nacitat nic\n"); else printf("Vysledky:\n"); for(int i = 0; i < size; i++) { if(databaza[i].votes != 0) printf("%d %s\n", databaza[i].votes, databaza[i].name); } return 0; }