usaa24/cv2/program.c

71 lines
2.0 KiB
C
Raw Permalink 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;
2024-10-05 12:24:34 +00:00
// Считывание данных
2024-10-05 11:44:53 +00:00
while (count < MAX_DISHES) {
char dish[MAX_LENGTH];
2024-10-05 11:51:59 +00:00
char price[MAX_LENGTH];
2024-10-05 11:44:53 +00:00
2024-10-05 11:51:59 +00:00
if (fgets(dish, MAX_LENGTH, stdin) == NULL) {
2024-10-05 11:55:41 +00:00
break;
2024-10-05 11:51:59 +00:00
}
2024-10-05 11:55:41 +00:00
dish[strcspn(dish, "\n")] = 0;
2024-10-05 11:44:53 +00:00
if (strlen(dish) == 0) {
2024-10-05 11:55:41 +00:00
break;
2024-10-05 11:44:53 +00:00
}
2024-10-05 12:02:03 +00:00
if (fgets(price, MAX_LENGTH, stdin) == NULL || strlen(price) == 0) {
2024-10-05 11:55:41 +00:00
break;
2024-10-05 11:44:53 +00:00
}
2024-10-05 11:55:41 +00:00
price[strcspn(price, "\n")] = 0;
2024-10-05 11:44:53 +00:00
2024-10-05 12:02:03 +00:00
char *endptr;
double current_price = strtod(price, &endptr);
if (*endptr != '\0' || strlen(price) == 0) {
2024-10-05 12:24:34 +00:00
continue; // Если цена введена неверно, пропустить
2024-10-05 11:44:53 +00:00
}
2024-10-05 12:24:34 +00:00
// Сохранение блюда и его цены
strcpy(dish_name[count], dish);
dish_price[count] = current_price;
count++;
2024-10-05 11:44:53 +00:00
}
2024-10-05 12:24:34 +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:55:41 +00:00
if (dish_price[j] > dish_price[j + 1] ||
(dish_price[j] == dish_price[j + 1] && strcmp(dish_name[j], dish_name[j + 1]) > 0)) {
2024-10-05 12:24:34 +00:00
// Меняем местами цены
2024-10-05 11:48:13 +00:00
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 12:24:34 +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 12:24:34 +00:00
}
2024-10-05 11:44:53 +00:00
}
}
2024-10-05 12:24:34 +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;
2024-10-05 12:24:34 +00:00
}