123456789

This commit is contained in:
Jozef Slaninka 2018-11-19 23:49:56 +01:00
commit e472798297
4 changed files with 256 additions and 0 deletions

16
Makefile Normal file
View File

@ -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

13
main.c Normal file
View File

@ -0,0 +1,13 @@
#include "pizza3.h"
#include <stdio.h>
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;
}

200
pizza3.c Normal file
View File

@ -0,0 +1,200 @@
#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]);
}
}

27
pizza3.h Normal file
View File

@ -0,0 +1,27 @@
#ifndef PIZZA3_H
#define PIZZA3_H
#define LINE_SIZE 100
#define LIST_SIZE 100
#include <stdio.h>
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