initial commit

This commit is contained in:
ІО-23 Шмуляр Олег 2024-09-22 13:21:34 +03:00
commit 79e61a227c
2 changed files with 94 additions and 0 deletions

6
Makefile Normal file
View File

@ -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

88
src/main.c Normal file
View File

@ -0,0 +1,88 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
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;
}