This commit is contained in:
Weber 2024-10-04 15:01:12 +00:00
parent b81bf82256
commit a0591fedab

View File

@ -0,0 +1,57 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PIZZAS 100
#define MAX_TITLE_LENGTH 100
#define MAX_PRICE_LENGTH 10
typedef struct {
char title[MAX_TITLE_LENGTH];
float price;
} Pizza;
int compare_pizzas(const void *a, const void *b) {
Pizza *pizza1 = (Pizza *)a;
Pizza *pizza2 = (Pizza *)b;
if (pizza1->price < pizza2->price) {
return -1;
} else if (pizza1->price > pizza2->price) {
return 1;
} else {
return strcmp(pizza1->title, pizza2->title);
}
}
int main() {
Pizza pizzas[MAX_PIZZAS];
int num_pizzas = 0;
while (num_pizzas < MAX_PIZZAS) {
printf("Enter pizza title: ");
fgets(pizzas[num_pizzas].title, MAX_TITLE_LENGTH, stdin);
pizzas[num_pizzas].title[strcspn(pizzas[num_pizzas].title, "\n")] = 0;
printf("Enter pizza price: ");
char price_str[MAX_PRICE_LENGTH];
fgets(price_str, MAX_PRICE_LENGTH, stdin);
price_str[strcspn(price_str, "\n")] = 0;
char *endptr;
pizzas[num_pizzas].price = strtof(price_str, &endptr);
if (endptr == price_str || *endptr != '\0') {
break;
}
num_pizzas++;
}
qsort(pizzas, num_pizzas, sizeof(Pizza), compare_pizzas);
for (int i = 0; i < num_pizzas; i++) {
printf("%s\n%.6f\n", pizzas[i].title, pizzas[i].price);
}
return 0;
}