Odovzdavka

This commit is contained in:
Peter Ferko 2018-11-14 02:09:52 +01:00
parent 188ccdb9be
commit 0ba8e104a9
3 changed files with 78 additions and 15 deletions

15
main.c
View File

@ -3,13 +3,18 @@
int main(int argc, char** argv){ int main(int argc, char** argv){
printf("Ja som nazov programu: $s\n",argv[0]); struct pizza list[LIST_SIZE];
printf("Ja som argument 1: $s\n",argv[1]); FILE * file;
printf("Ja som argument 2: $s\n",argv[2]);
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;} return 0;}

View File

@ -1,6 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "pizza3.h" #include "pizza3.h"
@ -14,7 +13,24 @@
*/ */
int read_line(FILE* file,char* line, int size) { 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) { 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;
} }
@ -44,7 +72,15 @@ int read_pizza(FILE* file,struct pizza* item) {
*/ */
int read_pizza_list(FILE* file, struct pizza* list, int size){ 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. * @return index prveho vyskytu needle v heap, alebo -1 ak nebol najdeny.
*/ */
int search_string(const char* heap, const char* needle) { 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. * @return index prvej polozky, kota obsahuje retazec needle. Ak neexistuje, vrati -1.
*/ */
int search_pizza_list(struct pizza* list,int size, const char* needle) { int search_pizza_list(struct pizza* list,int size, const char* needle) {
return 0; int p;
for(p=0;p <size; p++){
if(search_string(list[p].name,needle)!=-1){
return p;
}
}
return -1;
} }
/** /**
@ -76,6 +125,12 @@ int search_pizza_list(struct pizza* list,int size, const char* needle) {
* *
*/ */
void print_pizza_list(struct pizza* list, int size){ void print_pizza_list(struct pizza* list, int size){
printf("Pizza lístok:\n");
int p;
for( p = 0; p < size; p++){
printf("%s %.2f eur\n", list[p].name, list[p].prize);
}
} }
/** /**
@ -86,7 +141,10 @@ void print_pizza_list(struct pizza* list, int size){
* @return nula ak su pizze rovnake. Kladnu hodnotu aj je prva pizza drahsia. Zapornu inak. * @return nula ak su pizze rovnake. Kladnu hodnotu aj je prva pizza drahsia. Zapornu inak.
*/ */
int compare_pizza (const void * a, const void * b) { int compare_pizza (const void * a, const void * b) {
return 0; const struct pizza *firstpz = a;
const struct pizza *secondpz = b;
return (firstpz->prize - secondpz->prize) ;
} }
/** /**
@ -94,6 +152,6 @@ int compare_pizza (const void * a, const void * b) {
* *
*/ */
void sort_pizza(struct pizza* list,int size) { void sort_pizza(struct pizza* list,int size) {
qsort (list, size, sizeof(struct pizza), compare_pizza);
} }

View File

@ -2,7 +2,7 @@
#define PIZZA3_H #define PIZZA3_H
#define LINE_SIZE 100 #define LINE_SIZE 100
#define LIST_SIZE 100 #define LIST_SIZE 5
#include <stdio.h> #include <stdio.h>