pvjc24/cv5/program.c

108 lines
3.0 KiB
C
Raw Normal View History

2024-03-16 19:11:32 +00:00
#include <stdio.h>
#include <stdlib.h>
2024-03-16 19:38:13 +00:00
#include <string.h>
2024-03-16 19:11:32 +00:00
#define SIZE 100
struct student {
char name[SIZE];
int score;
};
2024-03-16 19:38:13 +00:00
int compar(const void*,const void*);
int loading_the_entire_line_from_standard_input_into_the_helper_field_and_verify_that_i_have_loaded_it_correctly(struct student *databaza,int *size_ptr);
void parce_line_and_save_into_database(char line[],struct student *databaza, int *size_ptr);
int retriving_integer(char *line,char** end);
int find_index(struct student databaza[],int size, const char *xxx);
void adding_an_item_to_the_database(struct student databaza[],int *size_ptr, const char* name,int score);
2024-03-16 19:11:32 +00:00
int main()
{
struct student databaza[SIZE];
memset(databaza,0,SIZE*sizeof(struct student));
int size = 0;
while(loading_the_entire_line_from_standard_input_into_the_helper_field_and_verify_that_i_have_loaded_it_correctly(databaza,&size)){
}
int element_size=SIZE+sizeof(int);
qsort((void *)databaza, size,element_size,compar);
2024-03-16 19:14:48 +00:00
printf("Vysledky:\n");
2024-03-16 19:11:32 +00:00
for(int i=0; i<size; i++){
2024-03-16 19:13:04 +00:00
printf("%d %s\n",databaza[i].score,databaza[i].name);
2024-03-16 19:11:32 +00:00
}
return 0;
}
int loading_the_entire_line_from_standard_input_into_the_helper_field_and_verify_that_i_have_loaded_it_correctly(struct student *databaza,int *size_ptr){
char line[SIZE];
memset(line,0,SIZE);
char* r = fgets(line,SIZE,stdin);
2024-03-16 19:38:13 +00:00
if (r == NULL||strlen(r)==1){
2024-03-16 19:11:32 +00:00
return 0;
}
parce_line_and_save_into_database(line,databaza,size_ptr);
return 1;
}
void parce_line_and_save_into_database(char line[],struct student *databaza, int *size_ptr){
char* end = NULL;
int score=retriving_integer(line,&end);
if(score==0){
return;
}
char name[SIZE];
memset(name,0,SIZE);
end += 1;
int velkost_mena = strlen(end) - 1;
if (velkost_mena > 0){
memcpy(name,end,velkost_mena);
}
else {
return;
}
adding_an_item_to_the_database( databaza, size_ptr, name, score);
}
int retriving_integer(char *line,char** end){
int value = strtol(line,end,10);
if (value == 0){
return 0;
}
return value;
}
int find_index(struct student databaza[],int size, const char *xxx){
for(int i=0; i<size; i++){
if(strcmp(databaza[i].name,xxx)==0){
return i;
}
}
return -1;
}
void adding_an_item_to_the_database(struct student databaza[],int *size_ptr, const char* name,int score){
int size_of_Name= strlen(name);
int id = find_index(databaza,*size_ptr,name);
if (id< 0){
memcpy(databaza[*size_ptr].name,name,size_of_Name);
databaza[*size_ptr].score=score;
*size_ptr+=1;
}else {
databaza[id].score+=score;
}
}
int compar(const void* a,const void* b){
2024-03-16 19:38:13 +00:00
const struct student * a_ptr = a;
const struct student * b_ptr = b;
2024-03-16 19:11:32 +00:00
if(a_ptr->score < b_ptr->score) {
return 1;
}
2024-03-16 19:38:13 +00:00
if(a_ptr->score > b_ptr->score) {
2024-03-16 19:11:32 +00:00
return -1;
}
if(a_ptr->score == b_ptr->score){
return strcmp(a_ptr->name, b_ptr->name);
}
}