generated from freudenreichan/info2Praktikum-DobleSpiel
Compare commits
16
Commits
294d7d393e
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f5d91286ce | ||
|
|
a7e328ae5c | ||
|
|
027a8eaeb7 | ||
|
|
fdfa3c5769 | ||
|
|
85b05133ea | ||
|
|
53f4716354 | ||
|
|
b7249a9c1c | ||
|
|
cbafb5f0f2 | ||
|
|
c7ff696b67 | ||
|
|
288385a220 | ||
|
|
bf4c41b897 | ||
|
|
c663833b6e | ||
|
|
036158fc59 | ||
|
|
0bc9263b60 | ||
|
|
94e489cfae | ||
|
|
6671a3aacc |
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 @@
|
|||||||
player_name;18787
|
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;
|
||||||
}
|
}
|
||||||
@@ -35,10 +35,12 @@ $(program_obj_filesobj_files): %.o: %.c
|
|||||||
# --------------------------
|
# --------------------------
|
||||||
# Unit Tests
|
# Unit Tests
|
||||||
# --------------------------
|
# --------------------------
|
||||||
unitTests:
|
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"
|
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)
|
||||||
{
|
{
|
||||||
@@ -103,3 +103,30 @@ 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:
|
||||||
@@ -10,6 +11,22 @@
|
|||||||
// 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)) { // check if stack is empty
|
||||||
|
stack = malloc(sizeof(StackNode));
|
||||||
|
if(!stack)
|
||||||
|
return NULL;
|
||||||
|
stack->stackData = data;
|
||||||
|
stack->below = NULL;
|
||||||
|
return stack;
|
||||||
|
}
|
||||||
|
|
||||||
|
StackNode* newStack = malloc(sizeof(StackNode));
|
||||||
|
if(!newStack)
|
||||||
|
return stack;
|
||||||
|
newStack->below = stack;
|
||||||
|
newStack->stackData = data;
|
||||||
|
stack = newStack;
|
||||||
|
return stack;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -17,17 +34,34 @@ StackNode *push(StackNode *stack, void *data)
|
|||||||
// freed by caller.)
|
// freed by caller.)
|
||||||
StackNode *pop(StackNode *stack)
|
StackNode *pop(StackNode *stack)
|
||||||
{
|
{
|
||||||
|
if(stack) {
|
||||||
|
StackNode* temp = stack;
|
||||||
|
stack = stack->below;
|
||||||
|
temp->stackData = NULL;
|
||||||
|
free(temp);
|
||||||
|
temp = NULL;
|
||||||
|
return stack;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the data of the top element.
|
// Returns the data of the top element.
|
||||||
void *top(StackNode *stack)
|
void *top(StackNode *stack)
|
||||||
{
|
{
|
||||||
|
if(stack)
|
||||||
|
return stack->stackData;
|
||||||
|
else
|
||||||
|
return NULL;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clears stack and releases all memory.
|
// Clears stack and releases all memory.
|
||||||
void clearStack(StackNode *stack)
|
void clearStack(StackNode *stack)
|
||||||
{
|
{
|
||||||
|
while(stack)
|
||||||
|
stack = pop(stack);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -9,6 +9,11 @@ The latest element is taken from the stack. */
|
|||||||
|
|
||||||
//TODO: passenden Datentyp als struct anlegen
|
//TODO: passenden Datentyp als struct anlegen
|
||||||
|
|
||||||
|
typedef struct StackNode {
|
||||||
|
struct StackNode* below;
|
||||||
|
void* stackData;
|
||||||
|
} StackNode;
|
||||||
|
|
||||||
// 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);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,97 @@
|
|||||||
|
#include "stack.h"
|
||||||
|
#include "unity.h"
|
||||||
|
|
||||||
|
|
||||||
|
void test_firstNodeAddedCorrectly(void) {
|
||||||
|
printf("\nStarting first test...\n");
|
||||||
|
StackNode* modelStack = malloc(sizeof(StackNode));
|
||||||
|
int modelData = 5;
|
||||||
|
modelStack->below = NULL;
|
||||||
|
modelStack->stackData = &modelData;
|
||||||
|
|
||||||
|
StackNode* testStack = NULL;
|
||||||
|
int testData = 5;
|
||||||
|
testStack = push(testStack, &testData);
|
||||||
|
TEST_ASSERT_EQUAL_INT(*(int*)modelStack->stackData, *(int*)testStack->stackData);
|
||||||
|
TEST_ASSERT_NULL(testStack->below);
|
||||||
|
clearStack(modelStack);
|
||||||
|
clearStack(testStack);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_topReturnsCorrectValues(void) {
|
||||||
|
printf("Starting second test...\n");
|
||||||
|
StackNode* testStack = NULL;
|
||||||
|
int data1 = 1;
|
||||||
|
testStack = push(testStack, &data1);
|
||||||
|
int* returnData1 = (int*) top(testStack);
|
||||||
|
int data2 = 0;
|
||||||
|
testStack = push(testStack, &data2);
|
||||||
|
int* returnData2 = (int*) top(testStack);
|
||||||
|
int data3 = 3;
|
||||||
|
testStack = push(testStack, &data3);
|
||||||
|
int* returnData3 = (int*) top(testStack);
|
||||||
|
|
||||||
|
size_t counter = 0;
|
||||||
|
if(*returnData1 == 1)
|
||||||
|
counter++;
|
||||||
|
if(*returnData2 == 0)
|
||||||
|
counter++;
|
||||||
|
if(*returnData3 == 3)
|
||||||
|
counter++;
|
||||||
|
TEST_ASSERT_EQUAL_INT(3, counter);
|
||||||
|
clearStack(testStack);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_popRemovesCorrectly(void) {
|
||||||
|
printf("Starting third test...\n");
|
||||||
|
StackNode* testStack = NULL;
|
||||||
|
int data1 = 1;
|
||||||
|
testStack = push(testStack, &data1);
|
||||||
|
int data2 = 0;
|
||||||
|
testStack = push(testStack, &data2);
|
||||||
|
int data3 = 3;
|
||||||
|
testStack = push(testStack, &data3);
|
||||||
|
int data4 = 9;
|
||||||
|
testStack = push(testStack, &data4);
|
||||||
|
|
||||||
|
int* returnData1 = (int*) top(testStack);
|
||||||
|
testStack = pop(testStack);
|
||||||
|
int* returnData2 = (int*) top(testStack);
|
||||||
|
testStack = pop(testStack);
|
||||||
|
int* returnData3 = (int*) top(testStack);
|
||||||
|
testStack = pop(testStack);
|
||||||
|
int* returnData4 = (int*) top(testStack);
|
||||||
|
testStack = pop(testStack);
|
||||||
|
|
||||||
|
size_t counter = 0;
|
||||||
|
if(*returnData1 == 9)
|
||||||
|
counter++;
|
||||||
|
if(*returnData2 == 3)
|
||||||
|
counter++;
|
||||||
|
if(*returnData3 == 0)
|
||||||
|
counter++;
|
||||||
|
if(*returnData4 == 1)
|
||||||
|
counter++;
|
||||||
|
TEST_ASSERT_EQUAL_INT(4, counter);
|
||||||
|
clearStack(testStack);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setUp(void) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void tearDown(void) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
|
||||||
|
UNITY_BEGIN();
|
||||||
|
|
||||||
|
printf("\n----------------------------Stack-Tests----------------------------\n");
|
||||||
|
RUN_TEST(test_firstNodeAddedCorrectly);
|
||||||
|
RUN_TEST(test_topReturnsCorrectValues);
|
||||||
|
RUN_TEST(test_popRemovesCorrectly);
|
||||||
|
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
Reference in new issue
Block a user