This commit is contained in:
Andrii Hermaniuk 2022-04-07 16:45:48 +02:00
parent d7272a55ca
commit 1332dbc534

View File

@ -38,13 +38,15 @@ int main(){
int id = find_student(databaza,size,name); int id = find_student(databaza,size,name);
if (id< 0){ if (id< 0){
// Skopirujte zaznam na posledne miesto v poli
memcpy(databaza[size].name,name,strlen(name)+1); memcpy(databaza[size].name,name,strlen(name)+1);
databaza[size].votes=value; databaza[size].votes=value;
// Zvacsite pocet zaznamov
size+=1; size+=1;
if(size>1) if(size>1){
//printf("=====================================\n");
qsort(databaza, size, sizeof(struct student), compare); qsort(databaza, size, sizeof(struct student), compare);
}
} }
else { else {
databaza[id].votes+=value; databaza[id].votes+=value;
@ -54,6 +56,7 @@ int main(){
for(int idx=0;databaza[idx].name[0]!='\0';idx++) for(int idx=0;databaza[idx].name[0]!='\0';idx++)
printf("%d %s", databaza[idx].votes, databaza[idx].name); printf("%d %s", databaza[idx].votes, databaza[idx].name);
//printf("\n");
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -65,12 +68,17 @@ int find_student(struct student* students,int size, const char* name){
int compare(const void* p1, const void* p2){ int compare(const void* p1, const void* p2){
struct student* s1 = (struct student*)p1; struct student* s1 = (struct student*)p1;
struct student* s2 = (struct student*)p2; struct student* s2 = (struct student*)p2;
//printf("============\n");
int res=(s2->votes)-(s1->votes); int res=(s2->votes)-(s1->votes);
for(int idx=0; res==0||(s1->name[idx])!='\0';idx++){
res=(int)(s2->name[idx])-(int)(s1->name[idx]); 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 (s2->votes)-(s1->votes); return res;
} }