88 lines
1.7 KiB
C
88 lines
1.7 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
|
|
#define LINE_SIZE 100
|
|
#define POCET_JEDAL 100
|
|
|
|
struct pizza {
|
|
float price;
|
|
char name[LINE_SIZE];
|
|
};
|
|
|
|
void delet(char* str) {
|
|
for (int i = 0; str[i] != '\n' && str[i] != 0; i++) {
|
|
str[i] = 0;
|
|
}
|
|
}
|
|
|
|
int read_pizza(struct pizza* item) {
|
|
|
|
char line[LINE_SIZE];
|
|
memset(line, 0, LINE_SIZE);
|
|
|
|
char* r = fgets(line, LINE_SIZE, stdin);
|
|
delet(line);
|
|
|
|
if (r != NULL && line[1] != 0) {
|
|
|
|
char line2[LINE_SIZE];
|
|
|
|
memset(line2, 0, LINE_SIZE);
|
|
fgets(line2, LINE_SIZE, stdin);
|
|
|
|
float value = strtof(line2, NULL);
|
|
|
|
if (value == 0.0F) {
|
|
return 0;
|
|
}
|
|
|
|
item->price = value;
|
|
strcpy(item->name, line);
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int compare_pizza(const void* a, const void* b) {
|
|
const struct pizza* pizza_a = (const struct pizza*)a;
|
|
const struct pizza* pizza_b = (const struct pizza*)b;
|
|
|
|
if (pizza_a->price < pizza_b->price) {
|
|
return -1;
|
|
} else if (pizza_a->price > pizza_b->price) {
|
|
return 1;
|
|
} else {
|
|
return strcmp(pizza_a->name, pizza_b->name);
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
struct pizza menu[POCET_JEDAL];
|
|
memset(menu, 0, sizeof(struct pizza) * POCET_JEDAL);
|
|
int counter = 0;
|
|
|
|
struct pizza item;
|
|
|
|
while (read_pizza(&item)) {
|
|
strcpy(menu[counter].name, item.name);
|
|
menu[counter].price = item.price;
|
|
counter += 1;
|
|
|
|
if (counter >= POCET_JEDAL) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
qsort(menu, counter, sizeof(struct pizza), compare_pizza);
|
|
|
|
for (int c = 0; c < counter; c++) {
|
|
printf("%s\n", menu[c].name);
|
|
printf("%.6f\n", menu[c].price);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|