125446
This commit is contained in:
parent
c15f8256ff
commit
efec7c9f44
@ -9,20 +9,21 @@ struct student {
|
||||
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) == 0) {
|
||||
int najdi_studenta(struct student databaza[], int pocet, char meno[]) {
|
||||
int i;
|
||||
for (i = 0; i < pocet; i++) {
|
||||
if (strcmp(databaza[i].name, meno) == 0) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int compare(const void* p1, const void* p2) {
|
||||
struct student* s1 = (struct student*)p1;
|
||||
struct student* s2 = (struct student*)p2;
|
||||
int porovnaj(const void *a, const void *b) {
|
||||
struct student *s1 = (struct student *)a;
|
||||
struct student *s2 = (struct student *)b;
|
||||
|
||||
if (s2->votes != s1->votes) {
|
||||
if (s1->votes != s2->votes) {
|
||||
return s2->votes - s1->votes;
|
||||
}
|
||||
|
||||
@ -31,59 +32,62 @@ int compare(const void* p1, const void* p2) {
|
||||
|
||||
int main() {
|
||||
struct student databaza[SIZE];
|
||||
int pocet_studentov = 0;
|
||||
int valid_input = 0;
|
||||
|
||||
memset(databaza, 0, SIZE * sizeof(struct student));
|
||||
int size = 0;
|
||||
int any_valid_input = 0;
|
||||
|
||||
char line[SIZE];
|
||||
char riadok[SIZE];
|
||||
char meno[SIZE];
|
||||
int hlasy;
|
||||
|
||||
while (fgets(line, SIZE, stdin) != NULL);
|
||||
char* newline = strchr(line, '\n');
|
||||
if (newline) *newline = '\0';
|
||||
while (fgets(riadok, SIZE, stdin) != NULL) {
|
||||
int dlzka = strlen(riadok);
|
||||
if (dlzka > 0 && riadok[dlzka-1] == '\n') {
|
||||
riadok[dlzka-1] = '\0';
|
||||
}
|
||||
|
||||
char* end = NULL;
|
||||
int value = strtol(line, &end, 10);
|
||||
char *koniec;
|
||||
hlasy = strtol(riadok, &koniec, 10);
|
||||
|
||||
if (value <= 0) {
|
||||
if (hlasy <= 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (*end != ' ') {
|
||||
if (*koniec != ' ') {
|
||||
break;
|
||||
}
|
||||
|
||||
char* zaciatok_mena = end + 1;
|
||||
int velkost_mena = strlen(zaciatok_mena);
|
||||
char *meno_start = koniec + 1;
|
||||
|
||||
if (velkost_mena <= 0) {
|
||||
if (strlen(meno_start) == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
char name[SIZE];
|
||||
memset(name, 0, SIZE);
|
||||
memcpy(name, zaciatok_mena, velkost_mena);
|
||||
strcpy(meno, meno_start);
|
||||
|
||||
int id = find_student(databaza, size, name);
|
||||
int pozicia = najdi_studenta(databaza, pocet_studentov, meno);
|
||||
|
||||
if (id < 0) {
|
||||
strcpy(databaza[size].name, name);
|
||||
databaza[size].votes = value;
|
||||
size++;
|
||||
if (pozicia == -1) {
|
||||
strcpy(databaza[pocet_studentov].name, meno);
|
||||
databaza[pocet_studentov].votes = hlasy;
|
||||
pocet_studentov++;
|
||||
} else {
|
||||
databaza[id].votes += value;
|
||||
databaza[pozicia].votes += hlasy;
|
||||
}
|
||||
|
||||
any_valid_input = 1;
|
||||
valid_input = 1;
|
||||
}
|
||||
|
||||
if (!any_valid_input) {
|
||||
if (!valid_input) {
|
||||
printf("Chyba: Nepodarilo sa načítať žiadny platný záznam.\n");
|
||||
return 1;
|
||||
}
|
||||
qsort(databaza, size, sizeof(struct student), compare);
|
||||
qsort(databaza, pocet_studentov, sizeof(struct student), porovnaj);
|
||||
|
||||
printf("Vysledky:\n");
|
||||
for (int i = 0; i < size; i++) {
|
||||
int i;
|
||||
for (i = 0; i < pocet_studentov; i++) {
|
||||
printf("%d %s\n", databaza[i].votes, databaza[i].name);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user