2024-03-21 21:55:47 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2024-03-21 21:58:36 +00:00
|
|
|
#include <stdbool.h>
|
2024-03-21 22:45:54 +00:00
|
|
|
#include <ctype.h>
|
2024-03-21 17:23:45 +00:00
|
|
|
#define SIZE 100
|
2024-03-21 23:19:20 +00:00
|
|
|
|
2024-03-21 17:23:45 +00:00
|
|
|
|
|
|
|
struct student {
|
2024-03-21 21:55:47 +00:00
|
|
|
char name[SIZE];
|
|
|
|
int votes;
|
2024-03-21 17:23:45 +00:00
|
|
|
};
|
|
|
|
|
2024-03-21 22:31:04 +00:00
|
|
|
// porovnavacia funkcia pre qsort
|
2024-03-21 21:55:47 +00:00
|
|
|
int compare_students(const void *a, const void *b) {
|
|
|
|
const struct student *student_a = (const struct student *)a;
|
|
|
|
const struct student *student_b = (const struct student *)b;
|
2024-03-21 22:10:29 +00:00
|
|
|
|
2024-03-21 22:31:04 +00:00
|
|
|
// ak maju rovnaky pocet hlasov, porovnam podla abecedy
|
|
|
|
if (student_a->votes != student_b->votes) {
|
|
|
|
return (student_b->votes - student_a->votes);
|
2024-03-21 22:10:29 +00:00
|
|
|
}
|
|
|
|
|
2024-03-21 22:31:04 +00:00
|
|
|
// inak porovnam podla poctu hlasov
|
|
|
|
return strcmp(student_a->name, student_b->name);
|
2024-03-21 21:55:47 +00:00
|
|
|
}
|
|
|
|
|
2024-03-21 22:45:54 +00:00
|
|
|
int check_line(char *line, int *votes, char *name) {
|
2024-03-21 22:31:04 +00:00
|
|
|
|
|
|
|
// najdenie prvej medzery v retazci
|
2024-03-21 22:45:54 +00:00
|
|
|
char *space = strchr(line, ' ');
|
2024-03-21 20:06:21 +00:00
|
|
|
|
2024-03-21 22:31:04 +00:00
|
|
|
// kontrola, ci sa nasla medzera
|
2024-03-21 22:45:54 +00:00
|
|
|
if (space == NULL) {
|
2024-03-21 22:31:04 +00:00
|
|
|
return 0; // neplatny riadok
|
2024-03-21 20:06:21 +00:00
|
|
|
}
|
|
|
|
|
2024-03-21 22:31:04 +00:00
|
|
|
//string na cislo premena
|
2024-03-21 21:55:47 +00:00
|
|
|
char *end;
|
|
|
|
*votes = strtol(line, &end, 10);
|
|
|
|
if (*votes == 0) {
|
2024-03-21 22:31:04 +00:00
|
|
|
return 0; // premena sa nepodarila
|
2024-03-21 21:55:47 +00:00
|
|
|
}
|
2024-03-21 22:45:54 +00:00
|
|
|
|
2024-03-21 22:31:04 +00:00
|
|
|
// nastavenie ukazovatela name na miesto za prvou medzerou
|
2024-03-21 22:45:54 +00:00
|
|
|
char *name_start = space + 1;
|
2024-03-21 20:06:21 +00:00
|
|
|
|
2024-03-21 22:31:04 +00:00
|
|
|
// nacitanie mena
|
|
|
|
memset(name, 0, SIZE); // vynulovanie pomocneho pola pre meno
|
|
|
|
int name_size = strlen(name_start) - 1; // velkost mena ale uz bez konca riadka
|
2024-03-21 21:55:47 +00:00
|
|
|
if (name_size > 0) {
|
|
|
|
memcpy(name, name_start, name_size);
|
|
|
|
} else {
|
2024-03-21 22:31:04 +00:00
|
|
|
return 0; // nepodarilo sa nacítat meno
|
2024-03-21 20:06:21 +00:00
|
|
|
}
|
|
|
|
|
2024-03-21 22:31:04 +00:00
|
|
|
return 1;
|
2024-03-21 20:06:21 +00:00
|
|
|
}
|
2024-03-21 17:23:45 +00:00
|
|
|
|
2024-03-21 22:31:04 +00:00
|
|
|
// vyhladanie polozky v databaze
|
2024-03-21 21:55:47 +00:00
|
|
|
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) {
|
2024-03-21 22:31:04 +00:00
|
|
|
return i; // ak najde studenta
|
2024-03-21 21:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
2024-03-21 17:23:45 +00:00
|
|
|
}
|
|
|
|
|
2024-03-21 21:55:47 +00:00
|
|
|
int main() {
|
|
|
|
struct student databaza[SIZE];
|
|
|
|
int size = 0;
|
|
|
|
char line[SIZE];
|
2024-03-21 22:31:04 +00:00
|
|
|
int votes;
|
|
|
|
char name[SIZE];
|
2024-03-21 21:58:36 +00:00
|
|
|
bool chyba = false;
|
2024-03-21 17:23:45 +00:00
|
|
|
|
2024-03-21 22:31:04 +00:00
|
|
|
// nacítanie a kontrola formatu vstupu
|
2024-03-21 21:55:47 +00:00
|
|
|
while (1) {
|
|
|
|
|
|
|
|
char* r = fgets(line,SIZE,stdin);
|
|
|
|
if (r == NULL){
|
2024-03-21 21:58:36 +00:00
|
|
|
printf("Nepodarilo nacitat nic\n");
|
|
|
|
chyba = true;
|
|
|
|
break;
|
2024-03-21 20:06:21 +00:00
|
|
|
}
|
2024-03-21 21:55:47 +00:00
|
|
|
|
|
|
|
if(line[0] == '\n'){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-03-21 22:45:54 +00:00
|
|
|
if (check_line(line, &votes, name) == 0) {
|
2024-03-21 22:04:02 +00:00
|
|
|
printf("Neplatny format");
|
2024-03-21 22:31:04 +00:00
|
|
|
continue; // preskoci riadok a nacita dalsi
|
2024-03-21 21:55:47 +00:00
|
|
|
}
|
2024-03-21 17:23:45 +00:00
|
|
|
|
2024-03-21 22:31:04 +00:00
|
|
|
// ci sa student nachadza v databaze
|
2024-03-21 21:55:47 +00:00
|
|
|
int id = find_student(databaza, size, name);
|
|
|
|
if (id < 0) {
|
2024-03-21 22:31:04 +00:00
|
|
|
// Ak student nie je v databaze ta ho pridaj
|
2024-03-21 21:55:47 +00:00
|
|
|
if (size < SIZE) {
|
|
|
|
strcpy(databaza[size].name, name);
|
|
|
|
databaza[size].votes = votes;
|
|
|
|
size++;
|
|
|
|
} else {
|
2024-03-21 22:04:02 +00:00
|
|
|
printf("Databaza je plna\n");
|
2024-03-21 22:31:04 +00:00
|
|
|
break; // ked je databaza plna break
|
2024-03-21 21:55:47 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-03-21 22:31:04 +00:00
|
|
|
// ak je v databaze aktualizuj pocet hlasov
|
2024-03-21 21:55:47 +00:00
|
|
|
databaza[id].votes += votes;
|
|
|
|
}
|
|
|
|
}
|
2024-03-21 17:23:45 +00:00
|
|
|
|
2024-03-21 22:31:04 +00:00
|
|
|
// triedim studentov
|
2024-03-21 21:55:47 +00:00
|
|
|
qsort(databaza, size, sizeof(struct student), compare_students);
|
2024-03-21 21:58:36 +00:00
|
|
|
|
|
|
|
if (chyba == false){
|
2024-03-21 22:04:02 +00:00
|
|
|
printf("Vysledky:\n");
|
2024-03-21 21:55:47 +00:00
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
printf("%d %s\n", databaza[i].votes, databaza[i].name);
|
|
|
|
}
|
2024-03-21 21:58:36 +00:00
|
|
|
}
|
2024-03-21 21:55:47 +00:00
|
|
|
return 0;
|
2024-03-21 17:23:45 +00:00
|
|
|
}
|