usaa24/cv2/program.c

62 lines
1.4 KiB
C
Raw Normal View History

2024-10-01 13:22:17 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2024-10-07 16:55:46 +00:00
#define MAX_NAME_LENGTH 100
#define MAX_ITEMS 100
2024-10-01 13:22:17 +00:00
2024-10-07 16:55:46 +00:00
typedef struct {
char name[MAX_NAME_LENGTH];
2024-10-01 13:22:17 +00:00
float price;
2024-10-07 16:55:46 +00:00
} Pizza;
2024-10-01 13:22:17 +00:00
2024-10-07 16:55:46 +00:00
int compare(const void *a, const void *b) {
Pizza *pizzaA = (Pizza *)a;
Pizza *pizzaB = (Pizza *)b;
2024-10-01 13:22:17 +00:00
2024-10-07 16:55:46 +00:00
if (pizzaA->price != pizzaB->price) {
return (pizzaA->price > pizzaB->price) - (pizzaA->price < pizzaB->price);
}
return strcasecmp(pizzaA->name, pizzaB->name);
2024-10-01 13:22:17 +00:00
}
2024-10-07 16:55:46 +00:00
int read_pizza_list(Pizza *menu) {
int count = 0;
char input[MAX_NAME_LENGTH];
2024-10-01 13:22:17 +00:00
2024-10-07 16:55:46 +00:00
// 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;
2024-10-01 13:22:17 +00:00
}
2024-10-07 16:55:46 +00:00
float price = atof(price_str);
if (price <= 0) {
2024-10-01 13:22:17 +00:00
break;
}
2024-10-07 16:55:46 +00:00
strncpy(menu[count].name, input, MAX_NAME_LENGTH - 1);
menu[count].price = price;
count++;
2024-10-01 13:22:17 +00:00
}
2024-10-07 16:55:46 +00:00
return count;
2024-10-01 13:22:17 +00:00
}
2024-10-07 16:55:46 +00:00
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;
}