2018-11-13 23:05:52 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#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) {
|
2018-11-14 01:09:52 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-11-13 23:05:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Naciata informacie o jednej pizzi z klavesnice.
|
|
|
|
* Prvy riadok je nazov pizze ako retazec.
|
|
|
|
* Druhy riadok je cena pizze akocislo s desatinnou bodkou.
|
|
|
|
*
|
|
|
|
* @arg smernik na otvoreny subor z ktoreho sa ma nacitavat.
|
|
|
|
* @arg item smernik kam sa ma nacitat info o pizzi
|
|
|
|
* @return V pripade, ze pizza nebola nacitana uspesne, funkcia vrati nulu. Inak vrati jednotku.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int read_pizza(FILE* file,struct pizza* item) {
|
2018-11-14 01:09:52 +00:00
|
|
|
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;
|
2018-11-13 23:05:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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){
|
2018-11-14 01:09:52 +00:00
|
|
|
|
|
|
|
int p;
|
|
|
|
for( p = 0; p < size; p++){
|
|
|
|
int pz=read_pizza(file,&list[p]);
|
|
|
|
if (pz == 0) return p+1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
return p+1;
|
2018-11-13 23:05:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2018-11-14 01:09:52 +00:00
|
|
|
char* find = strstr(heap,needle);
|
|
|
|
if (find == NULL){
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else return (find - heap);
|
|
|
|
|
2018-11-13 23:05:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2018-11-14 01:09:52 +00:00
|
|
|
int p;
|
|
|
|
for(p=0;p <size; p++){
|
|
|
|
|
|
|
|
if(search_string(list[p].name,needle)!=-1){
|
|
|
|
return p;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
2018-11-13 23:05:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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){
|
2018-11-14 01:09:52 +00:00
|
|
|
printf("Pizza lístok:\n");
|
|
|
|
int p;
|
|
|
|
|
|
|
|
for( p = 0; p < size; p++){
|
|
|
|
printf("%s %.2f eur\n", list[p].name, list[p].prize);
|
|
|
|
}
|
2018-11-13 23:05:52 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 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) {
|
2018-11-14 01:09:52 +00:00
|
|
|
const struct pizza *firstpz = a;
|
|
|
|
const struct pizza *secondpz = b;
|
|
|
|
|
|
|
|
return (firstpz->prize - secondpz->prize) ;
|
2018-11-13 23:05:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Triedenie pola pizz, podla porovnavacej funkcie compare_pizza
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void sort_pizza(struct pizza* list,int size) {
|
2018-11-14 01:09:52 +00:00
|
|
|
qsort (list, size, sizeof(struct pizza), compare_pizza);
|
2018-11-13 23:05:52 +00:00
|
|
|
}
|
|
|
|
|