72 lines
1.5 KiB
C
72 lines
1.5 KiB
C
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
#define LINE_SIZE 100
|
||
|
#define LIST_SIZE 100
|
||
|
|
||
|
struct pizza {
|
||
|
char name[LINE_SIZE];
|
||
|
float price;
|
||
|
};
|
||
|
|
||
|
int read_item(struct pizza *item) {
|
||
|
|
||
|
if (fgets(item->name, LINE_SIZE, stdin) == NULL || item->name[0] == '\n') {
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
item->name[strcspn(item->name, "\n")] = 0;
|
||
|
|
||
|
|
||
|
if (scanf("%f", &item->price) != 1 || item->price <= 0) {
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
while (getchar() != '\n');
|
||
|
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
int compare_pizza(const void *a, const void *b) {
|
||
|
struct pizza *pizza_a = (struct pizza *)a;
|
||
|
struct pizza *pizza_b = (struct pizza *)b;
|
||
|
|
||
|
if (pizza_a->price < pizza_b->price) return -1;
|
||
|
if (pizza_a->price > pizza_b->price) return 1;
|
||
|
return strcmp(pizza_a->name, pizza_b->name);
|
||
|
}
|
||
|
|
||
|
int read_pizza_list(struct pizza *list) {
|
||
|
int count = 0;
|
||
|
while (count < LIST_SIZE) {
|
||
|
struct pizza item;
|
||
|
memset(&item, 0, sizeof(struct pizza));
|
||
|
if (read_item(&item)) {
|
||
|
list[count++] = item;
|
||
|
} else {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
return count;
|
||
|
}
|
||
|
|
||
|
void print_pizza_list(struct pizza *list, int count) {
|
||
|
for (int i = 0; i < count; i++) {
|
||
|
printf("%s\n%.6f\n", list[i].name, list[i].price);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int main() {
|
||
|
struct pizza list[LIST_SIZE];
|
||
|
memset(list, 0, sizeof(list));
|
||
|
|
||
|
int count = read_pizza_list(list);
|
||
|
qsort(list, count, sizeof(struct pizza), compare_pizza);
|
||
|
|
||
|
print_pizza_list(list, count);
|
||
|
|
||
|
return 0;
|
||
|
}
|