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++) {