91 lines
1.6 KiB
C
91 lines
1.6 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define LINESIZE 200
|
|
#define MAXITEMS 100
|
|
|
|
// struktura pre polozku jed. listka
|
|
struct pizza {
|
|
char name[LINESIZE];
|
|
float price;
|
|
};
|
|
|
|
//funkcia na nacitanie jednej polozky
|
|
int read_pizza(struct pizza *item) {
|
|
char line1[LINESIZE];
|
|
char line2[LINESIZE];
|
|
|
|
//nazov
|
|
if (fgets(line1, sizeof(line1), stdin) == NULL) {
|
|
return 0;
|
|
}
|
|
line1[strcspn(line1, "\n")] = '\0';
|
|
if (line1[0] == '\0') {
|
|
return 0;
|
|
}
|
|
|
|
//cena
|
|
if (fgets(line2, sizeof(line2), stdin) == NULL) {
|
|
return 0;
|
|
}
|
|
line2[strcspn(line2, "\n")] = '\0';
|
|
|
|
float value;
|
|
if (sscanf(line2, "%f", &value) != 1) {
|
|
return 0;
|
|
}
|
|
|
|
strcpy(item->name, line1);
|
|
item->price = value;
|
|
return 1;
|
|
}
|
|
|
|
// porovnanie dvoch položiek
|
|
int greater(struct pizza *a, struct pizza *b) {
|
|
if (a->price > b->price)
|
|
return 1;
|
|
if (a->price < b->price)
|
|
return 0;
|
|
return strcmp(a->name, b->name) > 0;
|
|
}
|
|
|
|
// sort
|
|
void sort_pizzas(struct pizza *list, int count) {
|
|
for (int i = 0; i < count - 1; i++) {
|
|
for (int j = 0; j < count - i - 1; j++) {
|
|
if (greater(&list[j], &list[j + 1])) {
|
|
struct pizza tmp = list[j];
|
|
list[j] = list[j + 1];
|
|
list[j + 1] = tmp;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
struct pizza list[MAXITEMS];
|
|
int count = 0;
|
|
|
|
//nacitanie listka
|
|
while (count < MAXITEMS && read_pizza(&list[count])){
|
|
count++;
|
|
}
|
|
|
|
//ukoncit ak ziadne polozky
|
|
if (count == 0) {
|
|
return 0;
|
|
}
|
|
|
|
//triedenie
|
|
sort_pizzas(list, count);
|
|
|
|
//vypis listka
|
|
for (int i = 0; i < count; i++){
|
|
printf("%s\n", list[i].name);
|
|
printf("%s\n", list[i].price);
|
|
}
|
|
|
|
return 0;
|
|
}
|