From 85415c6f9eeddde8035d768ce2bf435129204df6 Mon Sep 17 00:00:00 2001 From: Jozef Slaninka Date: Mon, 19 Nov 2018 23:47:16 +0100 Subject: [PATCH] 123456789 --- Makefile | 15 ++++++ main.c | 15 ++++++ pizza2.c | 161 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ pizza2.h | 20 +++++++ 4 files changed, 211 insertions(+) create mode 100644 Makefile create mode 100644 main.c create mode 100644 pizza2.c create mode 100644 pizza2.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b577cc6 --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +CFLAGS= -std=c99 -g + +all: pizza2 + +%.o: %.c + gcc -c -o $@ $< $(CFLAGS) + +pizza2: main.o pizza2.o + gcc main.o pizza2.o -o pizza2 +clean: + rm *.o pizza2 test +test: pizza2.c tests/test.c + gcc pizza2.c tests/test.c tests/unity.c -Itests -o test + ./test + diff --git a/main.c b/main.c new file mode 100644 index 0000000..25a5600 --- /dev/null +++ b/main.c @@ -0,0 +1,15 @@ +#include "pizza2.h" +#include + +int main(){ + struct pizza list[LIST_SIZE]; + + + int list_size = read_pizza_list(list,LIST_SIZE); + + + sort_pizza(list,list_size); + print_pizza_list(list,list_size); + return 0; +} + diff --git a/pizza2.c b/pizza2.c new file mode 100644 index 0000000..9e4eb84 --- /dev/null +++ b/pizza2.c @@ -0,0 +1,161 @@ +#include +#include +#include + +#include "pizza2.h" + + +/** + * Nacita jeden riadok z klavesnice vratane konca riadka. + * + * @arg line Pole kam sa ma ulozit vysledok + * @arg size velkost pola s vysleddkom. + * @return Funkcia vrati EOF ak nastal koniec vstupu vstupu alebo pocet nacitanych znakov. + */ +int read_line(char* line, int size) { + + for(int i=0; iname,LINE_SIZE); + if (res == EOF || strlen(item->name) == 0){ + return 0; + } + char buffer[LINE_SIZE]; + res = read_line(buffer,LINE_SIZE); + if (res == EOF){ + return 0; + } + res = sscanf(buffer,"%f",&item->prize); + if (res != 1){ + return 0; + } + return 1; +} + +/** + * Nacita jedalny listok zo suboru. Zaznamy o jedlach su v textovom formate ulozene za sebou. + * Zoznam je ukonceny neplatnou pizzou }napr. prazdny riadok.| + * + * @arg pole do ktoreho sa ma naxitat + * @arg size velkost pola + * @return pocet nacitanych poloziek. + * + */ +int read_pizza_list(struct pizza* list, int size){ + + for (int i = 0; i < size; i++) + { + + if (read_pizza(&list[i])!=1) + { + return i; + } + } + + return size; +} + +/** + * Vypise jedalny listok na obrazovku. Vypis by mal byt v takom formate, aby sa dal nacitat pomocou funkcie + * read_pizza_list(). + * + */ +void print_pizza_list(struct pizza* list, int size){ + + + for (int i = 0; i < size; i++) + { + + struct pizza item = list[i]; + printf("%s\n",item.name ); + printf("%f\n",item.prize); + + } + +} + +/** + * Funkcia na porovnanie dvoch ceny dvoch pizz. + * + * @arg smernik na prvu pizzu + * @arg smernik na druhu pizzu + * @return nula ak su pizze rovnake. Kladnu hodnotu aj je prva pizza drahsia. Zapornu inak. + */ +int compare_pizza (const void * a, const void * b) { + + + struct pizza* first = (struct pizza*) a; + struct pizza* second = (struct pizza*) b; + + return (int) (first -> prize - second->prize); +} + +/** + * Triedenie pola pizz, podla porovnavacej funkcie compare_pizza + * + */ + +void swap(struct pizza* xp, struct pizza* yp) +{ + struct pizza temp = *xp; + *xp = *yp; + *yp = temp; +} + +void sort_pizza(struct pizza* list,int size) { + + +//qsort (list, size, sizeof(struct pizza), compare_pizza); + + + int i, j; + for (i = 0; i < size-1; i++) + + // Last i elements are already in place + for (j = 0; j < size-i-1; j++){ + + void* a = (void*) &list[j]; + void* b = (void*) &list[j+1]; + + if (compare_pizza(a,b) > 0) + swap(&list[j], &list[j+1]); + } + +} + + diff --git a/pizza2.h b/pizza2.h new file mode 100644 index 0000000..dbb8166 --- /dev/null +++ b/pizza2.h @@ -0,0 +1,20 @@ +#ifndef PIZZA2_H +#define PIZZA2_H + +#define LINE_SIZE 100 +#define LIST_SIZE 100 + +struct pizza { + float prize; + char name[LINE_SIZE]; +}; + +int read_line(char* line, int size); +int read_pizza(struct pizza* item); +int compare_pizza (const void * a, const void * b); +void sort_pizza(struct pizza* list,int size); +int read_pizza_list(struct pizza* list, int size); +void print_pizza_list(struct pizza* list, int size); + +#endif // PIZZA2_H +