From b0efda82987eb5d68a1011d15482f03fc23a2893 Mon Sep 17 00:00:00 2001 From: vj586da Date: Thu, 14 Oct 2021 16:42:58 +0200 Subject: [PATCH] comments --- cv2/program.c | 94 +++++++++++++++++++++++++++++---------------------- 1 file changed, 53 insertions(+), 41 deletions(-) diff --git a/cv2/program.c b/cv2/program.c index bbb2330..6f17de5 100644 --- a/cv2/program.c +++ b/cv2/program.c @@ -8,20 +8,61 @@ #define NAMESIZE 50 - - struct menu { char name[SIZE]; float price; }; -int compare_name(const void* c1, const void* c2){ - struct menu* p1 = (struct menu* ) c1; - struct menu* p2 = (struct menu* ) c2; - return strcmp(p1->name,p2->name); +// function to compare names alphabetically +int compare_name(const void* c1, const void* c2); +// function to compare floats +int compare_float(const void* c1, const void* c2); + + + + +int main(){ + + + + struct menu food_database[SIZE]; + memset(food_database, 0, SIZE*sizeof(struct menu)); + + + + int meal_counter = 0; + char temp[20]; + char* ptr1 = NULL; + char* ptr2 = NULL; + while(1){ + // load name into struct + ptr1 = fgets(food_database[meal_counter].name, NAMESIZE, stdin); + if ( ptr1 == NULL ){ + break; + } + // load price into struct + ptr2 = fgets(temp, 20, stdin); + food_database[meal_counter].price = strtof(temp, NULL); + if ( ptr2 == NULL || food_database[meal_counter].price == 0){ + break; + } + meal_counter++; + } + // first sort by name, then by price + qsort(food_database,meal_counter,sizeof(struct menu),compare_name); + qsort(food_database,meal_counter,sizeof(struct menu),compare_float); + // print results + for(int i=0; iname,p2->name); - - - // load name into struct - int meal_counter = 0; - char temp[20]; - char* ptr1 = NULL; - char* ptr2 = NULL; - while(1){ - ptr1 = fgets(food_database[meal_counter].name, NAMESIZE, stdin); - if ( ptr1 == NULL ){ - break; - } - ptr2 = fgets(temp, 20, stdin); - food_database[meal_counter].price = strtof(temp, NULL); - if ( ptr2 == NULL || food_database[meal_counter].price == 0){ - break; - } - meal_counter++; - } - - qsort(food_database,meal_counter,sizeof(struct menu),compare_name); - qsort(food_database,meal_counter,sizeof(struct menu),compare_float); - for(int i=0; i