generated from freudenreichan/info2Praktikum-DobleSpiel
Compare commits
8
Commits
c7ff696b67
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f5d91286ce | ||
|
|
a7e328ae5c | ||
|
|
027a8eaeb7 | ||
|
|
fdfa3c5769 | ||
|
|
85b05133ea | ||
|
|
53f4716354 | ||
|
|
b7249a9c1c | ||
|
|
cbafb5f0f2 |
No files matched your search
@@ -1,6 +1,9 @@
|
||||
#include <string.h>
|
||||
#include "stack.h"
|
||||
#include "bintree.h"
|
||||
#include <stdio.h>
|
||||
|
||||
StackNode *stackRoot;//Generates a new, empty stack for next tree data
|
||||
|
||||
//TODO: binären Suchbaum implementieren
|
||||
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv),
|
||||
@@ -14,28 +17,28 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
|
||||
{
|
||||
if(!root)
|
||||
{
|
||||
root = malloc(sizeof(node));
|
||||
*node newNode;
|
||||
*newNode.data = malloc(dataSize);
|
||||
strcpy(*newNode.data, data);
|
||||
*newNode.left = NULL;
|
||||
*newNode.right = NULL;
|
||||
//root = malloc(sizeof(TreeNode));
|
||||
TreeNode *newNode = malloc(sizeof(TreeNode));
|
||||
newNode->data = malloc(dataSize);
|
||||
newNode->left = NULL;
|
||||
newNode->right = NULL;
|
||||
memcpy(newNode->data, data, dataSize);
|
||||
if(isDuplicate)
|
||||
*isDuplicate = 0;
|
||||
return newNode;
|
||||
}
|
||||
if(compareFct(*data, *root->data) > 0)
|
||||
if(compareFct(data, root->data) > 0)
|
||||
{
|
||||
*right = addToTree(right, data, dataSize, compareFct, isDuplicate);
|
||||
root->left = addToTree((root->left), data, dataSize, compareFct, isDuplicate);
|
||||
}
|
||||
else if(compareFct(*data, *root->data) < 0)
|
||||
else if(compareFct(data, root->data) < 0)
|
||||
{
|
||||
*left = addToTree(right, data, dataSize, compareFct, isDuplicate);
|
||||
root->right = addToTree((root->right), data, dataSize, compareFct, isDuplicate);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!isDuplicate)
|
||||
*left = addToTree(right, data, dataSize, compareFct, isDuplicate);
|
||||
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
|
||||
else
|
||||
{
|
||||
*isDuplicate = 1;
|
||||
@@ -49,35 +52,41 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
|
||||
// push the top node and push all its left nodes.
|
||||
void *nextTreeData(TreeNode *root)
|
||||
{
|
||||
StackNode *stackRoot = NULL;//Generates a new, empty stack
|
||||
stackRoot = iterateThroughTree(root, stackRoot); //Fills the stack via the helping function iterateThroughTree
|
||||
stackNode * tempBuffer = pop(stackRoot);
|
||||
clearStack(stackRoot);
|
||||
return tempBuffer;
|
||||
}
|
||||
|
||||
//Function to aid nextTreeData
|
||||
StackNode *iterateThroughTree(TreeNode *root, StackNode *stackRoot)
|
||||
{
|
||||
if(!root)
|
||||
if(root)
|
||||
{
|
||||
return stackRoot;
|
||||
stackRoot = addTreeToStack(stackRoot, root);
|
||||
}
|
||||
stackRoot = iterateThroughTree(root->left, stackRoot);
|
||||
stackNode = push(stackNode, root->data);
|
||||
stackRoot = iterateThroughTree(root->right, stackRoot);
|
||||
return stackRoot;
|
||||
void *stackBuffer = top(stackRoot);
|
||||
stackRoot = pop(stackRoot);
|
||||
//void *buffer = (stackBuffer->stackData);
|
||||
//printf("neuer Eintrag wurde geholt: %x\n", buffer);
|
||||
//return buffer;
|
||||
return stackBuffer;
|
||||
}
|
||||
// Adds all Treenode-Datas of a root into a stack ordered from left to right
|
||||
StackNode *addTreeToStack(StackNode *start, TreeNode *root)
|
||||
{
|
||||
if(root->left)
|
||||
{
|
||||
start = addTreeToStack(start, root->left);
|
||||
}
|
||||
start = push(start, root->data);
|
||||
if(root->right)
|
||||
{
|
||||
start = addTreeToStack(start, root->right);
|
||||
}
|
||||
return start;
|
||||
}
|
||||
|
||||
// Releases all memory resources (including data copies).
|
||||
void clearTree(TreeNode *root)
|
||||
{
|
||||
if(left)
|
||||
clearTree(left);
|
||||
if(right)
|
||||
clearTree(right);
|
||||
free(data);
|
||||
data = NULL;
|
||||
if(root->left)
|
||||
clearTree(root->left);
|
||||
if(root->right)
|
||||
clearTree(root->right);
|
||||
free(root->data);
|
||||
root->data = NULL;
|
||||
free(root);
|
||||
root = NULL;
|
||||
}
|
||||
@@ -88,10 +97,10 @@ unsigned int treeSize(const TreeNode *root)
|
||||
if(root)
|
||||
{
|
||||
unsigned int tempSize = 0;
|
||||
if(left)
|
||||
tempSize += treeSize(left);
|
||||
if(right)
|
||||
tempSize += treeSize(right);
|
||||
if(root->left)
|
||||
tempSize += treeSize(root->left);
|
||||
if(root->right)
|
||||
tempSize += treeSize(root->right);
|
||||
return tempSize + 1;
|
||||
}
|
||||
return 0;
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
#ifndef BINTREE_H
|
||||
#define BINTREE_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "stack.h"
|
||||
|
||||
typedef int (*CompareFctType)(const void *arg1, const void *arg2);
|
||||
|
||||
@@ -23,6 +25,6 @@ void *nextTreeData(TreeNode *root);
|
||||
void clearTree(TreeNode *root);
|
||||
// Returns the number of entries in the tree given by root.
|
||||
unsigned int treeSize(const TreeNode *root);
|
||||
//Function to aid nextTreeData
|
||||
StackNode iterateThroughTree(TreeNode *root, StackNode stackRoot);
|
||||
// Adds all Treenode-Datas of a root into a stack ordered from left to right
|
||||
StackNode *addTreeToStack(StackNode *start, TreeNode *root);
|
||||
#endif
|
||||
+3
-2
@@ -1,2 +1,3 @@
|
||||
Lukas;4985
|
||||
player1;3999
|
||||
Player1;2996
|
||||
Player2;2988
|
||||
Player3;2939
|
||||
@@ -77,7 +77,6 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
else
|
||||
printf("Leider ist %u nicht korrekt. Richtig waere %u gewesen.\n", userInput, duplicate);
|
||||
|
||||
loadHighscores(highscorePath);
|
||||
showHighscores();
|
||||
saveHighscores(highscorePath);
|
||||
@@ -85,6 +84,5 @@ int main(int argc, char *argv[])
|
||||
|
||||
exitCode = EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
return exitCode;
|
||||
}
|
||||
@@ -39,8 +39,8 @@ stackTests: stack.o test_stack.c $(unityfolder)/unity.c
|
||||
$(CC) $(CFLAGS) -I$(unityfolder) -o runStackTests test_stack.c stack.o $(unityfolder)/unity.c
|
||||
|
||||
echo "needs to be implemented"
|
||||
numbersTests: numbers.o numbersTests.c $(unityfolder)/unity.c
|
||||
$(CC) $(CFLAGS) -I$(unityfolder) -o runNumbersTests numbersTests.c numbers.o $(unityfolder)/unity.c
|
||||
numbersTests: numbers.o numbersTests.c bintree.c bintree.h stack.o $(unityfolder)/unity.c
|
||||
$(CC) $(CFLAGS) -I$(unityfolder) -o runNumbersTests numbersTests.c numbers.o bintree.o stack.o $(unityfolder)/unity.c
|
||||
|
||||
# --------------------------
|
||||
# Clean
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Duplizieren eines zufälligen Eintrags im Array.
|
||||
* in `getDuplicate()`: Sortieren des Arrays und Erkennen der doppelten Zahl durch Vergleich benachbarter Elemente. */
|
||||
|
||||
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
|
||||
//1 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.
|
||||
unsigned int *createNumbers(unsigned int len){
|
||||
@@ -51,7 +51,7 @@ unsigned int *createNumbers(unsigned int len){
|
||||
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)
|
||||
{
|
||||
@@ -103,3 +103,30 @@ unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
||||
return duplicate;
|
||||
|
||||
}
|
||||
*/
|
||||
unsigned int getDuplicate(const unsigned int *numbers, unsigned int len)
|
||||
{
|
||||
TreeNode *root = NULL;
|
||||
int buffer = 0;
|
||||
int counter = 0;
|
||||
for(int i = 0; i < len; i++)
|
||||
{
|
||||
int isDuplicat = 0;
|
||||
root = addToTree(root, numbers + i, sizeof(unsigned int), checkIfBigger, &isDuplicat);
|
||||
if(isDuplicat)
|
||||
{
|
||||
buffer = *(numbers + i);
|
||||
counter += 1;
|
||||
}
|
||||
}
|
||||
clearTree(root);
|
||||
if(counter == 1)
|
||||
return buffer;
|
||||
return 0;
|
||||
}
|
||||
|
||||
//Just a compare function needed for getDuplicate
|
||||
int checkIfBigger(const void *char1, const void *char2)
|
||||
{
|
||||
return *((int *)char1) - *((int *)char2);
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef NUMBERS_H
|
||||
#define NUMBERS_H
|
||||
|
||||
#include "bintree.h"
|
||||
|
||||
// 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.
|
||||
@@ -9,4 +11,6 @@ unsigned int *createNumbers(unsigned int len);
|
||||
// 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);
|
||||
|
||||
//Just a compare function needed for getDuplicate
|
||||
int checkIfBigger(const void *char1, const void *char2);
|
||||
#endif
|
||||
@@ -42,6 +42,7 @@ void test_getDouplicateReturnsZeroWhenThereAreMultipleDuplicates(void){
|
||||
void test_CreateNumbersCreatesDuplicatein2LenArray(void){
|
||||
unsigned int *testArray = createNumbers(2);
|
||||
unsigned int testValue = getDuplicate(testArray, 2);
|
||||
printf("\nTestvalue: %d\n", testValue);
|
||||
|
||||
if(!testValue)
|
||||
testValue = 0;
|
||||
|
||||
Binary file not shown.
Reference in new issue
Block a user