Compare commits
No commits in common. "a0858c36cf087da032c7ce297dcb40d73080b9a8" and "0fc1f02c9f0802a8ca71a334f8a054338f4d6d9e" have entirely different histories.
a0858c36cf
...
0fc1f02c9f
4 changed files with 15 additions and 136 deletions
18
Makefile
18
Makefile
|
@ -1,5 +1,15 @@
|
||||||
|
NAME := aoc
|
||||||
|
FMT := c
|
||||||
|
|
||||||
|
CC := cc
|
||||||
|
CC_OPTIONS := -g -Wall -Wextra
|
||||||
|
DBG := gdb
|
||||||
|
|
||||||
|
SRCD := src
|
||||||
BIND := out
|
BIND := out
|
||||||
BIN := $(BIND)/aoc
|
|
||||||
|
SRCS := $(wildcard $(SRCD)/*.$(FMT))
|
||||||
|
BIN := $(BIND)/$(NAME)
|
||||||
|
|
||||||
all: $(BIND) $(BIN)
|
all: $(BIND) $(BIN)
|
||||||
|
|
||||||
|
@ -7,13 +17,13 @@ run: all
|
||||||
$(BIN)
|
$(BIN)
|
||||||
|
|
||||||
debug: all
|
debug: all
|
||||||
gdb $(BIN)
|
$(DBG) $(BIN)
|
||||||
|
|
||||||
$(BIND):
|
$(BIND):
|
||||||
mkdir $@
|
mkdir $@
|
||||||
|
|
||||||
$(BIN): $(wildcard src/*.c)
|
$(BIN): $(SRCS)
|
||||||
cc -g -Wall -Wextra -Werror -o $@ $^
|
$(CC) $(CC_OPTIONS) -o $@ $^
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(wildcard $(BIND)/*)
|
rm -f $(wildcard $(BIND)/*)
|
||||||
|
|
|
@ -4,33 +4,9 @@
|
||||||
|
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
#include "io.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 été trouvé, retourner 0
|
|
||||||
*/
|
|
||||||
static char first_digit(char *line) {
|
static char first_digit(char *line) {
|
||||||
while (line[0] != '\0' && !isdigit(line[0])) {
|
while (*line != '\0' && !isdigit(*line)) {
|
||||||
line++;
|
line++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
88
src/node.c
88
src/node.c
|
@ -1,88 +0,0 @@
|
||||||
#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
19
src/node.h
|
@ -1,19 +0,0 @@
|
||||||
#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 */
|
|
Reference in a new issue