diff --git a/cv2/program.c b/cv2/program.c new file mode 100644 index 0000000..8b00774 --- /dev/null +++ b/cv2/program.c @@ -0,0 +1,83 @@ +#include +#include +#include +#include + +struct pizza +{ + char* name; + float price; +}; + +int comparator(const void* a, const void* b) +{ + + struct pizza A = *(struct pizza*)a; + struct pizza B = *(struct pizza*)b; + + if (A.price < B.price) + { + return -1; + } + if (A.price == B.price) + { + return 0; + } + if (A.price > B.price) + { + return 1; + } +} + +int main(int argc, char const *argv[]) +{ + int itemc = 0; + char find[20]; + struct pizza menu[20]; + char buf[50]; + float price = 0; + memset(buf, 0, 50); + memset(find, 0, 20); + + + while (fgets(buf, 50, stdin) != NULL) + { + buf[strcspn(buf, "\n")] = '\0'; + + if (scanf("%f", &price) != 1) + { + break; + } + + getchar(); + + struct pizza piteml = + { + .name = (char*)malloc(strlen(buf) * sizeof(char*)), + .price = price + }; + + strcpy(piteml.name,buf); + + menu[itemc] = piteml; + + itemc++; + if (itemc >= 20) + { + break; + } + } + + qsort(menu, itemc, sizeof(struct pizza), comparator); + + for (int i = 0; i < itemc; i++) + { + printf("%s\n%.6f\n", menu[i].name, menu[i].price); + } + + for (int i = 0; i < itemc; i++) + { + free(menu[i].name); + } + return 0; +}