Random attempts from days that weren't completed

This commit is contained in:
Peter 2021-12-25 15:53:59 +08:00
parent 181ce8379f
commit a940379e98
4 changed files with 108 additions and 0 deletions

58
14/14-1.c Normal file
View File

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

50
18/18-1.c Normal file
View File

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