Compare commits

..

4 Commits
main ... main

14 changed files with 15 additions and 483 deletions

Binary file not shown.

View File

@ -1,9 +1,8 @@
#include <string.h>
#include "stack.h"
#include "bintree.h"
#include <stdlib.h>
// TODO: binären Suchbaum implementieren
//TODO: binären Suchbaum implementieren
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv),
* `clearTree`: gibt den gesamten Baum frei (rekursiv),
* `treeSize`: zählt die Knoten im Baum (rekursiv),
@ -13,116 +12,25 @@
// if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added).
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
{
if (isDuplicate)
*isDuplicate = 0;
if (root == NULL)
{
TreeNode *newNode = (TreeNode *)malloc(sizeof(TreeNode));
if (!newNode)
{
return NULL;
}
newNode->data = malloc(dataSize);
if (!newNode->data)
{
free(newNode);
return NULL;
}
memcpy(newNode->data, data, dataSize);
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
int cmp = compareFct(data, root->data);
if (cmp < 0)
{
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
}
else if (cmp > 0)
{
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
}
else
{
if (isDuplicate)
{
*isDuplicate = 1; // ignorieren
return root;
}
else
{
// Duplikate erlaubt -> nach rechts
root->right = addToTree(root->right, data, dataSize, compareFct, NULL);
}
}
return root;
}
// Iterates over the tree given by root. Follows the usage of strtok. If tree is NULL, the next entry of the last tree given is returned in ordering direction.
// Use your implementation of a stack to organize the iterator. Push the root node and all left nodes first. On returning the next element,
// push the top node and push all its left nodes.
static StackNode *iterator = NULL;
void *nextTreeData(TreeNode *root)
{
if (root != NULL)
{
clearStack(iterator);
iterator = NULL;
// Root + alle linken Nachfolger pushen
TreeNode *current = root;
while (current != NULL)
{
iterator = push(iterator, current);
current = current->left;
}
}
// Wenn Stack leer -> fertig
if (iterator == NULL)
{
return NULL;
}
// Top-Node holen
TreeNode *node = (TreeNode *)top(iterator);
iterator = pop(iterator);
// rechten Teilbaum + linke Kette pushen
TreeNode *right = node->right;
while (right != NULL)
{
iterator = push(iterator, right);
right = right->left;
}
return node->data;
}
// Releases all memory resources (including data copies).
void clearTree(TreeNode *root)
{
if (root != NULL)
{
clearTree(root->left);
clearTree(root->right);
free(root->data);
free(root);
}
}
// Returns the number of entries in the tree given by root.
unsigned int treeSize(const TreeNode *root)
{
if (root == NULL)
return 0;
return 1 + treeSize(root->left) + treeSize(root->right);
}

BIN
doble.exe

Binary file not shown.

Binary file not shown.

View File

@ -1,4 +1 @@
player_name;5967
player_name;4698
player_name;2994
jan;2993
player1;3999

View File

@ -29,47 +29,21 @@ program_obj_files = stack.o bintree.o numbers.o timer.o highscore.o
doble : main.o $(program_obj_files)
$(CC) $(FLAGS) $^ -o doble
$(program_obj_filesobj_files): %.o: %.c
$(program_obj_files): %.o: %.c
$(CC) -c $(FLAGS) $^ -o $@
# --------------------------
# Unit Tests
# --------------------------
unitTests: test_stack test_tree test_numbers
test_stack: test_stack.o stack.o unity/unity.c
$(CC) $(FLAGS) -I$(unityfolder) $^ -o test_stack
test_tree: test_tree.o bintree.o stack.o unity/unity.c
$(CC) $(FLAGS) -I$(unityfolder) $^ -o test_tree
test_numbers: test_numbers.o numbers.o bintree.o stack.o unity/unity.c
$(CC) $(FLAGS) -I$(unityfolder) $^ -o test_numbers
test_stack.o: test_stack.c
$(CC) -c $(FLAGS) -I$(unityfolder) $< -o $@
stack.o: stack.c
$(CC) -c $(FLAGS) $< -o $@
test_tree.o: test_tree.c
$(CC) -c $(FLAGS) -I$(unityfolder) $< -o $@
test_numbers.o: test_numbers.c
$(CC) -c $(FLAGS) -I$(unityfolder) $< -o $@
bintree.o: bintree.c
$(CC) -c $(FLAGS) $< -o $@
numbers.o: numbers.c
$(CC) -c $(FLAGS) $< -o $@
unitTests:
echo "needs to be implemented"
# --------------------------
# Clean
# --------------------------
clean:
ifeq ($(OS),Windows_NT)
del /f *.o doble $(TEST_BIN).exe
del /f *.o doble
else
rm -f *.o doble $(TEST_BIN)
rm -f *.o doble
endif

View File

@ -5,7 +5,7 @@
#include "numbers.h"
#include "bintree.h"
// TODO: getDuplicate und createNumbers implementieren
//TODO: getDuplicate und createNumbers implementieren
/* * * Erzeugen eines Arrays mit der vom Nutzer eingegebenen Anzahl an Zufallszahlen.
* Sicherstellen, dass beim Befüllen keine Duplikate entstehen.
* Duplizieren eines zufälligen Eintrags im Array.
@ -14,99 +14,13 @@
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
// creating random numbers.
static void duplicateRandomEntry(unsigned int *numbers, unsigned int len)
{
if (!numbers || len < 2)
return;
unsigned int src = rand() % len;
unsigned int dst = rand() % len;
while (dst == src)
dst = rand() % len;
numbers[dst] = numbers[src];
}
static int compare(const void *a, const void *b)
{
unsigned int x = *(const unsigned int *)a;
unsigned int y = *(const unsigned int *)b;
return (x > y) - (x < y);
}
unsigned int *createNumbers(unsigned int len)
{
if (len < 2)
return NULL;
unsigned int *numbers = malloc(sizeof(unsigned int) * len);
if (!numbers)
return NULL;
static int seeded = 0;
if (!seeded) {
srand((unsigned)time(NULL));
seeded = 1;
}
TreeNode *root = NULL;
unsigned int i = 0;
while (i < len)
{
unsigned int val = (rand() % (2 * len)) + 1;
int isDup = 0;
root = addToTree(root, &val, sizeof(unsigned int), compare, &isDup);
if (!root)
{
free(numbers); // malloc-Fehler
return NULL;
}
if (!isDup)
numbers[i++] = val;
}
duplicateRandomEntry(numbers, len);
clearTree(root);
return numbers;
}
// Returns only the only number in numbers which is present twice. Returns zero on errors.
unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
{
if (!numbers || len < 2)
{
return 0;
}
unsigned int *copy = (unsigned int *)malloc(len * sizeof(unsigned int));
if (!copy)
{
return 0;
}
memcpy(copy, numbers, len * sizeof(unsigned int));
qsort(copy, len, sizeof(unsigned int), compare);
unsigned int dup = 0;
for (unsigned int i = 1; i < len; i++)
{
if (copy[i] == copy[i - 1])
{
dup = copy[i];
break;
}
}
free(copy);
return dup;
}

29
stack.c
View File

@ -10,53 +10,24 @@
// Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data)
{
StackNode *newNode = malloc(sizeof(StackNode));
if(newNode == NULL)
{
return stack; //Speicher konnte nicht allokiert werden.
}
newNode->data = data;
newNode->next = stack;
return newNode;
}
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
// freed by caller.)
StackNode *pop(StackNode *stack)
{
if (stack == NULL)
{
return NULL; // stack leer nichts zu löschen
}
StackNode *next = stack->next;
free(stack);
return next;
}
// Returns the data of the top element.
void *top(StackNode *stack)
{
if(stack == NULL)
{
return NULL;
}
return stack->data;
}
// Clears stack and releases all memory.
void clearStack(StackNode *stack)
{
while (stack != NULL)
{
StackNode *next = stack->next;
free(stack);
stack = next;
}
}

View File

@ -9,11 +9,6 @@ The latest element is taken from the stack. */
//TODO: passenden Datentyp als struct anlegen
typedef struct StackNode {
void *data;
struct StackNode* next;
} StackNode;
// Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data);

View File

@ -1,72 +0,0 @@
#include <stdlib.h>
#include "numbers.h"
#include "unity.h"
#include "unity_internals.h"
void test_createNumbers_no_null(void)
{
unsigned int *arr = createNumbers(10);
TEST_ASSERT_NOT_NULL(arr);
free(arr);
}
void test_createNumbers_has_exactly_one_duplicate(void)
{
unsigned int len = 100;
unsigned int *arr = createNumbers(len);
TEST_ASSERT_NOT_NULL(arr);
unsigned int dup = getDuplicate(arr, len);
TEST_ASSERT_NOT_EQUAL(0, dup);
int count = 0;
for (unsigned int i = 0; i < len; i++)
{
if (arr[i] == dup)
count++;
}
TEST_ASSERT_EQUAL(2, count);
free(arr);
}
void test_createNumbers_has_correct_value_range(void)
{
unsigned int len = 10;
unsigned int *arr = createNumbers(len);
TEST_ASSERT_NOT_NULL(arr);
// passendes Interval
for (unsigned int i = 0; i < len; i++)
{
TEST_ASSERT_TRUE(arr[i] >= 1);
TEST_ASSERT_TRUE(arr[i] <= 2 * len);
}
free(arr);
}
void test_getDuplicate_returns_correct_value(void)
{
unsigned int arr[6] = {4, 1, 7, 7, 3, 2};
unsigned int d = getDuplicate(arr, 6);
TEST_ASSERT_EQUAL_UINT(7, d);
}
void setUp(void) { }
void tearDown(void) { }
int main(void)
{
UNITY_BEGIN();
printf("\n============================\n Numbers tests\n============================\n");
RUN_TEST(test_createNumbers_no_null);
RUN_TEST(test_createNumbers_has_exactly_one_duplicate);
RUN_TEST(test_createNumbers_has_correct_value_range);
RUN_TEST(test_getDuplicate_returns_correct_value);
return UNITY_END();
}

Binary file not shown.

View File

@ -1,155 +0,0 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "unity.h"
#include "stack.h"
// Hilfsfunktion: int dynamisch anlegen
static int *makeInt(int value)
{
int *p = (int *)malloc(sizeof(int));
if (p != NULL)
{
*p = value;
}
return p;
}
void test_pushAndTopReturnLastPushed(void)
{
StackNode *stack = NULL;
int *v1 = makeInt(10);
int *v2 = makeInt(20);
int *v3 = makeInt(30);
TEST_ASSERT_NOT_NULL(v1);
TEST_ASSERT_NOT_NULL(v2);
TEST_ASSERT_NOT_NULL(v3);
stack = push(stack, v1);
TEST_ASSERT_NOT_NULL(stack);
TEST_ASSERT_EQUAL_PTR(v1, top(stack));
TEST_ASSERT_EQUAL_INT(10, *(int *)top(stack));
stack = push(stack, v2);
TEST_ASSERT_EQUAL_PTR(v2, top(stack));
TEST_ASSERT_EQUAL_INT(20, *(int *)top(stack));
stack = push(stack, v3);
TEST_ASSERT_EQUAL_PTR(v3, top(stack));
TEST_ASSERT_EQUAL_INT(30, *(int *)top(stack));
TEST_ASSERT_EQUAL_PTR(v3, top(stack));
free(v3);
stack = pop(stack);
TEST_ASSERT_EQUAL_PTR(v2, top(stack));
free(v2);
stack = pop(stack);
TEST_ASSERT_EQUAL_PTR(v1, top(stack));
free(v1);
stack = pop(stack);
TEST_ASSERT_NULL(stack);
}
void test_popOnEmptyStackReturnsNull(void)
{
StackNode *stack = NULL;
StackNode *newStack = pop(stack);
TEST_ASSERT_NULL(newStack);
TEST_ASSERT_NULL(top(newStack));
}
void test_pushAndPopLifoOrder(void)
{
StackNode *stack = NULL;
int *v1 = makeInt(1);
int *v2 = makeInt(2);
int *v3 = makeInt(3);
TEST_ASSERT_NOT_NULL(v1);
TEST_ASSERT_NOT_NULL(v2);
TEST_ASSERT_NOT_NULL(v3);
stack = push(stack, v1);
stack = push(stack, v2);
stack = push(stack, v3);
TEST_ASSERT_EQUAL_PTR(v3, top(stack));
TEST_ASSERT_EQUAL_INT(3, *(int *)top(stack));
free(v3);
stack = pop(stack);
TEST_ASSERT_EQUAL_PTR(v2, top(stack));
TEST_ASSERT_EQUAL_INT(2, *(int *)top(stack));
free(v2);
stack = pop(stack);
TEST_ASSERT_EQUAL_PTR(v1, top(stack));
TEST_ASSERT_EQUAL_INT(1, *(int *)top(stack));
free(v1);
stack = pop(stack);
TEST_ASSERT_NULL(stack);
}
void test_topOnEmptyStackReturnsNull(void)
{
StackNode *stack = NULL;
TEST_ASSERT_NULL(top(stack));
}
void test_clearStackFreesAllNodes(void)
{
StackNode *stack = NULL;
int a = 11;
int b = 22;
int c = 33;
stack = push(stack, &a);
stack = push(stack, &b);
stack = push(stack, &c);
TEST_ASSERT_NOT_NULL(stack);
TEST_ASSERT_EQUAL_INT(33, *(int *)top(stack));
clearStack(stack);
stack = NULL;
}
void setUp(void)
{
}
void tearDown(void)
{
}
int main(void)
{
UNITY_BEGIN();
printf("\n============================\nStack tests\n============================\n");
RUN_TEST(test_pushAndTopReturnLastPushed);
RUN_TEST(test_popOnEmptyStackReturnsNull);
RUN_TEST(test_pushAndPopLifoOrder);
RUN_TEST(test_topOnEmptyStackReturnsNull);
RUN_TEST(test_clearStackFreesAllNodes);
return UNITY_END();
}

Binary file not shown.

Binary file not shown.