From 594d3cd491450ae71178a3b6a9b94747a121b673 Mon Sep 17 00:00:00 2001 From: Maryna Kravtsova Date: Sat, 24 Oct 2020 17:06:11 +0000 Subject: [PATCH] Update 'cv2/program.c' --- cv2/program.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/cv2/program.c b/cv2/program.c index e4ad9a0..fe26dbb 100644 --- a/cv2/program.c +++ b/cv2/program.c @@ -9,6 +9,9 @@ struct pizza{ float prize; }; +//read the name and prize +//push them to stack + int read_pizza(struct pizza* item){ char line1[LINESIZE]; @@ -29,12 +32,15 @@ int read_pizza(struct pizza* item){ return 1; } } + +//compare 2 elements + int compare(const void *a, const void *b){ struct pizza* pizza_a = (void*) a; struct pizza* pizza_b = (void*) b; float r = (pizza_a->prize > pizza_b->prize) - (pizza_a->prize < pizza_b->prize); - if (r == 0){ + if (r == 0){ //if prizes are identical, than check if names are in alphabetical order r = strcmp(pizza_a->name, pizza_b->name); } return r; @@ -48,12 +54,13 @@ int main(){ struct pizza item; int counter = 0; + //write menu to array while(stdin, read_pizza(&item)){ strcpy(jedalny_listok[counter].name, item.name); jedalny_listok[counter].prize = item.prize; counter++; } - + qsort(jedalny_listok, counter, sizeof(struct pizza), compare); int i = 0;