#include #include #include #include #define LINE_SIZE 100 #define LIST_SIZE 100 struct pizza { char name[LINE_SIZE]; float prize; }; char* read_line(); int read_pizza(struct pizza* item); int read_pizza_list(struct pizza* list); int compare(const void* a, const void* b); int main(){ //struct pizza **list_of_pizzas =(struct pizza**)malloc(LIST_SIZE*sizeof(struct pizza *)); struct pizza list_of_pizzas[LIST_SIZE]; memset(list_of_pizzas,0,LIST_SIZE*sizeof(struct pizza)); int number_of_pizzas = read_pizza_list(list_of_pizzas); qsort(list_of_pizzas,number_of_pizzas,sizeof(struct pizza), compare); for(int i = 0; i < number_of_pizzas; i++){ printf("%s", list_of_pizzas[i].name); printf("%.6f\n", list_of_pizzas[i].prize); } return 0; } int read_pizza(struct pizza* item){ char name_of_dish[LIST_SIZE]; memset(name_of_dish,0,LINE_SIZE); char *name_help = fgets(name_of_dish,LINE_SIZE,stdin); if(name_help == NULL || strlen(name_of_dish) == 1){ return 0; }else if (strlen(name_of_dish) == LIST_SIZE - 1 && name_of_dish[LIST_SIZE]!= '\n'){ printf("name of pizza is too long\n"); return 0; } if(name_of_dish!= 0){ strcpy(item->name,name_of_dish); char value_help[LIST_SIZE]; memset(value_help,0,LINE_SIZE); char *value_help1 = fgets(value_help,LIST_SIZE,stdin); if(value_help1 == NULL || strlen(value_help) == 1){ return 0; } float value = strtof(value_help,NULL); if(value!=0.0F){ item->prize=value; return 1; } else return 0; } else return 0; } int read_pizza_list(struct pizza *list){ int counter = 0; for (int i=0; i< LIST_SIZE; i++){ struct pizza item; memset(&item,0,sizeof(struct pizza)); int r = read_pizza(&item); if (r){ // Ak sa nacitanie podarilo, skopirujte polozku do pola memcpy(&list[i],&item,sizeof(struct pizza)); counter += 1; }else{ //free(); // Ak sa nacitanie nepodarilo, nasli sme poslednu polozku // Prerusim nacitanie break; } } return counter; } int compare(const void *a, const void *b){ // a = (struct pizza *)a; // b = (struct pizza *)b; struct pizza *first =(struct pizza *) a; struct pizza *second =(struct pizza *) b; if(first->prize == second->prize){ return strcmp(first->name,second->name); }else{ // printf("%0.6f\n",(first->prize) - (second->prize)); float r = (first->prize) - (second->prize); if (r > 0 ) return 1; else if ( r < 0) return -1; else return 0; } }