add basic math functions

This commit is contained in:
Evan Burkey 2021-12-10 14:33:37 -08:00
parent 12363f0ed6
commit 6737c3a781
5 changed files with 39 additions and 2 deletions

View File

@ -11,6 +11,7 @@ set(SOURCES
src/stack.c
src/binarytree.c
src/input.c
src/math.c
)
add_library(flint ${SOURCES})

View File

@ -4,7 +4,7 @@
#include <stdlib.h>
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 **);

8
include/math.h Normal file
View File

@ -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

View File

@ -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;

28
src/math.c Normal file
View File

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