cv5
This commit is contained in:
parent
c28dff4800
commit
c4ae619f7f
@ -4,12 +4,40 @@
|
|||||||
#include<stdlib.h>
|
#include<stdlib.h>
|
||||||
|
|
||||||
#define SIZE 100
|
#define SIZE 100
|
||||||
|
#define ERROR_OUTPUT stderr
|
||||||
|
|
||||||
|
|
||||||
struct student {
|
struct student {
|
||||||
char name[SIZE];
|
char name[SIZE];
|
||||||
int votes;
|
int votes;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
int find_student(struct student* students,int size, const char* name){
|
int find_student(struct student* students,int size, const char* name){
|
||||||
for (int i = 0; i < size; i++){
|
for (int i = 0; i < size; i++){
|
||||||
@ -22,12 +50,12 @@ int find_student(struct student* students,int size, const char* name){
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int compare(const void* p1, const void* p2){
|
/*int compare(const void* p1, const void* p2){
|
||||||
const struct student* s1 = (const struct student*)p1;
|
const struct student* s1 = (const struct student*)p1;
|
||||||
const struct student* s2 = (const struct student*)p2;
|
const struct student* s2 = (const struct student*)p2;
|
||||||
return strcmp(s2->name, s1->name);
|
return strcmp(s2->name, s1->name);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
|
|
||||||
@ -35,9 +63,20 @@ int main(){
|
|||||||
memset(databaza, 0, SIZE*sizeof(struct student)); //inicializujem pamat, vynulujem
|
memset(databaza, 0, SIZE*sizeof(struct student)); //inicializujem pamat, vynulujem
|
||||||
|
|
||||||
int size = 0;
|
int size = 0;
|
||||||
|
|
||||||
//nacitanie jednej polozky do databazy
|
|
||||||
char line[SIZE];
|
char line[SIZE];
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
memset(line,0,SIZE);
|
memset(line,0,SIZE);
|
||||||
char* r = fgets(line,SIZE,stdin);
|
char* r = fgets(line,SIZE,stdin);
|
||||||
if (r == NULL){
|
if (r == NULL){
|
||||||
@ -70,7 +109,7 @@ else {
|
|||||||
printf( "nepodarilo sa nacitat meno \n");
|
printf( "nepodarilo sa nacitat meno \n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
//prejde vsetky polozky v databaze
|
//prejde vsetky polozky v databaze
|
||||||
|
|
||||||
int id = find_student(databaza,size,name);
|
int id = find_student(databaza,size,name);
|
||||||
|
Loading…
Reference in New Issue
Block a user