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

This commit is contained in:
Yevhen Kozirovskyi 2024-10-05 11:48:13 +00:00
parent 89a8c90170
commit 2ed049575f

View File

@ -6,7 +6,7 @@
#define MAX_DISHES 100 #define MAX_DISHES 100
char dish_name[MAX_DISHES][MAX_LENGTH]; char dish_name[MAX_DISHES][MAX_LENGTH];
char dish_price[MAX_DISHES][MAX_LENGTH]; double dish_price[MAX_DISHES];
int main() { int main() {
int count = 0; int count = 0;
@ -14,10 +14,8 @@ int main() {
while (count < MAX_DISHES) { while (count < MAX_DISHES) {
char dish[MAX_LENGTH]; char dish[MAX_LENGTH];
char price[MAX_LENGTH];
fgets(dish, MAX_LENGTH, stdin); fgets(dish, MAX_LENGTH, stdin);
dish[strcspn(dish, "\n")] = 0; dish[strcspn(dish, "\n")] = 0;
if (strlen(dish) == 0) { if (strlen(dish) == 0) {
@ -31,8 +29,8 @@ int main() {
strcpy(dish_name[count], dish); strcpy(dish_name[count], dish);
char price[MAX_LENGTH];
fgets(price, MAX_LENGTH, stdin); fgets(price, MAX_LENGTH, stdin);
price[strcspn(price, "\n")] = 0; price[strcspn(price, "\n")] = 0;
if (strlen(price) == 0) { if (strlen(price) == 0) {
@ -45,34 +43,32 @@ int main() {
break; break;
} }
strcpy(dish_price[count], price); // Преобразуем цену в число с плавающей запятой
dish_price[count] = atof(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++) {
double price1 = atof(dish_price[j]); if (dish_price[j] > dish_price[j + 1]) {
double price2 = atof(dish_price[j + 1]); // Меняем местами цены
double temp_price = dish_price[j];
dish_price[j] = dish_price[j + 1];
dish_price[j + 1] = temp_price;
if (price1 > price2) { // Меняем местами названия
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);
char temp_price[MAX_LENGTH];
strcpy(temp_price, dish_price[j]);
strcpy(dish_price[j], dish_price[j + 1]);
strcpy(dish_price[j + 1], temp_price);
} }
} }
} }
// Выводим отсортированные блюда и их цены
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
double price = atof(dish_price[i]); printf("%s\n%.6f\n", dish_name[i], dish_price[i]);
printf("%s\n%0.6f\n", dish_name[i], price);
} }
return 0; return 0;