Fix binstr_to_int loop bug. Add lf prefix

This commit is contained in:
Evan Burkey 2021-12-21 06:48:29 -08:00
parent 6737c3a781
commit acf0164f4f
14 changed files with 33 additions and 20 deletions

View File

@ -6,12 +6,12 @@ set(CMAKE_C_STANDARD 99)
include_directories(include) include_directories(include)
set(SOURCES set(SOURCES
src/linkedlist.c src/lflinkedlist.c
src/set.c src/lfset.c
src/stack.c src/lfstack.c
src/binarytree.c src/lfbinarytree.c
src/input.c src/lfinput.c
src/math.c src/lfmath.c
) )
add_library(flint ${SOURCES}) add_library(flint ${SOURCES})

View File

@ -1,7 +1,7 @@
#ifndef LIBFLINT_SET_H #ifndef LIBFLINT_SET_H
#define LIBFLINT_SET_H #define LIBFLINT_SET_H
#include "linkedlist.h" #include "lflinkedlist.h"
#define Set List #define Set List

View File

@ -1,7 +1,7 @@
#ifndef LIBFLINT_STACK_H #ifndef LIBFLINT_STACK_H
#define LIBFLINT_STACK_H #define LIBFLINT_STACK_H
#include "linkedlist.h" #include "lflinkedlist.h"
#define Stack List #define Stack List

View File

@ -2,7 +2,7 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include "binarytree.h" #include "lfbinarytree.h"
void bintree_init(BinTree *tree, void (*destroy)(void *data)) { void bintree_init(BinTree *tree, void (*destroy)(void *data)) {
tree->size = 0; tree->size = 0;

View File

@ -7,7 +7,7 @@
#include <bsd/stdlib.h> #include <bsd/stdlib.h>
#endif #endif
#include "input.h" #include "lfinput.h"
char *get_input(const char *path) { char *get_input(const char *path) {
FILE *fp = NULL; FILE *fp = NULL;
@ -67,7 +67,7 @@ int *get_ints(const char *path, size_t *sz) {
for (size_t idx = 0; idx < *sz; idx++) { for (size_t idx = 0; idx < *sz; idx++) {
int n; int n;
const char *errstr; const char *errstr;
n = strtonum(lines[idx], INT_MIN, INT_MAX, &errstr); n = (int)strtonum(lines[idx], INT_MIN, INT_MAX, &errstr);
if (errstr) { if (errstr) {
printf("Failed to convert %s to int. Returning NULL\n", lines[idx]); printf("Failed to convert %s to int. Returning NULL\n", lines[idx]);
exit(1); exit(1);

View File

@ -1,7 +1,7 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include "linkedlist.h" #include "lflinkedlist.h"
void ll_init(List* list, void (*destroy)(void *data)) { void ll_init(List* list, void (*destroy)(void *data)) {
list->size = 0; list->size = 0;

View File

@ -1,6 +1,6 @@
#include <string.h> #include <string.h>
#include "math.h" #include "lfmath.h"
int max_int(int a, int b) { int max_int(int a, int b) {
if (a > b) { if (a > b) {
@ -18,7 +18,7 @@ int min_int(int a, int b) {
int binstr_to_int(const char *s) { int binstr_to_int(const char *s) {
int n = 0, m = 1; int n = 0, m = 1;
for (size_t i = strlen(s) - 1; i >= 0; --i) { for (int i = (int)strlen(s) - 1; i >= 0; --i) {
if (s[i] == '1') { if (s[i] == '1') {
n += m; n += m;
} }

View File

@ -1,4 +1,4 @@
#include "set.h" #include "lfset.h"
void set_init(Set* set, int (*match)(const void* a, const void* b), void set_init(Set* set, int (*match)(const void* a, const void* b),
void (*destroy)(void* data)) { void (*destroy)(void* data)) {

View File

@ -1,4 +1,4 @@
#include "stack.h" #include "lfstack.h"
void stack_init(Stack* stack, void (*destroy)(void* data)) { void stack_init(Stack* stack, void (*destroy)(void* data)) {
ll_init(stack, destroy); ll_init(stack, destroy);

View File

@ -1,10 +1,11 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "linkedlist.h" #include "lflinkedlist.h"
#include "set.h" #include "lfset.h"
#include "stack.h" #include "lfstack.h"
#include "binarytree.h" #include "lfbinarytree.h"
#include "lfmath.h"
void print_ll(List* list) { void print_ll(List* list) {
for (ListNode* node = list->head; node != NULL; node = node->next) { for (ListNode* node = list->head; node != NULL; node = node->next) {
@ -146,10 +147,22 @@ void test_bintree() {
bintree_destroy(tree); bintree_destroy(tree);
} }
void test_math() {
printf("\n--- MATH TEST ---\n");
int i = 1, j = 2;
printf("Between %d and %d, %d is larger\n", i, j, max_int(i, j));
printf("Between %d and %d, %d is smaller\n", i, j, min_int(i, j));
char *s = "1010110";
printf("Binary: %s\n", s);
printf("Decimal: %d\n", binstr_to_int(s));
}
int main() { int main() {
test_ll(); test_ll();
test_set(); test_set();
test_stack(); test_stack();
test_bintree(); test_bintree();
test_math();
return 0; return 0;
} }