pvjc22/du5/program.c

93 lines
2.2 KiB
C
Raw Normal View History

2022-04-06 21:47:17 +00:00
#include<stdio.h>
2022-04-07 13:40:40 +00:00
#include<string.h>
2022-04-06 21:47:17 +00:00
#include<stdlib.h>
2022-04-07 13:40:40 +00:00
#define SIZE 100
struct student {
char name[SIZE];
int votes;
2022-04-07 13:45:42 +00:00
};
2022-04-07 13:40:40 +00:00
int find_student(struct student* students,int size, const char* name);
int compare(const void* p1, const void* p2);
2022-04-06 21:47:17 +00:00
int main(){
2022-04-07 13:40:40 +00:00
struct student databaza[SIZE];
memset(databaza,0,SIZE*sizeof(struct student));
int size = 0;
while(1){
2022-04-07 14:53:51 +00:00
char line[SIZE];
memset(line,0,SIZE);
char* r = fgets(line,SIZE,stdin);
if (r == NULL){
2022-04-07 13:40:40 +00:00
// Nastal koniec načítania
break;
}
char* end = NULL;
int value = strtol(line,&end,10);
2022-04-07 14:53:51 +00:00
if (value == 0&&line[0]=='\n'){
2022-04-07 13:40:40 +00:00
// Premena sa nepodarila
break;
2022-04-07 15:02:24 +00:00
}else if((value==0&&line[0]!='\n'&&size==0)){
2022-04-07 14:53:51 +00:00
printf("Nepodarilo nacitat nic\n");
return 0;
}
2022-04-07 14:57:17 +00:00
if(line[0]<'0'||line[0]>'9')break;
2022-04-07 13:40:40 +00:00
char *name=end+1;
int id = find_student(databaza,size,name);
if (id< 0){
2022-04-07 14:45:48 +00:00
2022-04-07 13:40:40 +00:00
memcpy(databaza[size].name,name,strlen(name)+1);
databaza[size].votes=value;
2022-04-07 14:45:48 +00:00
2022-04-07 13:40:40 +00:00
size+=1;
}
else {
databaza[id].votes+=value;
}
2022-04-07 14:49:18 +00:00
if(size>1){
//printf("=====================================\n");
qsort(databaza, size, sizeof(struct student), compare);
}
2022-04-07 13:40:40 +00:00
}
2022-04-07 15:03:00 +00:00
if(size==0){
2022-04-07 15:02:24 +00:00
printf("Nepodarilo nacitat nic\n");
return 0;
}
2022-04-07 13:48:43 +00:00
printf("Vysledky:\n");
2022-04-07 13:40:40 +00:00
for(int idx=0;databaza[idx].name[0]!='\0';idx++)
printf("%d %s", databaza[idx].votes, databaza[idx].name);
2022-04-07 14:45:48 +00:00
//printf("\n");
2022-04-07 13:40:40 +00:00
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;
2022-04-06 21:47:17 +00:00
}
2022-04-07 13:40:40 +00:00
int compare(const void* p1, const void* p2){
struct student* s1 = (struct student*)p1;
struct student* s2 = (struct student*)p2;
2022-04-07 14:45:48 +00:00
//printf("============\n");
2022-04-07 14:12:22 +00:00
int res=(s2->votes)-(s1->votes);
2022-04-07 14:45:48 +00:00
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]);
2022-04-07 14:12:22 +00:00
}
2022-04-07 14:45:48 +00:00
return res;
2022-04-07 13:40:40 +00:00
}
2022-04-07 14:45:48 +00:00