From c7673f34d34d3e18cc20ff06e9844adeaf971d04 Mon Sep 17 00:00:00 2001 From: vj586da Date: Thu, 14 Oct 2021 15:20:13 +0200 Subject: [PATCH] first commit cv2 --- cv2/program.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 cv2/program.c diff --git a/cv2/program.c b/cv2/program.c new file mode 100644 index 0000000..72d3ac2 --- /dev/null +++ b/cv2/program.c @@ -0,0 +1,68 @@ +#include +#include +#include + + + +#define SIZE 100 +#define NAMESIZE 30 + + + + +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); + +} + +int compare_price(const void* c1, const void* c2){ + struct menu* p1 = (struct menu* ) c1; + struct menu* p2 = (struct menu* ) c2; + return p1->price - p2->price; + +} + +int main(){ + + + + struct menu food_database[SIZE]; + memset(food_database, 0, SIZE*sizeof(struct menu)); + + + + // 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 ){ + break; + } + meal_counter++; + } + + qsort(food_database,meal_counter,sizeof(struct menu),compare_name); + qsort(food_database,meal_counter,sizeof(struct menu),compare_price); + for(int i=0; i