More examples

This commit is contained in:
Leonardo Vencovsky committed 2023-01-24 22:23:33 +01:00
1 parent 301f080713
commit a8c1619be6
6 files changed
+306 -4

No files matched your search

+31 -3
View File
@@ -1,9 +1,37 @@
CC=gcc
CFLAGS=-Wall -Wextra -Werror
CFLAGS=-Wall -Wextra -Werror -O3
INCLUDE=-I ../src
dfs: dfs.c
$(CC) $< -o $@.exe $(CFLAGS) $(INCLUDE)
./$@.exe
example: example.c
$(CC) $< -o $@.exe $(CFLAGS) $(INCLUDE)
./$@.exe
run: example
./example.exe
hanoi: hanoi.c
$(CC) $< -o $@.exe $(CFLAGS) $(INCLUDE)
./$@.exe
heapsort: heapsort.c
$(CC) $< -o $@.exe $(CFLAGS) $(INCLUDE)
./$@.exe
sieve: sieve.c
$(CC) $< -o $@.exe $(CFLAGS) $(INCLUDE)
./$@.exe
clean:
rm *.exe
rm *.txt
memcheck:
ifndef FILE
$(error FILE parameter must be set first to check for leaks)
else
$(CC) $(FILE) -o mem.exe $(CFLAGS) $(INCLUDE) -g
valgrind --leak-check=yes ./mem.exe
endif
+94
View File
@@ -0,0 +1,94 @@
/**
* Example of In-Order Depth-First Search using:
* - A Stack
* - Recursion
* - Without both
*/
#define CMC_CAMEL_CASE
#include "cmc/cor/core.h"
#include "cmc/utl/assert.h"
#include "cmc/utl/futils.h"
#include "cmc/utl/log.h"
#define PFX tree
#define SNAME BinaryTree
#define V int32_t
#define CMC_EXT_ITER
#define CMC_EXT_STR
#include "cmc/treeset.h"
#define PFX stk
#define SNAME Stack
#define V struct BinaryTreeNode *
#include "cmc/stack.h"
void DFS(struct BinaryTreeNode *root)
{
if (!root)
return;
DFS(root->left);
fprintf(stdout, "%d ", root->value);
DFS(root->right);
}
int main(void)
{
struct BinaryTree *tree = tree_new(&(struct BinaryTreeFVal){ .cmp = cmc_i32_cmp, .str = cmc_i32_str, NULL });
for (int i = 1; i <= 10; i++)
tree_insert(tree, i);
int32_t result = 1;
cmc_assert(tree_min(tree, &result));
cmc_assert_equals(int32_t, 1, result);
cmc_assert(tree_max(tree, &result));
cmc_assert_equals(int32_t, 10, result);
printf("Without Recursion or Stack:\n");
tree_print(tree, stdout, "[ ", " ", " ]\n");
printf("Recursion:\n");
fprintf(stdout, "[ ");
DFS(tree->root);
fprintf(stdout, "]\n");
printf("Stack:\n");
fprintf(stdout, "[ ");
struct Stack *stack = stk_new(100, &(struct StackFVal){ NULL });
struct BinaryTreeNode *node = tree->root;
while (node || !stk_empty(stack))
{
if (node)
{
if (!stk_push(stack, node))
cmc_log_error("Could not push to stack %d\n", cmc_flags_to_str[stk_flag(stack)]);
node = node->left;
}
else
{
node = stk_top(stack);
if (!stk_pop(stack))
cmc_log_error("Could not pop from the stack %s\n", cmc_flags_to_str[stk_flag(stack)]);
fprintf(stdout, "%d ", node->value);
node = node->right;
}
}
stk_free(stack);
fprintf(stdout, "]\n");
tree_free(tree);
}
+73
View File
@@ -0,0 +1,73 @@
#define V int
#define PFX stk
#define SNAME stack
#define CMC_EXT_INIT
#include "cmc/stack.h"
const int disks = 6;
struct
{
struct stack *from;
struct stack *aux;
struct stack *to;
} print_aux = { NULL };
void print_stacks()
{
for (int i = 9; i >= 0; i--)
{
int ia = print_aux.from->buffer[i];
int ib = print_aux.aux->buffer[i];
int ic = print_aux.to->buffer[i];
printf("| %c | %c | %c |\n", ia == 0 ? 32 : ia + 48, ib == 0 ? 32 : ib + 48, ic == 0 ? 32 : ic + 48);
}
printf("+---+---+---+\n\n");
}
void move(struct stack *from, struct stack *to)
{
stk_push(to, stk_top(from));
stk_pop(from);
}
void hanoi(int n, struct stack *from, struct stack *aux, struct stack *to)
{
if (n == 0)
return;
hanoi(n - 1, from, to, aux);
move(from, to);
print_stacks();
hanoi(n - 1, aux, from, to);
}
int main()
{
struct stack from = stk_init(10, &(struct stack_fval){ NULL });
struct stack to = stk_init(10, &(struct stack_fval){ NULL });
struct stack aux = stk_init(10, &(struct stack_fval){ NULL });
for (int i = disks; i >= 1; i--)
stk_push(&from, i);
print_aux.from = &from;
print_aux.aux = &aux;
print_aux.to = &to;
print_stacks();
hanoi(stk_count(&from), &from, &aux, &to);
stk_release(from);
stk_release(to);
stk_release(aux);
return 0;
}
+69
View File
@@ -0,0 +1,69 @@
/**
* heapsort.c
*
* Creation Date: 25/03/2019
*
* Authors:
* Leonardo Vencovsky (https://github.com/LeoVen)
*
*/
#include <stdio.h>
#include "cmc/cor/core.h"
#include "cmc/utl/futils.h"
#define PFX h
#define SNAME heap
#define V int
#define CMC_EXT_ITER
#include "cmc/heap.h"
int main()
{
struct heap *h = h_new(32, CMC_MIN_HEAP, &(struct heap_fval){ .cmp = cmc_i32_cmp });
for (int i = 1000; i > 0; i--)
{
if (!h_insert(h, i))
{
printf("Failed to insert: %s", cmc_flags_to_str[h_flag(h)]);
return h_flag(h);
}
}
size_t S = h_count(h);
int *buff = malloc(sizeof(int) * S);
int index = 0;
while (!h_empty(h))
{
int tmp = h_peek(h);
if (h_flag(h) == CMC_FLAG_OK)
{
buff[index++] = tmp;
h_remove(h);
}
else
{
printf("Heap peek failed: %s", cmc_flags_to_str[h_flag(h)]);
return h_flag(h);
}
}
for (size_t i = 0; i < S; i++)
{
if (i == 0)
printf("[ %d, ", buff[i]);
else if (i == S - 1)
printf("%d ]\n", buff[i]);
else
printf("%d, ", buff[i]);
}
free(buff);
h_free(h);
return 0;
}
+38
View File
@@ -0,0 +1,38 @@
#include <math.h>
#define PFX bs
#define SNAME bitset
#include "cmc/bitset.h"
// All the prime numbers up to 1 billion
int main(void)
{
const size_t n = 1000000000;
struct bitset *set = bs_new(n);
if (!set)
return 1;
bs_set_range(set, 0, n);
size_t up_to = sqrt((double)n) + 1;
for (size_t i = 2; i < up_to; ++i)
{
if (bs_get(set, i))
{
for (size_t j = 2 * i; j < n; j += i)
bs_clear(set, j);
}
}
FILE *file = fopen("primes.txt", "w");
for (size_t i = 2; i < n; i++)
{
if (bs_get(set, i))
fprintf(file, "%" PRIuMAX "\n", i);
}
fclose(file);
}