From 89a8c90170dfa5982beb1aa63f6cbfecaa302811 Mon Sep 17 00:00:00 2001 From: Yevhen Kozirovskyi Date: Sat, 5 Oct 2024 11:44:53 +0000 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20cv2/program.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cv2/program.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/cv2/program.c b/cv2/program.c index e69de29..861ecb4 100644 --- a/cv2/program.c +++ b/cv2/program.c @@ -0,0 +1,79 @@ +#include +#include +#include + +#define MAX_LENGTH 100 +#define MAX_DISHES 100 + +char dish_name[MAX_DISHES][MAX_LENGTH]; +char dish_price[MAX_DISHES][MAX_LENGTH]; + +int main() { + int count = 0; + int empty_lines = 0; + + while (count < MAX_DISHES) { + char dish[MAX_LENGTH]; + char price[MAX_LENGTH]; + + fgets(dish, MAX_LENGTH, stdin); + + dish[strcspn(dish, "\n")] = 0; + + if (strlen(dish) == 0) { + empty_lines++; + } else { + empty_lines = 0; + } + if (empty_lines == 2) { + break; + } + + strcpy(dish_name[count], dish); + + fgets(price, MAX_LENGTH, stdin); + + price[strcspn(price, "\n")] = 0; + + if (strlen(price) == 0) { + empty_lines++; + } else { + empty_lines = 0; + } + + if (empty_lines == 2) { + break; + } + + strcpy(dish_price[count], price); + + count++; + } + + + for (int i = 0; i < count - 1; i++) { + for (int j = 0; j < count - i - 1; j++) { + double price1 = atof(dish_price[j]); + double price2 = atof(dish_price[j + 1]); + + if (price1 > price2) { + char temp_name[MAX_LENGTH]; + strcpy(temp_name, dish_name[j]); + strcpy(dish_name[j], dish_name[j + 1]); + strcpy(dish_name[j + 1], temp_name); + + char temp_price[MAX_LENGTH]; + strcpy(temp_price, dish_price[j]); + strcpy(dish_price[j], dish_price[j + 1]); + strcpy(dish_price[j + 1], temp_price); + } + } + } + + for (int i = 0; i < count; i++) { + double price = atof(dish_price[i]); + printf("%s\n%0.6f\n", dish_name[i], price); + } + + return 0; +}