From 0ba8e104a991bd1bcde9787b1678c32a5aba5e1c Mon Sep 17 00:00:00 2001 From: Peter Ferko Date: Wed, 14 Nov 2018 02:09:52 +0100 Subject: [PATCH] Odovzdavka --- main.c | 15 +++++++---- pizza3.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++------- pizza3.h | 2 +- 3 files changed, 78 insertions(+), 15 deletions(-) diff --git a/main.c b/main.c index f52c108..669ce91 100644 --- a/main.c +++ b/main.c @@ -3,13 +3,18 @@ int main(int argc, char** argv){ - printf("Ja som nazov programu: $s\n",argv[0]); - printf("Ja som argument 1: $s\n",argv[1]); - printf("Ja som argument 2: $s\n",argv[2]); +struct pizza list[LIST_SIZE]; +FILE * file; + +file=fopen("zoznam.txt","r"); + +int size= read_pizza_list(file, list, LIST_SIZE); +print_pizza_list(list,size); - int res = search_pizza_list(list,size,"slanina"); - printf("Slaninu obsahuje pizza %s za %f EUR",list[res].name,list[res].prize); + int res = search_pizza_list(list,size,"fazula"); + printf("Fazulu obsahuje pizza %s za %f EUR",list[res].name,list[res].prize); + fclose(file); return 0;} diff --git a/pizza3.c b/pizza3.c index 1d071ed..316bfb7 100644 --- a/pizza3.c +++ b/pizza3.c @@ -1,6 +1,5 @@ #include #include - #include "pizza3.h" @@ -14,7 +13,24 @@ */ int read_line(FILE* file,char* line, int size) { - return 0; + int p; + char f; + + for(p = 0; p < size ; p++){ + + f= getc(file); + + if (f == EOF){ + line[p]='\0'; + return EOF;} + if (f =='\n'){ + line[p]='\n'; + line[p+1]='\0'; + return p+1;} + + line[p]=f; + } + } /** @@ -28,7 +44,19 @@ int read_line(FILE* file,char* line, int size) { * */ int read_pizza(FILE* file,struct pizza* item) { - return 0; + int res = read_line(file, item->name, LINE_SIZE); + if (res == EOF || strlen(item->name) == 0){ + return 0; + } + char buffer[LINE_SIZE]; + res = read_line(file, buffer, LINE_SIZE); + res = sscanf(buffer,"%f",&item->prize); + + if (res == EOF){ + return 0; + } + + return 1; } @@ -43,8 +71,16 @@ int read_pizza(FILE* file,struct pizza* item) { * */ int read_pizza_list(FILE* file, struct pizza* list, int size){ - - return 0; + + int p; + for( p = 0; p < size; p++){ + int pz=read_pizza(file,&list[p]); + if (pz == 0) return p+1; + + + + } + return p+1; } /** @@ -55,7 +91,12 @@ int read_pizza_list(FILE* file, struct pizza* list, int size){ * @return index prveho vyskytu needle v heap, alebo -1 ak nebol najdeny. */ int search_string(const char* heap, const char* needle) { - return 0; + char* find = strstr(heap,needle); + if (find == NULL){ + return -1; + } + else return (find - heap); + } /** @@ -67,7 +108,15 @@ int search_string(const char* heap, const char* needle) { * @return index prvej polozky, kota obsahuje retazec needle. Ak neexistuje, vrati -1. */ int search_pizza_list(struct pizza* list,int size, const char* needle) { - return 0; + int p; + for(p=0;p prize - secondpz->prize) ; } /** @@ -94,6 +152,6 @@ int compare_pizza (const void * a, const void * b) { * */ void sort_pizza(struct pizza* list,int size) { - + qsort (list, size, sizeof(struct pizza), compare_pizza); } diff --git a/pizza3.h b/pizza3.h index dc16902..008d12b 100644 --- a/pizza3.h +++ b/pizza3.h @@ -2,7 +2,7 @@ #define PIZZA3_H #define LINE_SIZE 100 -#define LIST_SIZE 100 +#define LIST_SIZE 5 #include