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)
set(SOURCES
src/linkedlist.c
src/set.c
src/stack.c
src/binarytree.c
src/input.c
src/math.c
src/lflinkedlist.c
src/lfset.c
src/lfstack.c
src/lfbinarytree.c
src/lfinput.c
src/lfmath.c
)
add_library(flint ${SOURCES})

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,6 +1,6 @@
#include <string.h>
#include "math.h"
#include "lfmath.h"
int max_int(int a, int b) {
if (a > b) {
@ -18,7 +18,7 @@ int min_int(int a, int b) {
int binstr_to_int(const char *s) {
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') {
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 (*destroy)(void* data)) {

View File

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

View File

@ -1,10 +1,11 @@
#include <stdio.h>
#include <stdlib.h>
#include "linkedlist.h"
#include "set.h"
#include "stack.h"
#include "binarytree.h"
#include "lflinkedlist.h"
#include "lfset.h"
#include "lfstack.h"
#include "lfbinarytree.h"
#include "lfmath.h"
void print_ll(List* list) {
for (ListNode* node = list->head; node != NULL; node = node->next) {
@ -146,10 +147,22 @@ void test_bintree() {
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() {
test_ll();
test_set();
test_stack();
test_bintree();
test_math();
return 0;
}