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