This commit is contained in:
Denis Landa 2025-03-21 15:29:19 +01:00
parent da94129580
commit c15f8256ff

View File

@ -1,7 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define SIZE 100
@ -10,75 +9,83 @@ struct student {
int votes;
};
struct student databaza[SIZE];
int size = 0;
int find_student(const char* name) {
int find_student(struct student* students, int size, const char* name) {
for (int i = 0; i < size; i++) {
if (strcmp(databaza[i].name, name) == 0) {
if (strcmp(students[i].name, name) == 0) {
return i;
}
}
return -1;
}
int compare(const void* a, const void* b) {
struct student* s1 = (struct student*)a;
struct student* s2 = (struct student*)b;
int compare(const void* p1, const void* p2) {
struct student* s1 = (struct student*)p1;
struct student* s2 = (struct student*)p2;
if (s2->votes != s1->votes) {
return s2->votes - s1->votes;
}
if (s1->votes > s2->votes) return -1;
if (s1->votes < s2->votes) return 1;
return strcmp(s1->name, s2->name);
}
int main() {
char line[SIZE];
memset(databaza, 0, sizeof(databaza));
struct student databaza[SIZE];
memset(databaza, 0, SIZE * sizeof(struct student));
int size = 0;
int any_valid_input = 0;
while (fgets(line, SIZE, stdin)) {
char* end;
int votes = strtol(line, &end, 10);
if (*end != ' ') {
printf("Chyba: Neplatny format vstupu!\n");
return 1;
char line[SIZE];
while (fgets(line, SIZE, stdin) != NULL);
char* newline = strchr(line, '\n');
if (newline) *newline = '\0';
char* end = NULL;
int value = strtol(line, &end, 10);
if (value <= 0) {
break;
}
end++;
if (*end == '\0' || *end == '\n') {
printf("Chyba: Neplatny format vstupu!\n");
return 1;
if (*end != ' ') {
break;
}
char* zaciatok_mena = end + 1;
int velkost_mena = strlen(zaciatok_mena);
if (velkost_mena <= 0) {
break;
}
char name[SIZE];
memset(name, 0, SIZE);
strncpy(name, end, SIZE - 1);
name[strcspn(name, "\n")] = '\0';
memcpy(name, zaciatok_mena, velkost_mena);
int id = find_student(databaza, size, name);
int id = find_student(name);
if (id < 0) {
if (size >= SIZE) {
printf("Chyba: Databaza je plna!\n");
return 1;
}
strncpy(databaza[size].name, name, SIZE - 1);
databaza[size].votes = votes;
strcpy(databaza[size].name, name);
databaza[size].votes = value;
size++;
} else {
databaza[id].votes += votes;
databaza[id].votes += value;
}
any_valid_input = 1;
}
if (size == 0) {
printf("Chyba: Nebol nacitany ziadny platny zaznam!\n");
if (!any_valid_input) {
printf("Chyba: Nepodarilo sa načítať žiadny platný záznam.\n");
return 1;
}
qsort(databaza, size, sizeof(struct student), compare);
printf("Vysledky:\n");
for (int i = 0; i < size; i++) {
printf("%d %s\n", databaza[i].votes, databaza[i].name);
}
return 0;
}