From e472798297bfe648ba06dd0421a30428d2f84a5e Mon Sep 17 00:00:00 2001 From: Jozef Slaninka Date: Mon, 19 Nov 2018 23:49:56 +0100 Subject: [PATCH] 123456789 --- Makefile | 16 +++++ main.c | 13 ++++ pizza3.c | 200 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ pizza3.h | 27 ++++++++ 4 files changed, 256 insertions(+) create mode 100644 Makefile create mode 100644 main.c create mode 100644 pizza3.c create mode 100644 pizza3.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..41a96da --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +CFLAGS= -std=c99 -g + +all: pizza3 + +%.o: %.c + gcc -c -o $@ $< $(CFLAGS) + +pizza3: main.o pizza3.o + gcc main.o pizza3.o -o pizza3 + +clean: + rm *.o pizza3 +test: pizza3.c tests/test.c + gcc pizza3.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..6bc589d --- /dev/null +++ b/main.c @@ -0,0 +1,13 @@ +#include "pizza3.h" +#include + +int main(){ + + struct pizza list[LIST_SIZE]; + int size = read_pizza_list(stdin,list,LIST_SIZE); + int res = search_pizza_list(list,size,"bryndza"); + printf("Bryndzu obsahuje pizza %s za %f EUR",list[res].name,list[res].prize); + + return 0; +} + diff --git a/pizza3.c b/pizza3.c new file mode 100644 index 0000000..e0ec9ee --- /dev/null +++ b/pizza3.c @@ -0,0 +1,200 @@ +#include +#include +#include +#include "pizza3.h" + +/** + * Nacita jeden riadok zo suboru vratane konca riadka. + * + * @arg amernik na otvoreny subor z ktoreho sa ma nacitavat + * @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(FILE* file,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(file,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 Subor z ktoreho sa ma nacitavat + * @arg pole do ktoreho sa ma naxitat + * @arg size velkost pola + * @return pocet nacitanych poloziek. + * + */ +int read_pizza_list(FILE* file, struct pizza* list, int size){ + + for (int i = 0; i < size; i++) + { + if (read_pizza(file,&list[i])!=1) return i; + } + + return size; +} + +/** + * Vyhlada prvy vyskyt retazca needle v retazci heap. + * + * @arg Retazec v ktorom vyhladavame + * @arg retazec ktory vyhladavame + * @return index prveho vyskytu needle v heap, alebo -1 ak nebol najdeny. + */ +int search_string(const char* heap, const char* needle) { + + char *ret; + + if((ret = strstr(heap, needle))==NULL) return -1; + + return ret - heap; +} + +/** + * Prehlada pole struktoru + * + * @arg zoznam na prehladanie + * @arg velkost zoznamu na prehladanie + * @arg retazec, kotry sa ma vyhladat. + * @return index prvej polozky, kota obsahuje retazec needle. Ak neexistuje, vrati -1. + */ +int search_pizza_list(struct pizza* list,int size, const char* needle) { + + + for (int i = 0; i < size; i++) + { + + struct pizza* item = &list[i]; + + if (search_string(item -> name,needle)!=-1) + { + return i; + } + + + } + + return (-1); +} + +/** + * Vypise jedalny listok na obrazovku. Vypis by mal byt v takom formate, aby sa dal nacitat pomocou finkcie + * 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. + */ + +/** + * 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/pizza3.h b/pizza3.h new file mode 100644 index 0000000..58e59c6 --- /dev/null +++ b/pizza3.h @@ -0,0 +1,27 @@ +#ifndef PIZZA3_H +#define PIZZA3_H + +#define LINE_SIZE 100 +#define LIST_SIZE 100 + +#include + +struct pizza { + float prize; + char name[LINE_SIZE]; +}; + +int read_line(FILE* file,char* line, int size); + +int search_string(const char* heap, const char* needle); + +int search_pizza_list(struct pizza* list,int size, const char* needle); + +int read_pizza(FILE* file,struct pizza* item); +int compare_pizza (const void * a, const void * b); +void sort_pizza(struct pizza* list,int size); +int read_pizza_list(FILE* file,struct pizza* list, int size); +void print_pizza_list(struct pizza* list, int size); + +#endif // PIZZA3_H +