diff --git a/cv2/program b/cv2/program new file mode 100755 index 0000000..3f1d663 Binary files /dev/null and b/cv2/program differ diff --git a/cv2/program.c b/cv2/program.c new file mode 100644 index 0000000..ff7ebb9 --- /dev/null +++ b/cv2/program.c @@ -0,0 +1,84 @@ +#include +#include +#include + +#define LINE_SIZE 100 +#define POCET_JEDAL 100 + +struct pizza { + float price; + char name[LINE_SIZE]; +}; + +void delet(char* str) { + for (int i = 0; str[i] != '\n' && str[i] != 0; i++) { + str[i] = 0; + } +} + +int read_pizza(struct pizza* item) { + + char line[LINE_SIZE]; + memset(line, 0, LINE_SIZE); + + char* r = fgets(line, LINE_SIZE, stdin); + delet(line); + + if (r != NULL && line[1] != 0) { + + char line2[LINE_SIZE]; + + memset(line2, 0, LINE_SIZE); + fgets(line2, LINE_SIZE, stdin); + + float value = strtof(line2, NULL); + + if (value == 0.0F) { + return 0; + } + + item->price = value; + strcpy(item->name, line); + return 1; + } + return 0; +} + +int compare_pizza(const void* a, const void* b) { + const struct pizza* pizza_a = (const struct pizza*)a; + const struct pizza* pizza_b = (const struct pizza*)b; + if (pizza_a->price < pizza_b->price) { + return -1; + } else if (pizza_a->price > pizza_b->price) { + return 1; + } else { + return strcmp(pizza_a->name, pizza_b->name); + } +} + +int main() { + struct pizza menu[POCET_JEDAL]; + memset(menu, 0, sizeof(struct pizza) * POCET_JEDAL); + int counter = 0; + + struct pizza item; + + while (read_pizza(&item)) { + strcpy(menu[counter].name, item.name); + menu[counter].price = item.price; + counter += 1; + + if (counter >= POCET_JEDAL) { + break; + } + } + + qsort(menu, counter, sizeof(struct pizza), compare_pizza); + + for (int c = 0; c < counter; c++) { + printf("%s\n", menu[c].name); + printf("%.6f\n", menu[c].price); + } + + return 0; +}