usaa24/cv2/program.c
2024-10-07 16:55:46 +00:00

62 lines
1.4 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LENGTH 100
#define MAX_ITEMS 100
typedef struct {
char name[MAX_NAME_LENGTH];
float price;
} Pizza;
int compare(const void *a, const void *b) {
Pizza *pizzaA = (Pizza *)a;
Pizza *pizzaB = (Pizza *)b;
if (pizzaA->price != pizzaB->price) {
return (pizzaA->price > pizzaB->price) - (pizzaA->price < pizzaB->price);
}
return strcasecmp(pizzaA->name, pizzaB->name);
}
int read_pizza_list(Pizza *menu) {
int count = 0;
char input[MAX_NAME_LENGTH];
// Nacitame pizza
while (count < MAX_ITEMS && fgets(input, sizeof(input), stdin)) {
input[strcspn(input, "\n")] = 0; // Odstran znak noveho riadku
// Nacitame cenu pizze
char price_str[10];
if (fgets(price_str, sizeof(price_str), stdin) == NULL) {
break;
}
float price = atof(price_str);
if (price <= 0) {
break;
}
strncpy(menu[count].name, input, MAX_NAME_LENGTH - 1);
menu[count].price = price;
count++;
}
return count;
}
int main() {
Pizza menu[MAX_ITEMS];
int item_count = read_pizza_list(menu);
qsort(menu, item_count, sizeof(Pizza), compare);
for (int i = 0; i < item_count; i++) {
printf("%s\n%.6f\n", menu[i].name, menu[i].price);
}
return 0;
}