diff --git a/14/14-1.c b/14/14-1.c new file mode 100644 index 0000000..690bcf6 --- /dev/null +++ b/14/14-1.c @@ -0,0 +1,58 @@ +#include +#include +#include +#include "../check_alloc.h" + +// LIST(string) DATASTRUCTURE + +#define INITIAL_SIZE BUFSIZ +typedef char LIST_TYPE; + +typedef struct +{ + LIST_TYPE *list; + size_t size; + size_t capacity; +} LIST; + +LIST *new_list(void) +{ + LIST *p_list = calloc(1, sizeof(LIST)); + CHECK_ALLOC(p_list); + p_list->capacity = INITIAL_SIZE; + p_list->list = calloc(p_list->capacity, sizeof(LIST_TYPE)); + CHECK_ALLOC(p_list->list); + p_list->size = 0; + return p_list; +} + +void add_list(LIST *list, LIST_TYPE elem) +{ + if (list->size >= list->capacity) + { + list->capacity *= 2; + list->list = realloc(list->list, list->capacity * sizeof(LIST_TYPE)); + CHECK_ALLOC(list->list); + } + list->list[list->size] = elem; + list->size++; +} + +// + +LIST *read_template(FILE *p_file) +{ + LIST *p_string = new_list(); + char buf[BUFSIZ]; + fscanf(p_file, "%[^\n]\n\n", buf); + strcpy(p_string->list, buf); + return p_string; +} + +int main(int argc, char const *argv[]) +{ + FILE *p_file = fopen("input", "r"); + LIST *p_string = read_template(p_file); + printf("%s\n", p_string->list); + return 0; +} diff --git a/15/15-1.c b/15/15-1and2.c similarity index 100% rename from 15/15-1.c rename to 15/15-1and2.c diff --git a/17/17-1.c b/17/17-1and2.c similarity index 100% rename from 17/17-1.c rename to 17/17-1and2.c diff --git a/18/18-1.c b/18/18-1.c new file mode 100644 index 0000000..5352a6f --- /dev/null +++ b/18/18-1.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include "../check_alloc.h" + +typedef union +{ + struct _FISH_NUMBER *pair; + int integer; +} ANY_NUMBER; + +typedef struct _FISH_NUMBER +{ + ANY_NUMBER left; + bool left_integer; + ANY_NUMBER right; + bool right_integer; +} FISH_NUMBER; + +FISH_NUMBER *new_number() +{ + FISH_NUMBER *p_number = calloc(1, sizeof(FISH_NUMBER)); + CHECK_ALLOC(p_number); + return p_number; +} + +typedef enum +{ + FIRST, + SECOND +} STATE; + +FISH_NUMBER *str_2_number(char *number) +{ + FISH_NUMBER *p_number = new_number(); + number = strchr(number, '['); + if ('[' == *(number + 1)) + { + str_2_number(number + 1); + } +} + +int main(int argc, char const *argv[]) +{ + char number[] = "[[1,2],3]"; + int a, b; + printf("%d\n", sscanf(number, "%d", &a, &b)); + return 0; +}