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