new
This commit is contained in:
parent
0435c4e01d
commit
cfba7868c1
97
du2/program.c
Normal file
97
du2/program.c
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
#include <ctype.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#define SIZE 100
|
||||||
|
struct student
|
||||||
|
|
||||||
|
{
|
||||||
|
char name[SIZE];
|
||||||
|
int holosa;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
int find_student_name(struct student* students, int size, const char* name)
|
||||||
|
{
|
||||||
|
for (int l = 0; l < size; l = l + 1)
|
||||||
|
{
|
||||||
|
if (strcmp(students[l].name, name) == 0)
|
||||||
|
{
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
return s2->holosa - s1->holosa;
|
||||||
|
|
||||||
|
}
|
||||||
|
return strcmp(s1->name, s2->name);
|
||||||
|
}
|
||||||
|
int main() {
|
||||||
|
struct student databaza[SIZE];
|
||||||
|
memset(databaza, 0, SIZE * sizeof(struct student));
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
char* name_start = end + 1;
|
||||||
|
int rozmir_mena = strlen(name_start) - 1;
|
||||||
|
if (rozmir_mena <= 0)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
char name[SIZE];
|
||||||
|
memset(name, 0, SIZE);
|
||||||
|
memcpy(name, name_start, rozmir_mena);
|
||||||
|
|
||||||
|
|
||||||
|
int index = find_student_name(databaza, rozmer, name);
|
||||||
|
if (index < 0)
|
||||||
|
{
|
||||||
|
memcpy(databaza[rozmer].name, name, rozmir_mena);
|
||||||
|
databaza[rozmer].holosa = znak;
|
||||||
|
rozmer = rozmer + 1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
databaza[index].holosa = databaza[index].holosa + znak;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
if (rozmer == 0)
|
||||||
|
{
|
||||||
|
printf("Chyba: Ziadne platne zaznamty.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user