162 lines
3.2 KiB
C
162 lines
3.2 KiB
C
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
#include <stdlib.h>
|
||
|
|
||
|
#include "pizza2.h"
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Nacita jeden riadok z klavesnice vratane konca riadka.
|
||
|
*
|
||
|
* @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(char* line, int size) {
|
||
|
|
||
|
for(int i=0; i<size; i++) {
|
||
|
|
||
|
char c = getchar();
|
||
|
|
||
|
if (c=='\n')
|
||
|
{
|
||
|
line[i-1] = '\n';
|
||
|
line[i] = '\0';
|
||
|
return i;
|
||
|
}
|
||
|
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. Ak je prevy riadok prazdny, zaznam o pizzi je neplatny.
|
||
|
* Druhy riadok je cena pizze akocislo s desatinnou bodkou.
|
||
|
*
|
||
|
* Pozor - spravanie funkcie nie je definovane v pripade, ze pouzivatel zada neocakavany vstyup, napr.
|
||
|
* pismena namiesto cisla.
|
||
|
*
|
||
|
* @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(struct pizza* item){
|
||
|
int res = read_line(item->name,LINE_SIZE);
|
||
|
if (res == EOF || strlen(item->name) == 0){
|
||
|
return 0;
|
||
|
}
|
||
|
char buffer[LINE_SIZE];
|
||
|
res = read_line(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 pole do ktoreho sa ma naxitat
|
||
|
* @arg size velkost pola
|
||
|
* @return pocet nacitanych poloziek.
|
||
|
*
|
||
|
*/
|
||
|
int read_pizza_list(struct pizza* list, int size){
|
||
|
|
||
|
for (int i = 0; i < size; i++)
|
||
|
{
|
||
|
|
||
|
if (read_pizza(&list[i])!=1)
|
||
|
{
|
||
|
return i;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return size;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Vypise jedalny listok na obrazovku. Vypis by mal byt v takom formate, aby sa dal nacitat pomocou funkcie
|
||
|
* 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.
|
||
|
*/
|
||
|
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]);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
|