usaa24/cv2/program.c

76 lines
1.9 KiB
C
Raw Normal View History

2024-10-05 11:44:53 +00:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LENGTH 100
#define MAX_DISHES 100
char dish_name[MAX_DISHES][MAX_LENGTH];
2024-10-05 11:48:13 +00:00
double dish_price[MAX_DISHES];
2024-10-05 11:44:53 +00:00
int main() {
int count = 0;
int empty_lines = 0;
while (count < MAX_DISHES) {
char dish[MAX_LENGTH];
fgets(dish, MAX_LENGTH, stdin);
dish[strcspn(dish, "\n")] = 0;
if (strlen(dish) == 0) {
empty_lines++;
} else {
empty_lines = 0;
}
if (empty_lines == 2) {
break;
}
strcpy(dish_name[count], dish);
2024-10-05 11:48:13 +00:00
char price[MAX_LENGTH];
2024-10-05 11:44:53 +00:00
fgets(price, MAX_LENGTH, stdin);
price[strcspn(price, "\n")] = 0;
if (strlen(price) == 0) {
empty_lines++;
} else {
empty_lines = 0;
}
if (empty_lines == 2) {
break;
}
2024-10-05 11:48:13 +00:00
// Преобразуем цену в число с плавающей запятой
dish_price[count] = atof(price);
2024-10-05 11:44:53 +00:00
count++;
}
2024-10-05 11:48:13 +00:00
// Сортируем блюда по цене
2024-10-05 11:44:53 +00:00
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - i - 1; j++) {
2024-10-05 11:48:13 +00:00
if (dish_price[j] > dish_price[j + 1]) {
// Меняем местами цены
double temp_price = dish_price[j];
dish_price[j] = dish_price[j + 1];
dish_price[j + 1] = temp_price;
2024-10-05 11:44:53 +00:00
2024-10-05 11:48:13 +00:00
// Меняем местами названия
2024-10-05 11:44:53 +00:00
char temp_name[MAX_LENGTH];
strcpy(temp_name, dish_name[j]);
strcpy(dish_name[j], dish_name[j + 1]);
strcpy(dish_name[j + 1], temp_name);
}
}
}
2024-10-05 11:48:13 +00:00
// Выводим отсортированные блюда и их цены
2024-10-05 11:44:53 +00:00
for (int i = 0; i < count; i++) {
2024-10-05 11:48:13 +00:00
printf("%s\n%.6f\n", dish_name[i], dish_price[i]);
2024-10-05 11:44:53 +00:00
}
return 0;
}