From d9759f60758a32622cc4b7fcdad53f6d3d76eebf Mon Sep 17 00:00:00 2001 From: Maryna Kravtsova Date: Mon, 5 Oct 2020 14:30:37 +0200 Subject: [PATCH] program --- cv2/program.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 cv2/program.c diff --git a/cv2/program.c b/cv2/program.c new file mode 100644 index 0000000..e1cb04b --- /dev/null +++ b/cv2/program.c @@ -0,0 +1,79 @@ +#include +#include +#include + +#define LINESIZE 100 + +struct pizza{ + char name[LINESIZE]; + float prize; +}; + +int read_pizza(struct pizza* item){ + + char line1[LINESIZE]; + char line2[LINESIZE]; + + memset(line1, 0, LINESIZE); + memset(line2, 0, LINESIZE); + char *l1 = fgets(line1,LINESIZE,stdin); + char *l2 = fgets(line2,LINESIZE,stdin); + + //line1[strlen(line1)-1] = '\0'; + //line2[strlen(line2)-1] = '\0'; + + float value = strtof(line2, &l2); + if(value == 0.0F){ + return 0; + } + if(l1 != NULL && line1[1] != 0){ + item->prize = value; + strcpy(item->name, line1); + return 1; + } +} +int comparename(const void *a, const void *b){ + + struct pizza* pizza_a = (void*) a; + struct pizza* pizza_b = (void*) b; + + return strcmp(pizza_a->name, pizza_b->name); + +} +int compareprize(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); + return r; +} + +int main(){ + + struct pizza jedalny_listok[100]; + memset(jedalny_listok, 0 , sizeof(struct pizza)*100); + + struct pizza item; + int counter = 0; + + 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), compareprize); + + //qsort(jedalny_listok, counter, sizeof(struct pizza), comparename); + + int i = 0; + for(i = 0; i < counter; i++){ + printf("%s", jedalny_listok[i].name); + printf("%f", jedalny_listok[i].prize); + printf("\n"); + } + + + return 0; +}