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 <string.h>
|
||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
#include "bintree.h"
|
#include "bintree.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
StackNode *stackRoot;//Generates a new, empty stack for next tree data
|
||||||
|
|
||||||
//TODO: binären Suchbaum implementieren
|
//TODO: binären Suchbaum implementieren
|
||||||
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv),
|
/* * `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)
|
if(!root)
|
||||||
{
|
{
|
||||||
root = malloc(sizeof(node));
|
//root = malloc(sizeof(TreeNode));
|
||||||
*node newNode;
|
TreeNode *newNode = malloc(sizeof(TreeNode));
|
||||||
*newNode.data = malloc(dataSize);
|
newNode->data = malloc(dataSize);
|
||||||
strcpy(*newNode.data, data);
|
newNode->left = NULL;
|
||||||
*newNode.left = NULL;
|
newNode->right = NULL;
|
||||||
*newNode.right = NULL;
|
memcpy(newNode->data, data, dataSize);
|
||||||
if(isDuplicate)
|
if(isDuplicate)
|
||||||
*isDuplicate = 0;
|
*isDuplicate = 0;
|
||||||
return newNode;
|
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
|
else
|
||||||
{
|
{
|
||||||
if(!isDuplicate)
|
if(!isDuplicate)
|
||||||
*left = addToTree(right, data, dataSize, compareFct, isDuplicate);
|
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
*isDuplicate = 1;
|
*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.
|
// push the top node and push all its left nodes.
|
||||||
void *nextTreeData(TreeNode *root)
|
void *nextTreeData(TreeNode *root)
|
||||||
{
|
{
|
||||||
StackNode *stackRoot = NULL;//Generates a new, empty stack
|
if(root)
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
return stackRoot;
|
stackRoot = addTreeToStack(stackRoot, root);
|
||||||
}
|
}
|
||||||
stackRoot = iterateThroughTree(root->left, stackRoot);
|
void *stackBuffer = top(stackRoot);
|
||||||
stackNode = push(stackNode, root->data);
|
stackRoot = pop(stackRoot);
|
||||||
stackRoot = iterateThroughTree(root->right, stackRoot);
|
//void *buffer = (stackBuffer->stackData);
|
||||||
return stackRoot;
|
//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).
|
// Releases all memory resources (including data copies).
|
||||||
void clearTree(TreeNode *root)
|
void clearTree(TreeNode *root)
|
||||||
{
|
{
|
||||||
if(left)
|
if(root->left)
|
||||||
clearTree(left);
|
clearTree(root->left);
|
||||||
if(right)
|
if(root->right)
|
||||||
clearTree(right);
|
clearTree(root->right);
|
||||||
free(data);
|
free(root->data);
|
||||||
data = NULL;
|
root->data = NULL;
|
||||||
free(root);
|
free(root);
|
||||||
root = NULL;
|
root = NULL;
|
||||||
}
|
}
|
||||||
@@ -88,10 +97,10 @@ unsigned int treeSize(const TreeNode *root)
|
|||||||
if(root)
|
if(root)
|
||||||
{
|
{
|
||||||
unsigned int tempSize = 0;
|
unsigned int tempSize = 0;
|
||||||
if(left)
|
if(root->left)
|
||||||
tempSize += treeSize(left);
|
tempSize += treeSize(root->left);
|
||||||
if(right)
|
if(root->right)
|
||||||
tempSize += treeSize(right);
|
tempSize += treeSize(root->right);
|
||||||
return tempSize + 1;
|
return tempSize + 1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
#ifndef BINTREE_H
|
#ifndef BINTREE_H
|
||||||
#define BINTREE_H
|
#define BINTREE_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include "stack.h"
|
||||||
|
|
||||||
typedef int (*CompareFctType)(const void *arg1, const void *arg2);
|
typedef int (*CompareFctType)(const void *arg1, const void *arg2);
|
||||||
|
|
||||||
@@ -23,6 +25,6 @@ void *nextTreeData(TreeNode *root);
|
|||||||
void clearTree(TreeNode *root);
|
void clearTree(TreeNode *root);
|
||||||
// Returns the number of entries in the tree given by root.
|
// Returns the number of entries in the tree given by root.
|
||||||
unsigned int treeSize(const TreeNode *root);
|
unsigned int treeSize(const TreeNode *root);
|
||||||
//Function to aid nextTreeData
|
// Adds all Treenode-Datas of a root into a stack ordered from left to right
|
||||||
StackNode iterateThroughTree(TreeNode *root, StackNode stackRoot);
|
StackNode *addTreeToStack(StackNode *start, TreeNode *root);
|
||||||
#endif
|
#endif
|
||||||
+3
-2
@@ -1,2 +1,3 @@
|
|||||||
Lukas;4985
|
Player1;2996
|
||||||
player1;3999
|
Player2;2988
|
||||||
|
Player3;2939
|
||||||
@@ -77,7 +77,6 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
printf("Leider ist %u nicht korrekt. Richtig waere %u gewesen.\n", userInput, duplicate);
|
printf("Leider ist %u nicht korrekt. Richtig waere %u gewesen.\n", userInput, duplicate);
|
||||||
|
|
||||||
loadHighscores(highscorePath);
|
loadHighscores(highscorePath);
|
||||||
showHighscores();
|
showHighscores();
|
||||||
saveHighscores(highscorePath);
|
saveHighscores(highscorePath);
|
||||||
@@ -85,6 +84,5 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
exitCode = EXIT_SUCCESS;
|
exitCode = EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
return exitCode;
|
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
|
$(CC) $(CFLAGS) -I$(unityfolder) -o runStackTests test_stack.c stack.o $(unityfolder)/unity.c
|
||||||
|
|
||||||
echo "needs to be implemented"
|
echo "needs to be implemented"
|
||||||
numbersTests: numbers.o numbersTests.c $(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 $(unityfolder)/unity.c
|
$(CC) $(CFLAGS) -I$(unityfolder) -o runNumbersTests numbersTests.c numbers.o bintree.o stack.o $(unityfolder)/unity.c
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Clean
|
# Clean
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
* Duplizieren eines zufälligen Eintrags im Array.
|
* Duplizieren eines zufälligen Eintrags im Array.
|
||||||
* in `getDuplicate()`: Sortieren des Arrays und Erkennen der doppelten Zahl durch Vergleich benachbarter Elemente. */
|
* 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
|
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
|
||||||
// creating random numbers.
|
// creating random numbers.
|
||||||
unsigned int *createNumbers(unsigned int len){
|
unsigned int *createNumbers(unsigned int len){
|
||||||
@@ -51,7 +51,7 @@ unsigned int *createNumbers(unsigned int len){
|
|||||||
return numbers;
|
return numbers;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
// Returns only the only number in numbers which is present twice. Returns zero on errors.
|
// 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)
|
||||||
{
|
{
|
||||||
@@ -102,4 +102,31 @@ unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
|||||||
|
|
||||||
return duplicate;
|
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
|
#ifndef NUMBERS_H
|
||||||
#define 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 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.
|
||||||
@@ -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.
|
// 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);
|
||||||
|
|
||||||
|
//Just a compare function needed for getDuplicate
|
||||||
|
int checkIfBigger(const void *char1, const void *char2);
|
||||||
#endif
|
#endif
|
||||||
@@ -42,6 +42,7 @@ void test_getDouplicateReturnsZeroWhenThereAreMultipleDuplicates(void){
|
|||||||
void test_CreateNumbersCreatesDuplicatein2LenArray(void){
|
void test_CreateNumbersCreatesDuplicatein2LenArray(void){
|
||||||
unsigned int *testArray = createNumbers(2);
|
unsigned int *testArray = createNumbers(2);
|
||||||
unsigned int testValue = getDuplicate(testArray, 2);
|
unsigned int testValue = getDuplicate(testArray, 2);
|
||||||
|
printf("\nTestvalue: %d\n", testValue);
|
||||||
|
|
||||||
if(!testValue)
|
if(!testValue)
|
||||||
testValue = 0;
|
testValue = 0;
|
||||||
|
|||||||
Binary file not shown.
@@ -1,4 +1,5 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
|
|
||||||
//TODO: grundlegende Stackfunktionen implementieren:
|
//TODO: grundlegende Stackfunktionen implementieren:
|
||||||
@@ -40,7 +41,9 @@ StackNode *pop(StackNode *stack)
|
|||||||
free(temp);
|
free(temp);
|
||||||
temp = NULL;
|
temp = NULL;
|
||||||
return stack;
|
return stack;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user