From 34471f35b5a97f897565654a0e9a259d1e19189a Mon Sep 17 00:00:00 2001 From: Yevhen Kozirovskyi Date: Sat, 5 Oct 2024 12:02:03 +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 | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/cv2/program.c b/cv2/program.c index d5c2554..201c3e5 100644 --- a/cv2/program.c +++ b/cv2/program.c @@ -23,19 +23,34 @@ int main() { if (strlen(dish) == 0) { break; } - strcpy(dish_name[count], dish); - if (fgets(price, MAX_LENGTH, stdin) == NULL) { + if (fgets(price, MAX_LENGTH, stdin) == NULL || strlen(price) == 0) { break; } price[strcspn(price, "\n")] = 0; - if (strlen(price) == 0) { - break; + char *endptr; + double current_price = strtod(price, &endptr); + if (*endptr != '\0' || strlen(price) == 0) { + continue; } - dish_price[count] = atof(price); - count++; + 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++; + } } for (int i = 0; i < count - 1; i++) {