201 lines
4.2 KiB
C
201 lines
4.2 KiB
C
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
#include <stdlib.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) {
|
||
|
|
||
|
for(int i=0; i<size; i++) {
|
||
|
|
||
|
char c = fgetc(file);
|
||
|
|
||
|
if (c=='\n')
|
||
|
{
|
||
|
line[i] = '\n';
|
||
|
line[i+1] = '\0';
|
||
|
return i+1;
|
||
|
}
|
||
|
else if (c==EOF)
|
||
|
{
|
||
|
line[i] = '\0';
|
||
|
return EOF;
|
||
|
}
|
||
|
|
||
|
else line[i] = c;
|
||
|
}
|
||
|
|
||
|
line[size-1] = '\0';
|
||
|
|
||
|
return size-1;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 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) {
|
||
|
|
||
|
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);
|
||
|
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]);
|
||
|
}
|
||
|
}
|
||
|
|