Compare commits

..

7 commits

4 changed files with 136 additions and 15 deletions

View file

@ -1,15 +1,5 @@
NAME := aoc
FMT := c
CC := cc
CC_OPTIONS := -g -Wall -Wextra
DBG := gdb
SRCD := src
BIND := out
SRCS := $(wildcard $(SRCD)/*.$(FMT))
BIN := $(BIND)/$(NAME)
BIN := $(BIND)/aoc
all: $(BIND) $(BIN)
@ -17,13 +7,13 @@ run: all
$(BIN)
debug: all
$(DBG) $(BIN)
gdb $(BIN)
$(BIND):
mkdir $@
$(BIN): $(SRCS)
$(CC) $(CC_OPTIONS) -o $@ $^
$(BIN): $(wildcard src/*.c)
cc -g -Wall -Wextra -Werror -o $@ $^
clean:
rm -f $(wildcard $(BIND)/*)

View file

@ -4,9 +4,33 @@
#include "string.h"
#include "io.h"
#include "node.h"
/*
* Analyser le contenu d'une chaîne de caractères :
* - Si le premier caractère est un chiffre, le retourner
* - Sinon, déterminer s'il s'agit d'un chiffre écrit en toutes lettres,
* et dans ce cas le retourner sous forme de chiffre. Sinon, retourner le
* caractère nul.
* - La chaîne de caractère contient forcément des caractères non-nuls.
*/
/*
static char parse_digit(char *line, Node *digits_tree) {
if (isdigit(line[0])) {
return line[0];
} else {
return Node_stringToDigit(digits_tree, line);
}
}
*/
/**
* Lire tous les caractères d'une ligne :
* - Au premier chiffre, qu'il soit tel quel ou écrit en toutes lettres, le retourner
* - Si aucun chiffre n'a é trouvé, retourner 0
*/
static char first_digit(char *line) {
while (*line != '\0' && !isdigit(*line)) {
while (line[0] != '\0' && !isdigit(line[0])) {
line++;
}

88
src/node.c Normal file
View file

@ -0,0 +1,88 @@
#include <stdio.h>
#include <stdlib.h>
#include "node.h"
/**
* Create a brand new node, initialized
*/
Node *Node_new(char letter) {
Node *node = malloc(sizeof(Node));
node->letter = letter;
node->next_sibling = NULL;
node->first_child = NULL;
return node;
}
/**
* Free all nodes from a root node
*/
void Node_destroyTree(Node *root_node) {
if (root_node->first_child != NULL) {
Node_destroyTree(root_node->first_child);
}
if (root_node->next_sibling != NULL) {
Node_destroyTree(root_node->next_sibling);
}
free(root_node);
}
/**
* With a root node, build the n-ary tree from a string
*/
void Node_addString(Node *root_node, char *letters) {
if (letters[0] == '\0') {
return;
}
while (root_node->letter != letters[0] && root_node->next_sibling != NULL) {
root_node = root_node->next_sibling;
}
if (root_node->letter == letters[0]) {
letters++;
if (letters[0] != '\0' && root_node->first_child == NULL) {
root_node->first_child = Node_new(letters[0]);
}
Node_addString(root_node->first_child, letters);
} else if (root_node->next_sibling == NULL) {
root_node->next_sibling = Node_new(letters[0]);
Node_addString(root_node->next_sibling, letters);
}
}
static void print_tabs(unsigned int depth) {
for (unsigned int i = 0; i < depth; i++) {
printf("| ");
}
}
/**
* Recursively print the content of the tree
*/
static void print_node(Node *node, unsigned int depth) {
print_tabs(depth);
printf("%c", node->letter);
if (node->first_child != NULL) {
putchar('\n');
print_node(node->first_child, depth+1);
}
if (node->next_sibling != NULL) {
putchar('\n');
print_node(node->next_sibling, depth);
}
}
/**
* Initialize the printing of a tree
*/
void Node_printTree(Node *root_node) {
print_node(root_node, 0);
putchar('\n');
}

19
src/node.h Normal file
View file

@ -0,0 +1,19 @@
#ifndef NODE_H
#define NODE_H
/**
* Define the members of my n-ary tree
*/
typedef struct Node {
char letter;
struct Node *next_sibling;
struct Node *first_child;
} Node;
Node *Node_new(char letter);
void Node_destroyTree(Node *root_node);
void Node_addString(Node *root_node, char *letters);
void Node_printTree(Node *root_node);
char Node_stringToDigit(Node *root_node, char *string);
#endif /* NODE_H */