pvjc20/du5/program.c

79 lines
1.6 KiB
C
Raw Normal View History

2020-04-10 06:59:49 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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)){
return i;
}
}
return -1;
}
int compare_votes(const void * a, const void * b){
if(((struct student*)a)->votes < ((struct student*)b)->votes)
return 1;
if(((struct student*)a)->votes == ((struct student*)b)->votes)
return strcmp(((struct student*)a)->name, ((struct student*)b)->name);
if(((struct student*)a)->votes > ((struct student*)b)->votes)
return -1;
}
int main(int argc, char const *argv[]){
struct student databaza[SIZE];
memset(databaza,0,SIZE*sizeof(struct student));
int size = 0;
while(size!=SIZE){
char line[SIZE];
memset(line,0,SIZE);
char* r = fgets(line,SIZE,stdin);
if (r == NULL){
break;
}
2020-04-10 07:23:04 +00:00
if(line[0]=='\n'){
break;
}
2020-04-10 06:59:49 +00:00
char* end = NULL;
int value = strtol(line,&end,10);
if (value == 0){
2020-04-10 07:37:15 +00:00
if(size!=0){
break;
}
2020-04-10 07:23:04 +00:00
printf("Nepodarilo nacitat nic\n");
return 0;
2020-04-10 06:59:49 +00:00
}
char tmp[SIZE];
memset(tmp, 0, SIZE);
memcpy(tmp, end+1, strlen(end+1)-1);
int idx = find_student(databaza, size, tmp);
if(idx!=-1){
memcpy(databaza[idx].name, tmp, strlen(tmp));
databaza[idx].votes += value;
}
else{
memcpy(databaza[size].name, tmp, strlen(tmp));
databaza[size].votes = value;
size++;
}
}
qsort(databaza, size, sizeof(struct student), compare_votes);
printf("Vysledky:\n");
for(int i=0; i<size; i++){
printf("%d %s\n", databaza[i].votes, databaza[i].name);
}
return 0;
}