2024-03-21 17:23:45 +00:00
|
|
|
#include<string.h>
|
|
|
|
#include<stdio.h>
|
|
|
|
#include<math.h>
|
|
|
|
#include<stdlib.h>
|
|
|
|
|
|
|
|
#define SIZE 100
|
2024-03-21 20:06:21 +00:00
|
|
|
#define ERROR_OUTPUT stderr
|
|
|
|
|
2024-03-21 17:23:45 +00:00
|
|
|
|
|
|
|
struct student {
|
|
|
|
char name[SIZE];
|
|
|
|
int votes;
|
|
|
|
};
|
|
|
|
|
2024-03-21 20:06:21 +00:00
|
|
|
int check_and_parse_line(char *line, int *votes, char *name) {
|
|
|
|
// Nájdenie prvej medzery v reťazci
|
|
|
|
char *space_pos = strchr(line, ' ');
|
|
|
|
|
|
|
|
// Kontrola, či sa našla medzera
|
|
|
|
if (space_pos == NULL) {
|
|
|
|
return 0; // Neplatný formát riadku
|
|
|
|
}
|
|
|
|
|
|
|
|
// Konverzia počtu hlasov zo substringu reťazca do celého čísla
|
|
|
|
*votes = atoi(line);
|
|
|
|
|
|
|
|
// Nastavenie ukazovateľa name na miesto za prvou medzerou
|
|
|
|
char *name_start = space_pos + 1;
|
|
|
|
|
|
|
|
// Kopírovanie mena zo zvyšku reťazca (za prvou medzerou) do poľa name
|
|
|
|
strcpy(name, name_start);
|
|
|
|
|
|
|
|
// Odstránenie konca riadka zo záznamu mena (ak je prítomný)
|
|
|
|
char *newline_pos = strchr(name, '\n');
|
|
|
|
if (newline_pos != NULL) {
|
|
|
|
*newline_pos = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1; // Platný formát riadku
|
|
|
|
}
|
2024-03-21 17:23:45 +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) {
|
|
|
|
return i; //ak najde studenta
|
|
|
|
}
|
|
|
|
}
|
2024-03-21 18:13:33 +00:00
|
|
|
//printf("nenaslo studenta\n"); // ak nenajde studenta
|
2024-03-21 17:23:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-03-21 20:06:21 +00:00
|
|
|
/*int compare(const void* p1, const void* p2){
|
2024-03-21 17:23:45 +00:00
|
|
|
const struct student* s1 = (const struct student*)p1;
|
|
|
|
const struct student* s2 = (const struct student*)p2;
|
|
|
|
return strcmp(s2->name, s1->name);
|
|
|
|
}
|
2024-03-21 20:06:21 +00:00
|
|
|
*/
|
2024-03-21 17:23:45 +00:00
|
|
|
|
|
|
|
int main(){
|
|
|
|
|
|
|
|
struct student databaza[SIZE];
|
|
|
|
memset(databaza, 0, SIZE*sizeof(struct student)); //inicializujem pamat, vynulujem
|
|
|
|
|
|
|
|
int size = 0;
|
|
|
|
char line[SIZE];
|
2024-03-21 20:06:21 +00:00
|
|
|
int votes;
|
|
|
|
char name[SIZE];
|
|
|
|
|
|
|
|
//nacitanie vstupu
|
|
|
|
while (fgets(line, SIZE, stdin) != NULL){
|
|
|
|
if (!check_line(line, &votes, name)){
|
|
|
|
printf(ERROR_OUTPUT, "chybny format vstupu: %s\n", line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2024-03-21 17:23:45 +00:00
|
|
|
memset(line,0,SIZE);
|
|
|
|
char* r = fgets(line,SIZE,stdin);
|
|
|
|
if (r == NULL){
|
2024-03-21 18:13:33 +00:00
|
|
|
return -1;
|
2024-03-21 17:23:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//nacitanie celeho cisla, premiena kym nepride na znak ktory nieje cislo
|
|
|
|
char* end = NULL;
|
|
|
|
int value = strtol(line,&end,10);
|
|
|
|
if (value == 0){
|
2024-03-21 18:13:33 +00:00
|
|
|
return 1;
|
2024-03-21 17:23:45 +00:00
|
|
|
}
|
|
|
|
// pomocné pole
|
|
|
|
char name[SIZE];
|
|
|
|
// vynulujeme
|
|
|
|
memset(name,0,SIZE);
|
|
|
|
// Vypocitame zaciatok mena
|
|
|
|
// Jedno miesto za medzerou
|
|
|
|
char* start_name = end + 1; //urcenie zaciatku mena
|
|
|
|
// Velkost mena je pocet znakov do konca retazca
|
|
|
|
// minus koniec riadka
|
|
|
|
int name_size = strlen(start_name) - 1; //urcenie konca mena
|
|
|
|
if (name_size > 0){
|
|
|
|
// nakopirujeme
|
|
|
|
memcpy(name,start_name,name_size);
|
|
|
|
// Na konci je v poli name ulozeny retazec s menom
|
|
|
|
// bez konca riadka a s nulou na konci
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf( "nepodarilo sa nacitat meno \n");
|
2024-03-21 18:13:33 +00:00
|
|
|
return 1;
|
2024-03-21 17:23:45 +00:00
|
|
|
}
|
2024-03-21 20:06:21 +00:00
|
|
|
*/
|
2024-03-21 17:23:45 +00:00
|
|
|
//prejde vsetky polozky v databaze
|
|
|
|
|
|
|
|
int id = find_student(databaza,size,name);
|
|
|
|
if (id< 0){
|
|
|
|
if (size < SIZE){
|
|
|
|
memcpy(databaza[size].name,name,name_size);
|
|
|
|
databaza[size].votes = value;
|
|
|
|
size++;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
printf("databaza je plna\n");
|
2024-03-21 18:13:33 +00:00
|
|
|
return 1;
|
2024-03-21 17:23:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
databaza[id].votes = value;
|
|
|
|
}
|
|
|
|
|
2024-03-21 18:13:33 +00:00
|
|
|
printf("Vysledky:\n");
|
|
|
|
for (int i = 0; i < size; i++){
|
|
|
|
printf ("%d %s\n", databaza[i].votes, databaza[i].name);
|
|
|
|
}
|
2024-03-21 17:23:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|