From 6737c3a7818339d15cf91e88fe311af7b892003e Mon Sep 17 00:00:00 2001 From: Evan Burkey Date: Fri, 10 Dec 2021 14:33:37 -0800 Subject: [PATCH] add basic math functions --- CMakeLists.txt | 1 + include/input.h | 2 +- include/math.h | 8 ++++++++ src/input.c | 2 +- src/math.c | 28 ++++++++++++++++++++++++++++ 5 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 include/math.h create mode 100644 src/math.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 14eb77a..5b6db61 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,7 @@ set(SOURCES src/stack.c src/binarytree.c src/input.c + src/math.c ) add_library(flint ${SOURCES}) diff --git a/include/input.h b/include/input.h index 08fed0e..a6d67f6 100644 --- a/include/input.h +++ b/include/input.h @@ -4,7 +4,7 @@ #include char *get_input(const char *); -char **split(const char *, size_t *, const char *); +char **split(char *, size_t *, const char *); char **get_lines(const char *, size_t *); int *get_ints(const char *, size_t *); void del_split(char **); diff --git a/include/math.h b/include/math.h new file mode 100644 index 0000000..56bf2d4 --- /dev/null +++ b/include/math.h @@ -0,0 +1,8 @@ +#ifndef LIBFLINT_H_MATH +#define LIBFLINT_H_MATH + +int max_int(int a, int b); +int min_int(int a, int b); +int binstr_to_int(const char *s); + +#endif // LIBFLINT_H_MATH diff --git a/src/input.c b/src/input.c index ad439fe..f892896 100644 --- a/src/input.c +++ b/src/input.c @@ -36,7 +36,7 @@ char *get_input(const char *path) { return buf; } -char **split(const char *s, size_t *lsz, const char *delim) { +char **split(char *s, size_t *lsz, const char *delim) { char **lines = NULL; char *t = strtok(s, delim); size_t n = 0; diff --git a/src/math.c b/src/math.c new file mode 100644 index 0000000..13a4647 --- /dev/null +++ b/src/math.c @@ -0,0 +1,28 @@ +#include + +#include "math.h" + +int max_int(int a, int b) { + if (a > b) { + return a; + } + return b; +} + +int min_int(int a, int b) { + if (a < b) { + return a; + } + return b; +} + +int binstr_to_int(const char *s) { + int n = 0, m = 1; + for (size_t i = strlen(s) - 1; i >= 0; --i) { + if (s[i] == '1') { + n += m; + } + m *= 2; + } + return n; +}