usaa24/cv2/program.c
2024-10-09 10:08:03 +02:00

51 lines
1.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char name[101];
double price;
} Pizza;
int comparePizzas(const void *a, const void *b) {
Pizza *pizzaA = (Pizza *)a;
Pizza *pizzaB = (Pizza *)b;
if (pizzaA->price < pizzaB->price) {
return -1;
} else if (pizzaA->price > pizzaB->price) {
return 1;
} else {
return strcmp(pizzaA->name, pizzaB->name);
}
}
int main() {
Pizza menu[100]; // Максимум 100 пицц
int menuSize = 0;
while (1) {
Pizza pizza;
if (fgets(pizza.name, sizeof(pizza.name), stdin) == NULL) {
break;
}
// Удаляем символ новой строки, если он есть
pizza.name[strcspn(pizza.name, "\n")] = 0;
if (scanf("%lf%*c", &pizza.price) != 1) {
break;
}
menu[menuSize++] = pizza;
}
qsort(menu, menuSize, sizeof(Pizza), comparePizzas);
for (int i = 0; i < menuSize; i++) {
printf("%s\n%.6f\n", menu[i].name, menu[i].price);
}
return 0;
}