88 lines
2.0 KiB
C
88 lines
2.0 KiB
C
#include<stdio.h>
|
|
#include<string.h>
|
|
#include<stdlib.h>
|
|
#define SIZE 100
|
|
|
|
struct student {
|
|
char name[SIZE];
|
|
int votes;
|
|
};
|
|
|
|
|
|
int find_student(struct student* students,int size, const char* name);
|
|
|
|
int compare(const void* p1, const void* p2);
|
|
|
|
int main(){
|
|
|
|
struct student databaza[SIZE];
|
|
memset(databaza,0,SIZE*sizeof(struct student));
|
|
int size = 0;
|
|
|
|
while(1){
|
|
char line[SIZE];
|
|
memset(line,0,SIZE);
|
|
char* r = fgets(line,SIZE,stdin);
|
|
if (r == NULL){
|
|
// Nastal koniec načítania
|
|
break;
|
|
}
|
|
|
|
char* end = NULL;
|
|
int value = strtol(line,&end,10);
|
|
if (value == 0&&line[0]=='\n'){
|
|
// Premena sa nepodarila
|
|
break;
|
|
}else if(value==0&&line[0]!='\n'){
|
|
printf("Nepodarilo nacitat nic\n");
|
|
return 0;
|
|
}
|
|
char *name=end+1;
|
|
|
|
int id = find_student(databaza,size,name);
|
|
if (id< 0){
|
|
|
|
memcpy(databaza[size].name,name,strlen(name)+1);
|
|
databaza[size].votes=value;
|
|
|
|
size+=1;
|
|
}
|
|
else {
|
|
databaza[id].votes+=value;
|
|
}
|
|
if(size>1){
|
|
//printf("=====================================\n");
|
|
qsort(databaza, size, sizeof(struct student), compare);
|
|
}
|
|
}
|
|
printf("Vysledky:\n");
|
|
for(int idx=0;databaza[idx].name[0]!='\0';idx++)
|
|
printf("%d %s", databaza[idx].votes, databaza[idx].name);
|
|
|
|
//printf("\n");
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
int find_student(struct student* students,int size, const char* name){
|
|
for(int idx=0; idx<size;idx++)if(strcmp(students[idx].name,name)==0)return idx;
|
|
return -1;
|
|
}
|
|
|
|
int compare(const void* p1, const void* p2){
|
|
struct student* s1 = (struct student*)p1;
|
|
struct student* s2 = (struct student*)p2;
|
|
|
|
//printf("============\n");
|
|
int res=(s2->votes)-(s1->votes);
|
|
|
|
for(int idx=0;res==0&&idx<SIZE;idx++){
|
|
res=(int)(s1->name[idx])-(int)(s2->name[idx]);
|
|
//printf("%d, %c, %c\n",res,s2->name[idx],s1->name[idx]);
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
|
|
|