diff --git a/Esercizi n. 6/Makefile b/Esercizi n. 6/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..71b5a857be56ce68cc572249dbb005224c47b673 --- /dev/null +++ b/Esercizi n. 6/Makefile @@ -0,0 +1,14 @@ +CC = clang +CFLAGS = -Wall -Wextra -g +objects = list.o book.o main.o + +all: $(objects) + $(CC) $(CFLAGS) $(objects) -o main +list.o: list.c list.h + $(CC) -c $(CFLAGS) $< -o $@ +book.o: book.c book.h + $(CC) -c $(CFLAGS) $< -o $@ +main.o: main.c + $(CC) -c $(CFLAGS) $< -o $@ +clean: + rm main $(objects) diff --git a/Esercizi n. 6/book.c b/Esercizi n. 6/book.c new file mode 100644 index 0000000000000000000000000000000000000000..ffedd12861e7c32aeb02a0e8b2577104a8e0963b --- /dev/null +++ b/Esercizi n. 6/book.c @@ -0,0 +1,35 @@ +#include "book.h" +#include <stdio.h> +#include <string.h> + +int book_input(Book *book) { + puts("titolo"); + scanf("%63s", book->title); + puts("autore"); + scanf("%63s", book->author); + puts("price"); + scanf("%f", &book->price); + scanf("%u", &book->copies); + return 1; +} + +void book_output(Book *book) { + printf( + "titolo: %s, autore: %s, prezzo: %f, disponibilità : %u\n", + book->title, + book->author, + book->price, + book->copies + ); +} + +int book_has_copies(Book *book, unsigned int n) { + return book->copies >= n; +} + +int book_cmp(Book *book1, Book *book2) { + return + strcmp(book1->title, book2->title) == 0 && + strcmp(book1->author, book2->author) == 0 && + book1->price == book2->price; +} diff --git a/Esercizi n. 6/book.h b/Esercizi n. 6/book.h new file mode 100644 index 0000000000000000000000000000000000000000..7607be1a0a51f131e8427c2a62b83851b7925a6b --- /dev/null +++ b/Esercizi n. 6/book.h @@ -0,0 +1,15 @@ +#ifndef BOOK_H +#define BOOK_H + +typedef struct { + char title[64]; + char author[64]; + float price; + unsigned int copies; +} Book; + +int book_input(Book *book); +void book_output(Book *book); +int book_has_copies(Book *book, unsigned int n); + +#endif diff --git a/Esercizi n. 6/list.c b/Esercizi n. 6/list.c new file mode 100644 index 0000000000000000000000000000000000000000..9369b42e52bf85e9181c4aadb6640e78e32f53b4 --- /dev/null +++ b/Esercizi n. 6/list.c @@ -0,0 +1,47 @@ +#include "list.h" +#include <stdlib.h> + +List node_alloc() { + return malloc(sizeof(Head)); +} + +void list_input_insert_tail(List *list, int (*input) (Data *)) { + if (!(*list)) { + List next = *list; + (*list) = node_alloc(); + Data data; + input(&data); + (*list)->data = data; + (*list)->next = next; + } else { + list_input_insert_tail(&(*list)->next, input); + } +} + +void list_filter_output( + List list, + int (*predicate) (Data *, void *), + void *key, + void (*output) (Data *)) +{ + if (list) { + if (predicate(&list->data, key)) { + output(&list->data); + } + list_filter_output(list->next, predicate, key, output); + } +} + +void list_rev_filter_output( + List list, + int (*predicate) (Data *, void *), + void *key, + void (*output) (Data *)) +{ + if (list) { + list_rev_filter_output(list->next, predicate, key, output); + if (predicate(&list->data, key)) { + output(&list->data); + } + } +} diff --git a/Esercizi n. 6/list.h b/Esercizi n. 6/list.h new file mode 100644 index 0000000000000000000000000000000000000000..d01525780893b84c7f5fd52ee109439feb9d17d3 --- /dev/null +++ b/Esercizi n. 6/list.h @@ -0,0 +1,19 @@ +#ifndef LIST_H +#define LIST_H +#include "book.h" + +typedef Book Data; + +typedef struct Head { + struct Head *next; + Data data; +} Head; + +typedef Head *List; + +List node_alloc(); +void list_input_insert_tail(List *list, int (*input) (Data *)); +void list_filter_output(List list, int (*predicate) (Data *, void *), void *key, void (*output) (Data *)); +void list_rev_filter_output(List list, int (*predicate) (Data *, void *), void *key, void (*output) (Data *)); + +#endif diff --git a/Esercizi n. 6/main.c b/Esercizi n. 6/main.c new file mode 100644 index 0000000000000000000000000000000000000000..dd084edc243f5bbc9f6e524970fd74c17208b5e3 --- /dev/null +++ b/Esercizi n. 6/main.c @@ -0,0 +1,19 @@ +#include "list.h" + +void f1(List *list) { + return list_input_insert_tail(list, book_input); +} + +int has_copies(Book *book, void *n) { + return book_has_copies(book, *(int *)n); +} + +void f2(List list, unsigned int x) { + list_filter_output(list, has_copies, &x, book_output); +} + +void f3(List list, unsigned int x) { + list_rev_filter_output(list, has_copies, &x, book_output); +} + +int main(void) { }