5 Commits
Author SHA1 Message Date
silvana884 dcdc1949ab Noch ein paar Kommentare zum binTree 2025-12-12 06:49:18 +01:00
haemmerlre98889 f513d63ebf Kommentare angepasst 2025-12-11 15:47:51 +01:00
silvana884 f249355a30 Noch ein bisschen kommentiert 2025-12-11 10:00:44 +01:00
silvana884 8f3ee7b9d7 Fertig 2025-12-11 09:46:50 +01:00
silvana884 37f38cbc7d Numbers Test erfolgreich ebstanden 2025-12-11 09:15:03 +01:00
19 changed files with 162 additions and 85 deletions

No files matched your search

+7 -6
View File
@@ -38,7 +38,7 @@ void test_add_multiple_elements_to_Tree()
for(int j = 0; j < 4; ++j) for(int j = 0; j < 4; ++j)
{ {
root = addToTree(root, &value[j], sizeof(int), compare, &duplicate); root = addToTree(root, &value[j], sizeof(int), compare, &duplicate); //Duplikate nicht erlaubt
} }
TEST_ASSERT_EQUAL_INT(4, treeSize(root)); TEST_ASSERT_EQUAL_INT(4, treeSize(root));
@@ -62,13 +62,13 @@ void test_detect_size() {
for (int j = 0; j < 10; ++j) { for (int j = 0; j < 10; ++j) {
root = addToTree(root, &values[j], sizeof(int), compare, &duplicate); root = addToTree(root, &values[j], sizeof(int), compare, &duplicate);
if (duplicate) { if (duplicate) {
// Optional: prüfen, dass ein Duplikat erkannt wurde // Ist der Wert schon eingefuegt? Also gibt es schon ein Duplikat?
TEST_ASSERT_TRUE(duplicate == 1); TEST_ASSERT_TRUE(duplicate == 1);
} }
duplicate = 0; // zurücksetzen für nächstes Einfügen duplicate = 0; // zurücksetzen für nächstes Einfügen
} }
// Prüfen der Baumgröße ohne Duplikate // Prüfen der Baumgroeße ohne Duplikate
TEST_ASSERT_EQUAL_INT(8, treeSize(root)); TEST_ASSERT_EQUAL_INT(8, treeSize(root));
clearTree(root); clearTree(root);
@@ -91,7 +91,7 @@ void test_add_multiplie_elements_one_dup() {
} }
//Traverses the tree inorder to check wether nextTreeData works
// Hilfsfunktion: rekursive Inorder-Prüfung // Hilfsfunktion: rekursive Inorder-Prüfung
void inorderCheck(TreeNode *node, int expected[], int *idx) { void inorderCheck(TreeNode *node, int expected[], int *idx) {
if (node == NULL) return; if (node == NULL) return;
@@ -99,14 +99,14 @@ void inorderCheck(TreeNode *node, int expected[], int *idx) {
// Linken Teilbaum prüfen // Linken Teilbaum prüfen
inorderCheck(node->left, expected, idx); inorderCheck(node->left, expected, idx);
// Aktuelles Element prüfen // Aktuelles Element prüfen -> wenn das aktuelle Element gefunden wurde, wird naechstes gesucht
TEST_ASSERT_EQUAL_INT(expected[*idx], *(int*)node->data); TEST_ASSERT_EQUAL_INT(expected[*idx], *(int*)node->data);
(*idx)++; (*idx)++;
// Rechten Teilbaum prüfen // Rechten Teilbaum prüfen
inorderCheck(node->right, expected, idx); inorderCheck(node->right, expected, idx);
} }
//Traverses the tree inorder to check wether nextTreeData works
void test_inorder() { void test_inorder() {
TreeNode *root = NULL; TreeNode *root = NULL;
int values[] = {5, 3, 7, 2, 4, 6, 8}; int values[] = {5, 3, 7, 2, 4, 6, 8};
@@ -120,6 +120,7 @@ void test_inorder() {
int expected[] = {2,3,4,5,6,7,8}; int expected[] = {2,3,4,5,6,7,8};
int idx = 0; int idx = 0;
//rekursives Pruefen der Eintraege
inorderCheck(root, expected, &idx); inorderCheck(root, expected, &idx);
// Alle Einträge geprüft? // Alle Einträge geprüft?
+3 -3
View File
@@ -14,7 +14,7 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
{ {
if(data!= NULL && dataSize > 0) if(data!= NULL && dataSize > 0)
{ {
if(root == NULL) //Abbruchbedingung: Keine Wurzel vorhanden, deshalb fügen wir hier einen neuen Knote ein if(root == NULL) //Abbruchbedingung: Keine Wurzel vorhanden, deshalb fuegen wir hier einen neuen Knote ein
{ {
TreeNode *newNode = (TreeNode *)malloc(sizeof(TreeNode)); TreeNode *newNode = (TreeNode *)malloc(sizeof(TreeNode));
if(newNode == NULL) if(newNode == NULL)
@@ -31,7 +31,7 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
newNode->left = NULL; newNode->left = NULL;
newNode->right = NULL; newNode->right = NULL;
if(isDuplicate!= NULL) //wenn Zeiger isDUplicate auf einen Wert zeigt, wird isDuplicate auf 0 gesetzt if(isDuplicate!= NULL) //wenn isDuplicate ungelich null, ignoriere duplikate und setze isDuplaicate 0 fuer neues Element
{ {
*isDuplicate = 0; *isDuplicate = 0;
} }
@@ -48,7 +48,7 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
} }
else else
{ {
if (isDuplicate) { if (isDuplicate) { //Duplikate sollen ignoriert werden
*isDuplicate = 1; *isDuplicate = 1;
} }
else { else {
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+2
View File
@@ -1,3 +1,5 @@
Silvana;9944 Silvana;9944
hannes;9910 hannes;9910
silvana;9865
player2;4983
player1;3999 player1;3999
BIN
View File
Binary file not shown.
+31 -31
View File
@@ -1,10 +1,17 @@
CC = gcc CC = gcc
FLAGS = -g -Wall -lm
raylibfolder = ./raylib
unityfolder = ./unity
FLAGS = -g -Wall -I$(unityfolder)
ifeq ($(OS),Windows_NT) ifeq ($(OS),Windows_NT)
include makefile_windows.variables include makefile_windows.variables
else else
UNAME = $(shell uname) UNAME := $(shell uname)
ifeq ($(UNAME),Linux) ifeq ($(UNAME),Linux)
include makefile_linux.variables include makefile_linux.variables
else else
@@ -12,55 +19,48 @@ else
endif endif
endif endif
raylibfolder = ./raylib # --------------------------
unityfolder = ./unity # Objektdateien
# --------------------------
program_obj_files := stack.o bintree.o numbers.o timer.o highscore.o
# --------------------------
# Initiales Programm bauen (zum ausprobieren)
# --------------------------
doble_initial:
$(CC) -o doble_initial $(BINARIES)/libdoble_complete.a
# -------------------------- %.o: %.c
# Selbst implementiertes Programm bauen $(CC) $(FLAGS) -c $< -o $@
# --------------------------
program_obj_files = stack.o bintree.o numbers.o timer.o highscore.o
doble: main.o $(program_obj_files) doble: main.o $(program_obj_files)
$(CC) $(FLAGS) $^ -o doble $(CC) $(FLAGS) $^ -o doble
$(program_obj_filesobj_files): %.o: %.c
$(CC) -c $(FLAGS) $^ -o $@ doble_initial:
$(CC) -o doble_initial $(BINARIES)/libdoble_complete.a
# -------------------------- # --------------------------
# Unit Tests # Unit Tests
# -------------------------- # --------------------------
unity_src = $(unityfolder)/unity.c
unitTests: numbersTest stackTest bintreeTest unitTests:
# ./runNumbersTest @echo "needs to be implemented"
# ./runStackTest
./runBintreeTest
numbersTest: numbers.o bintree.o stack.o numbersTest.c $(unity_src)
$(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runNumbersTest
stackTest: stack.o stackTest.c $(unity_src) binTreeTest: stack.o bintree.o binTreeTest.c $(unityfolder)/unity.c
$(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runStackTests $(CC) $(FLAGS) -o runbinTreeTest binTreeTest.c bintree.o stack.o $(unityfolder)/unity.c
binTreeTest: bintree.o binTreeTest.c $(unity_src) stack.o
$(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runBinTreeTest
%.o: %.c test_numbers: numbers_no_tree.o bintree.o stack.o test_numbers.c $(unityfolder)/unity.c
$(CC) -c $(CFLAGS) $< -o $@ $(CC) $(FLAGS) -o run_numbersTests test_numbers.c numbers_no_tree.o bintree.o stack.o $(unityfolder)/unity.c
test_stack: stack.o test_stack.c $(unityfolder)/unity.c
$(CC) $(FLAGS) -o runstackTests test_stack.c stack.o $(unityfolder)/unity.c
# -------------------------- # --------------------------
# Clean # Cleaning
# -------------------------- # --------------------------
clean: clean:
ifeq ($(OS),Windows_NT) ifeq ($(OS),Windows_NT)
del /f *.o doble del /f *.o doble runstackTests run_numbersTests runbintreeTests
else else
rm -f *.o doble rm -f *.o doble runstackTests run_numbersTests runbintreeTests
endif endif
+34 -31
View File
@@ -17,13 +17,28 @@
// Returns len random numbers between 1 and 2*len in random order, // Returns len random numbers between 1 and 2*len in random order,
// all different, except for exactly one duplicate (two entries the same). // all different, except for exactly one duplicate (two entries the same).
// Uses your binary search tree implementation to check for duplicates while generating numbers. // Uses your binary search tree implementation to check for duplicates while generating numbers.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "numbers.h"
#include "bintree.h"
int compareFct(const void *a, const void *b)
{
return (*(int *)a > *(int *)b) - (*(int *)a < *(int *)b); // a und b werden in int konvertiert und deren Werte miteinander verglichen
// returns 1 for a>b or -1 for a<b
// in bintree.c wird ueberprueft, ob compare eine positive oder eine negative Zahl zurueckgibt,
// wenn a groeßer b, positiv und dann wird links nach Teilbauemen gesucht
}
// Erzeugt len Zufallszahlen zwischen 1 und 2*len
// alle einzigartig, außer genau ein Duplikat
unsigned int *createNumbers(unsigned int len) unsigned int *createNumbers(unsigned int len)
{ {
if (len < 2) if (len < 2)
return NULL; return NULL;
srand(time(NULL)); srand((unsigned int)time(NULL));
unsigned int *numbers = malloc(len * sizeof(unsigned int)); unsigned int *numbers = malloc(len * sizeof(unsigned int));
if (!numbers) if (!numbers)
@@ -33,27 +48,30 @@ unsigned int *createNumbers(unsigned int len)
unsigned int count = 0; unsigned int count = 0;
// Zufallszahlen generieren, bis das Array voll ist // Zufallszahlen generieren, bis das Array voll ist
while (count < len) { while (count < len)
{
unsigned int random = (rand() % (2 * len)) + 1; unsigned int random = (rand() % (2 * len)) + 1;
int duplicate = 0; // Anfangswert für Duplikat-Check int duplicate = 0; // Anfangswert für Duplikat-Check
root = addToTree(root, &random, sizeof(random), compareFct, &duplicate); root = addToTree(root, &random, sizeof(random), compareFct, &duplicate);
if (root == NULL) { if (root == NULL)
{
free(numbers); free(numbers);
return NULL; return NULL;
} }
if (!duplicate) { // Zahl war neu → ins Array einfügen if (!duplicate)
{
numbers[count++] = random; numbers[count++] = random;
} }
// duplicate == 1 → Zahl existiert schon, neue Zahl generieren // duplicate == 1 → Zahl existiert schon, neue Zahl generieren
} }
// Jetzt len eindeutige Zahlen erzeugt → ein Duplikat erzwingen // genau ein Duplikat erzeugen
unsigned int idx1 = rand() % len; unsigned int idx1 = rand() % len;
unsigned int idx2 = rand() % len; unsigned int idx2 = rand() % len;
while (idx2 == idx1) // sicherstellen, dass es eine andere Position ist while (idx2 == idx1)
idx2 = rand() % len; idx2 = rand() % len;
numbers[idx2] = numbers[idx1]; numbers[idx2] = numbers[idx1];
@@ -64,38 +82,23 @@ unsigned int *createNumbers(unsigned int len)
return numbers; return numbers;
} }
// Jetzt len eindeutige Zahlen erzeugt ⇒ wir müssen ein Duplikat erzwingen
unsigned int idx1 = rand() % len;
unsigned int idx2 = rand() % len;
while (idx2 == idx1)
idx2 = rand() % len;
numbers[idx2] = numbers[idx1]; // zweites Exemplar // findet die eine doppelte Zahl im Array
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) unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
{ {
if(len>0) if (!numbers || len < 2)
{ return 0;
unsigned int duplicate = 0;
for (unsigned int i = 0; i < len; i++) for (unsigned int i = 0; i < len; i++)
{ {
unsigned int v1 = numbers[i];
for (unsigned int j = i + 1; j < len; j++) for (unsigned int j = i + 1; j < len; j++)
{ {
unsigned int v2 = numbers[j]; if (numbers[i] == numbers[j])
if(v1==v2) return numbers[i];
{
return v1;
}
}
} }
} }
return 0; return 0;
} }
+2
View File
@@ -1,6 +1,8 @@
#ifndef NUMBERS_H #ifndef NUMBERS_H
#define NUMBERS_H #define NUMBERS_H
int compareFct(const void *a, const void *b);
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries. // 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 // Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
// creating random numbers. // creating random numbers.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
View File
Binary file not shown.
+11 -14
View File
@@ -1,16 +1,14 @@
#include <stdlib.h> #include <stdlib.h>
#include "stack.h" #include "stack.h"
//TODO: grundlegende Stackfunktionen implementieren:
/* * `push`: legt ein Element oben auf den Stack,
* `pop`: entfernt das oberste Element,
* `top`: liefert das oberste Element zurück,
* `clearStack`: gibt den gesamten Speicher frei. */
// Pushes data as pointer onto the stack. // Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data) StackNode *push(StackNode *stack, void *data)
{ {
if(stack && data){ if (!data)
{
return stack; //Nichts pushen
}
StackNode *t = (StackNode *)malloc(sizeof(StackNode)); StackNode *t = (StackNode *)malloc(sizeof(StackNode));
if(!t) if(!t)
{ {
@@ -19,7 +17,7 @@ StackNode *push(StackNode *stack, void *data)
t->next = stack; t->next = stack;
t->data = data; t->data = data;
return t; //Gibt den ersten StackNode des Stacks zurueck return t; //Gibt den ersten StackNode des Stacks zurueck
}
return NULL; return NULL;
} }
@@ -27,12 +25,11 @@ StackNode *push(StackNode *stack, void *data)
// freed by caller.) // freed by caller.)
StackNode *pop(StackNode *stack) StackNode *pop(StackNode *stack)
{ {
if(stack) if(stack == NULL)
{ {
StackNode *t = stack->next; //Naechstes Element im Stack wird erstes Element return NULL;
free(stack);
return t;
} }
return stack->next;
} }
// Returns the data of the top element. // Returns the data of the top element.
@@ -50,8 +47,8 @@ void clearStack(StackNode *stack)
{ {
while(stack) while(stack)
{ {
StackNode *tmp = stack; StackNode *tmp = stack; //merkt sich den momentanen obersten Knoten
stack = stack->next; stack = stack->next; //setzt den obersten Knoten auf den Zweiten im Stack
free(tmp->data); free(tmp->data);
free(tmp); free(tmp);
} }
BIN
View File
Binary file not shown.
+72
View File
@@ -0,0 +1,72 @@
#include <stdlib.h>
#include <stdio.h>
#include "stack.h"
//Testfunkionen zu push, pull, top & clearStack schreiben
void setUp()
{
}
void tearDown()
{
}
void test(char *name, int condition) {
if (condition) {
printf("[OK] %s\n", name);
} else {
printf("[FAIL] %s\n", name);
}
}
int main() {
StackNode *stack = NULL;
// Werte dynamisch anlegen
int *val1 = malloc(sizeof(int));
*val1 = 5;
stack = push(stack, val1);
test("push(5) legt 5 oben auf den Stack", *(int*)stack->data == 5);
int *val2 = malloc(sizeof(int));
*val2 = 6;
stack = push(stack, val2);
test("push(6) legt 6 oben auf den Stack", *(int*)stack->data == 6);
int *val3 = malloc(sizeof(int));
*val3 = 24;
stack = push(stack, val3);
test("push(24) legt 24 oben auf den Stack", *(int*)stack->data == 24);
// Test top()
int t = *(int*)top(stack);
test("top() liefert 24", t == 24);
// Test pop()
StackNode *tmp;
tmp = stack;
stack = pop(stack);
free(tmp->data); // Daten freigeben
free(tmp); // Knoten freigeben
test("pop() entfernt 24, 6 ist jetzt oben", *(int*)stack->data == 6);
tmp = stack;
stack = pop(stack);
free(tmp->data);
free(tmp);
test("pop() entfernt 6, 5 ist jetzt oben", *(int*)stack->data == 5);
tmp = stack;
stack = pop(stack);
free(tmp->data);
free(tmp);
test("pop() entfernt 5, Stack ist jetzt leer", stack == NULL);
// Am Ende Stack leeren (falls noch Elemente übrig)
clearStack(stack);
return 0;
}
BIN
View File
Binary file not shown.