This commit is contained in:
Vadym Afanasiev 2026-03-12 15:03:07 +00:00
parent 9c5a3a87c0
commit a912255cc9

View File

@ -7,7 +7,7 @@ struct student
{
char name[SIZE];
int holosa;
int votes;
};
@ -28,9 +28,9 @@ int funkcia_compare(const void* p1, const void* p2)
{
const struct student* s1 = (const struct student*)p1;
const struct student* s2 = (const struct student*)p2;
if (s1->holosa != s2->holosa)
if (s1->votes != s2->votes)
{
return s2->holosa - s1->holosa;
return s2->votes - s1->votes;
}
return strcmp(s1->name, s2->name);
@ -43,13 +43,7 @@ int rozmer = 0;
char line[SIZE];
while (fgets(line, SIZE, stdin))
{
memset(line, 0 , SIZE);
char* r = fgets(line, SIZE, stdin);
if (r == NULL)
{
break;
}
char* end = NULL;
int znak = strtol(line, &end, 10);
if (znak == 0)
@ -66,17 +60,18 @@ if (rozmir_mena <= 0)
char name[SIZE];
memset(name, 0, SIZE);
memcpy(name, name_start, rozmir_mena);
name[rozmir_mena] = '\0'; // Null-terminate the string properly
int index = find_student_name(databaza, rozmer, name);
if (index < 0)
{
memcpy(databaza[rozmer].name, name, rozmir_mena);
databaza[rozmer].holosa = znak;
memcpy(databaza[rozmer].name, name, rozmir_mena + 1); // Include null terminator
databaza[rozmer].votes = znak;
rozmer = rozmer + 1;
}
else {
databaza[index].holosa = databaza[index].holosa + znak;
databaza[index].votes = databaza[index].votes + znak;
}
@ -91,7 +86,7 @@ qsort(databaza, rozmer, sizeof(struct student), funkcia_compare);
printf("Vysledky:\n");
for (int i =0; i < rozmer; i = i + 1)
{
printf("%s: %d\n", databaza[i].name, databaza[i].holosa);
printf("%d %s\n", databaza[i].votes, databaza[i].name);
}
return 0;
}