pvjc20/du5/program.c
Zoltán Szabó dae6e68199 c4
2020-04-10 09:48:05 +02:00

84 lines
1.7 KiB
C

#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;
}
if(line[0]=='\n'){
break;
}
char* end = NULL;
int value = strtol(line,&end,10);
if (value == 0){
if(size!=0){
break;
}
printf("Nepodarilo nacitat nic\n");
return 0;
}
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);
if(size==0){
printf("Nepodarilo nacitat nic\n");
}
else{
printf("Vysledky:\n");
for(int i=0; i<size; i++){
printf("%d %s\n", databaza[i].votes, databaza[i].name);
}
}
return 0;
}