Обновить cv2/program.c
This commit is contained in:
parent
34471f35b5
commit
0cd1c038bd
@ -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,35 +33,26 @@ 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++;
|
||||
}
|
||||
}
|
||||
|
||||
// Сортировка блюд по цене и имени
|
||||
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]);
|
||||
@ -69,6 +61,7 @@ int main() {
|
||||
}
|
||||
}
|
||||
|
||||
// Выводим результаты
|
||||
for (int i = 0; i < count; i++) {
|
||||
printf("%s\n%.6f\n", dish_name[i], dish_price[i]);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user