Обновить cv2/program.c

This commit is contained in:
Yevhen Kozirovskyi 2024-10-05 12:24:34 +00:00
parent 34471f35b5
commit 0cd1c038bd

View File

@ -11,6 +11,7 @@ double dish_price[MAX_DISHES];
int main() { int main() {
int count = 0; int count = 0;
// Считывание данных
while (count < MAX_DISHES) { while (count < MAX_DISHES) {
char dish[MAX_LENGTH]; char dish[MAX_LENGTH];
char price[MAX_LENGTH]; char price[MAX_LENGTH];
@ -32,43 +33,35 @@ int main() {
char *endptr; char *endptr;
double current_price = strtod(price, &endptr); double current_price = strtod(price, &endptr);
if (*endptr != '\0' || strlen(price) == 0) { if (*endptr != '\0' || strlen(price) == 0) {
continue; continue; // Если цена введена неверно, пропустить
} }
int found = 0; // Сохранение блюда и его цены
for (int i = 0; i < count; i++) { strcpy(dish_name[count], dish);
if (strcmp(dish_name[i], dish) == 0) { dish_price[count] = current_price;
found = 1; count++;
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++) { for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - i - 1; j++) { for (int j = 0; j < count - i - 1; j++) {
if (dish_price[j] > dish_price[j + 1] || if (dish_price[j] > dish_price[j + 1] ||
(dish_price[j] == dish_price[j + 1] && strcmp(dish_name[j], dish_name[j + 1]) > 0)) { (dish_price[j] == dish_price[j + 1] && strcmp(dish_name[j], dish_name[j + 1]) > 0)) {
// Меняем местами цены
double temp_price = dish_price[j]; double temp_price = dish_price[j];
dish_price[j] = dish_price[j + 1]; dish_price[j] = dish_price[j + 1];
dish_price[j + 1] = temp_price; dish_price[j + 1] = temp_price;
// Меняем местами названия
char temp_name[MAX_LENGTH]; char temp_name[MAX_LENGTH];
strcpy(temp_name, dish_name[j]); strcpy(temp_name, dish_name[j]);
strcpy(dish_name[j], dish_name[j + 1]); strcpy(dish_name[j], dish_name[j + 1]);
strcpy(dish_name[j + 1], temp_name); strcpy(dish_name[j + 1], temp_name);
} }
} }
} }
// Выводим результаты
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
printf("%s\n%.6f\n", dish_name[i], dish_price[i]); printf("%s\n%.6f\n", dish_name[i], dish_price[i]);
} }