This commit is contained in:
Denis Landa 2025-03-21 15:17:39 +01:00
parent f7323ac3c7
commit 29911df6d3

View File

@ -4,7 +4,7 @@
#define SIZE 100
struct student {
struct student{
char name[SIZE];
int votes;
};
@ -20,6 +20,7 @@ int find_student(const char* name){
}
return -1;
}
int compare(const void* a, const void* b) {
struct student* s1 = (struct student*)a;
struct student* s2 = (struct student*)b;
@ -36,18 +37,30 @@ int compare(const void* a, const void* b) {
int main() {
char line[SIZE];
memset(databaza, 0, sizeof(databaza));
while (fgets(line, SIZE, stdin)) {
char* end = NULL;
int votes = strtol(line, &end, 10);
if (votes == 0 && end == line) {
printf("Chyba: Neplatny format vstupu!\n");
continue;
}
while (*end == ' ') {
end++;
}
if (*end == '\0' || *end == '\n') {
printf("Chyba: Neplatny format - chyba meno!\n");
continue;
}
char name[SIZE];
memset(name, 0, SIZE);
strncpy(name, end + 1, strlen(end + 1) - 1);
strncpy(name, end, SIZE - 1);
name[strcspn(name, "\n")] = '\0';
int id = find_student(name);
if (id < 0){
if (size >= SIZE){
if (id < 0) {
if (size >= SIZE) {
printf("Chyba: Databaza je plna!\n");
continue;
}
@ -55,14 +68,12 @@ int main() {
strncpy(databaza[size].name, name, SIZE - 1);
databaza[size].votes = votes;
size++;
}
else
{
} else {
databaza[id].votes += votes;
}
}
if (size == 0){
if (size == 0) {
printf("Chyba: Nebol nacitany ziadny platny zaznam!\n");
return 1;
}
@ -75,3 +86,4 @@ int main() {
}
return 0;
}