push
This commit is contained in:
parent
348ec980d4
commit
76516ba3d8
111
du2/program.c
Normal file
111
du2/program.c
Normal file
@ -0,0 +1,111 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define SIZE 100
|
||||
|
||||
struct student {
|
||||
char meno[SIZE];
|
||||
int hlasy;
|
||||
};
|
||||
|
||||
int najdi_studenta(struct student* studenti, int pocet, const char* meno) {
|
||||
|
||||
for (int i = 0; i < pocet; i++) {
|
||||
if (strcmp(studenti[i].meno, meno) == 0) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int porovnaj(const void* p1, const void* p2) {
|
||||
|
||||
struct student* s1 = (struct student*)p1;
|
||||
struct student* s2 = (struct student*)p2;
|
||||
|
||||
if (s2->hlasy > s1->hlasy) return -1;
|
||||
if (s2->hlasy < s1->hlasy) return 1;
|
||||
|
||||
return strcmp(s1->meno, s2->meno);
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
struct student databaza[SIZE];
|
||||
memset(databaza, 0, SIZE * sizeof(struct student));
|
||||
|
||||
int pocet = 0;
|
||||
char riadok[SIZE];
|
||||
|
||||
while (1) {
|
||||
|
||||
memset(riadok, 0, SIZE);
|
||||
char* vysledok = fgets(riadok, SIZE, stdin);
|
||||
|
||||
if (vysledok == NULL) {
|
||||
break;
|
||||
}
|
||||
|
||||
char* koniec = NULL;
|
||||
int hodnota = (int)strtol(riadok, &koniec, 10);
|
||||
|
||||
if (koniec == riadok) {
|
||||
continue;
|
||||
}
|
||||
|
||||
while (*koniec == ' ') {
|
||||
koniec++;
|
||||
}
|
||||
|
||||
char* zaciatok_mena = koniec;
|
||||
|
||||
if (*zaciatok_mena == '\n' || *zaciatok_mena == '\0') {
|
||||
continue;
|
||||
}
|
||||
|
||||
char meno[SIZE];
|
||||
memset(meno, 0, SIZE);
|
||||
|
||||
int velkost_mena = (int)strlen(zaciatok_mena);
|
||||
|
||||
if (zaciatok_mena[velkost_mena - 1] == '\n') {
|
||||
velkost_mena--;
|
||||
}
|
||||
|
||||
if (velkost_mena <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
memcpy(meno, zaciatok_mena, velkost_mena);
|
||||
meno[velkost_mena] = '\0';
|
||||
|
||||
int index = najdi_studenta(databaza, pocet, meno);
|
||||
|
||||
if (index < 0) {
|
||||
|
||||
if (pocet >= SIZE) {
|
||||
break;
|
||||
}
|
||||
|
||||
memcpy(databaza[pocet].meno, meno, velkost_mena);
|
||||
databaza[pocet].meno[velkost_mena] = '\0';
|
||||
|
||||
databaza[pocet].hlasy = hodnota;
|
||||
pocet++;
|
||||
|
||||
} else {
|
||||
|
||||
databaza[index].hlasy += hodnota;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
qsort(databaza, pocet, sizeof(struct student), porovnaj);
|
||||
|
||||
for (int i = 0; i < pocet; i++) {
|
||||
printf("%s %d\n", databaza[i].meno, databaza[i].hlasy);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user