2023-03-25 11:57:05 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#define SIZE 100
|
|
|
|
|
|
|
|
struct student{
|
|
|
|
int votes;
|
|
|
|
char name[SIZE];
|
|
|
|
};
|
|
|
|
|
|
|
|
int search_index(int size, const struct student list[SIZE], const char student[SIZE]){
|
|
|
|
for(int x = 0; x < size; x++){
|
|
|
|
if(strcmp(list[x].name, student) == 0){
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sort_by_votes(const void* p1, const void* p2){
|
|
|
|
struct student* student1 = (struct student*) p1;
|
|
|
|
struct student* student2 = (struct student*) p2;
|
|
|
|
|
|
|
|
if(student1->votes != student2->votes){
|
|
|
|
if(student1->votes < student2->votes){
|
|
|
|
return 1;
|
|
|
|
}else{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
return strcmp(student1->name, student2->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(){
|
|
|
|
int votes;
|
|
|
|
char name[SIZE];
|
|
|
|
char line[SIZE];
|
|
|
|
char *end;
|
|
|
|
int counter = 0;
|
|
|
|
|
|
|
|
struct student list[SIZE];
|
|
|
|
|
|
|
|
while(fgets(line, SIZE, stdin) != NULL){
|
|
|
|
if(line[0] == '\n' || !isdigit(line[0])){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
votes = strtol(line, &end, 10);
|
|
|
|
|
|
|
|
if(end[0] != ' ' || !isalpha(end[1])){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(end[strlen(end) - 1] == '\n'){
|
|
|
|
end[strlen(end) - 1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(name, end + 1);
|
|
|
|
|
|
|
|
int stud_index = search_index(counter, list, name);
|
|
|
|
|
|
|
|
if(stud_index == -1){
|
|
|
|
counter++;
|
|
|
|
|
|
|
|
list[counter - 1].votes = votes;
|
|
|
|
strcpy(list[counter - 1].name, name);
|
|
|
|
}else{
|
|
|
|
list[stud_index].votes += votes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(counter == 0){
|
2023-03-25 11:58:48 +00:00
|
|
|
printf("Nepodarilo sa nacitat nic\n");
|
2023-03-25 11:57:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
qsort(list, counter, sizeof(list[0]), sort_by_votes);
|
|
|
|
|
|
|
|
printf("Vysledky:\n");
|
|
|
|
|
|
|
|
for(int x = 0; x < counter; x++){
|
|
|
|
printf("%d %s\n", list[x].votes, list[x].name);
|
|
|
|
}
|
2023-03-25 11:52:14 +00:00
|
|
|
}
|