From ab86b9d186b68c4c9a944ecba19bb0bcfee512e5 Mon Sep 17 00:00:00 2001
From: 20041679 <->
Date: Tue, 27 Jun 2023 16:56:10 +0200
Subject: [PATCH] added Esercizi n. 6, missing last point

---
 Esercizi n. 6/Makefile | 14 +++++++++++++
 Esercizi n. 6/book.c   | 35 +++++++++++++++++++++++++++++++
 Esercizi n. 6/book.h   | 15 ++++++++++++++
 Esercizi n. 6/list.c   | 47 ++++++++++++++++++++++++++++++++++++++++++
 Esercizi n. 6/list.h   | 19 +++++++++++++++++
 Esercizi n. 6/main.c   | 19 +++++++++++++++++
 6 files changed, 149 insertions(+)
 create mode 100644 Esercizi n. 6/Makefile
 create mode 100644 Esercizi n. 6/book.c
 create mode 100644 Esercizi n. 6/book.h
 create mode 100644 Esercizi n. 6/list.c
 create mode 100644 Esercizi n. 6/list.h
 create mode 100644 Esercizi n. 6/main.c

diff --git a/Esercizi n. 6/Makefile b/Esercizi n. 6/Makefile
new file mode 100644
index 0000000..71b5a85
--- /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 0000000..ffedd12
--- /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 0000000..7607be1
--- /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 0000000..9369b42
--- /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 0000000..d015257
--- /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 0000000..dd084ed
--- /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) { }
-- 
GitLab