From 79e61a227c080abf87446a825bf55efc293ce53a Mon Sep 17 00:00:00 2001 From: hasslesstech Date: Sun, 22 Sep 2024 13:21:34 +0300 Subject: [PATCH] initial commit --- Makefile | 6 ++++ src/main.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 Makefile create mode 100644 src/main.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5ef1109 --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +CFLAGS = -I lib/ -O3 -Wall -Werror -Wextra -Wpedantic + +build: src/main.c + mkdir -p bin/ + gcc $(CFLAGS) -o bin/main \ + src/main.c diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..fbbbff8 --- /dev/null +++ b/src/main.c @@ -0,0 +1,88 @@ +#include +#include +#include + +struct BookNode +{ + struct BookNode* next; + char* name; + uint32_t price; // in cents + uint16_t page_amount; + char language[2]; // code: EN, UA, FR, GE, etc. + uint16_t weight; // in grams + int year; +}; + +static struct BookNode* +new_book(char* name, + uint32_t price, + uint16_t page_amount, + char language[2], + uint16_t weight, + int year) +{ + struct BookNode* book = malloc(sizeof(struct BookNode)); + book->name = name; + book->price = price; + book->page_amount = page_amount; + book->language[0] = language[0]; + book->language[1] = language[1]; + book->weight = weight; + book->year = year; + + return book; +} + +static void +print_book(struct BookNode* book) +{ + printf("Book title: %s\n", book->name); + printf("- $%.2lf\n", (double) book->price / 100.0 ); + printf("- %d pages\n", book->page_amount); + printf("- %c%c\n", book->language[0], book->language[1]); + printf("- %d g\n", book->weight); + printf("- Year: %d\n", book->year); +} + +static void +add_book_to_list(struct BookNode* head, + struct BookNode* book) +{ + struct BookNode* current_object = head; + + while (NULL != current_object->next) + { + current_object = current_object->next; + } + + current_object->next = book; +} + +static void +print_list(struct BookNode* head) +{ + struct BookNode* current_object = head; + + while (NULL != current_object) + { + print_book(current_object); + puts(""); + current_object = current_object->next; + } +} + +int +main(void) +{ + struct BookNode* head = new_book("Harry Potter and the Sorcerer's Stone", 962, 360, "EN", 518, 1997); + add_book_to_list(head, new_book("Harry Potter and the Chamber of Secrets", 821, 352, "EN", 310, 1998)); + add_book_to_list(head, new_book("Harry Potter and the Prisoner of Azkaban", 1030, 384, "EN", 370, 1999)); + add_book_to_list(head, new_book("Harry Potter and the Goblet of Fire", 1100, 464, "EN", 434, 2000)); + add_book_to_list(head, new_book("Harry Potter and the Order of the Phoenix", 1044, 816, "EN", 670, 2003)); + add_book_to_list(head, new_book("Harry Potter and the Half-Blood Prince", 973, 576, "EN", 400, 2005)); + add_book_to_list(head, new_book("Harry Potter and the Deathly Hallows", 1253, 640, "EN", 500, 2007)); + add_book_to_list(head, new_book("Harry Potter and the Cursed Child", 1000, 352, "EN", 315, 2016)); + + print_list(head); + return 0; +}