From 0cd1c038bd142e4ac484a8824295c7e083925966 Mon Sep 17 00:00:00 2001 From: Yevhen Kozirovskyi Date: Sat, 5 Oct 2024 12:24:34 +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 | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/cv2/program.c b/cv2/program.c index 201c3e5..f1982d3 100644 --- a/cv2/program.c +++ b/cv2/program.c @@ -11,6 +11,7 @@ double dish_price[MAX_DISHES]; int main() { int count = 0; + // Считывание данных while (count < MAX_DISHES) { char dish[MAX_LENGTH]; char price[MAX_LENGTH]; @@ -32,46 +33,38 @@ int main() { char *endptr; double current_price = strtod(price, &endptr); if (*endptr != '\0' || strlen(price) == 0) { - continue; + continue; // Если цена введена неверно, пропустить } - int found = 0; - for (int i = 0; i < count; i++) { - if (strcmp(dish_name[i], dish) == 0) { - found = 1; - if (current_price < dish_price[i]) { - dish_price[i] = current_price; - } - break; - } - } - - if (!found) { - strcpy(dish_name[count], dish); - dish_price[count] = current_price; - count++; - } + // Сохранение блюда и его цены + strcpy(dish_name[count], dish); + dish_price[count] = current_price; + count++; } + // Сортировка блюд по цене и имени for (int i = 0; i < count - 1; i++) { for (int j = 0; j < count - i - 1; j++) { if (dish_price[j] > dish_price[j + 1] || (dish_price[j] == dish_price[j + 1] && strcmp(dish_name[j], dish_name[j + 1]) > 0)) { + // Меняем местами цены double temp_price = dish_price[j]; dish_price[j] = dish_price[j + 1]; dish_price[j + 1] = temp_price; + // Меняем местами названия 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); - } + } } } + // Выводим результаты for (int i = 0; i < count; i++) { printf("%s\n%.6f\n", dish_name[i], dish_price[i]); } return 0; -} \ No newline at end of file +}