Compare commits

..
2 Commits
Author SHA1 Message Date
weberlu100319 94e489cfae Finished first draw of stack, next step: writing tests 2025-11-27 17:10:02 +01:00
weberlu100319 6671a3aacc Initial commit for stack 2025-11-27 16:29:29 +01:00
6 changed files with 41 additions and 155 deletions

No files matched your search

+1 -1
View File
@@ -1,2 +1,2 @@
player_name;18787 Lukas;4985
player1;3999 player1;3999
-2
View File
@@ -37,8 +37,6 @@ $(program_obj_filesobj_files): %.o: %.c
# -------------------------- # --------------------------
unitTests: unitTests:
echo "needs to be implemented" 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
# -------------------------- # --------------------------
# Clean # Clean
+2 -81
View File
@@ -14,92 +14,13 @@
// 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.
unsigned int *createNumbers(unsigned int len){ unsigned int *createNumbers(unsigned int len)
{
if(len == 1){
printf("Minimum length is 2");
return NULL;
}
srand(time(NULL));
unsigned int *numbers = malloc(len * sizeof(unsigned int)); //reserving memory for array
if(!numbers){
printf("Error allocating Memory");
return NULL;
}
for (int i = 0; i < len; i++){
numbers[i] = rand() % (2 * len) + 1; //creates randorm number between 1 and 2x len
if(getDuplicate(numbers, i+1)){ //We use i+1 to refer to current initialised array length
i--; //If there a douplicate has been created, i gets 1 lower therefore overwriting it in next iteration
}
}
//Creating Duplicate
unsigned int duplicationSuccess = 0;
while (duplicationSuccess == 0){
unsigned int duplicateInitialIndex = rand()%len; //Getting random index for creating duplicate
unsigned int duplicateReplaceIndex = rand ()%len; //Index of Number replaced by duplicateInitialIndex
if(duplicateInitialIndex == duplicateReplaceIndex)
continue; //returning process if chosen duplicate is replacing itsself
duplicationSuccess = 1;
numbers[duplicateReplaceIndex] = numbers [duplicateInitialIndex];
}
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)
{ {
unsigned int *sortedNumbers = malloc(len * sizeof(unsigned int));
if(!sortedNumbers){
printf("Error allocating memory");
return 0;
}
//Creating Copy of Numbers Array
for (int i = 0; i < len; i++){
sortedNumbers[i] = numbers[i];
}
unsigned int duplicate = 0;
unsigned int temp = 0;
unsigned int duplicateCount = 0;
//Sorting sortedNumbers
for (int i = 0; i < len; i++){
for(int j = 0; j< len - 1; j++){
if(sortedNumbers[j]> sortedNumbers[j+1]){ //Switching Elements
temp = sortedNumbers[j];
sortedNumbers[j] = sortedNumbers[j+1];
sortedNumbers[j+1] = temp;
}
}
}
//identifying duplicates
for(int i = 0; i < len; i++){
if(sortedNumbers[i] == sortedNumbers[i+1]){
duplicate = sortedNumbers[i];
duplicateCount++;
}
}
free(sortedNumbers);
if(duplicateCount != 1){
return 0;
}
return duplicate;
} }
-70
View File
@@ -1,70 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "unity.h"
#include "numbers.h"
void test_ArrayIsCreatedAndNotNull(void){
size_t len = 15;
int *testArray = createNumbers(len);
TEST_ASSERT_NOT_NULL(testArray); //If array hasnt been created at all
free(testArray);
}
void test_getDouplicateReturnsDouplicateNumber(void){
unsigned int testArray[8] = {1, 2, 3, 3, 4, 5, 6, 7};
unsigned int testValue = getDuplicate(testArray, 8);
TEST_ASSERT_EQUAL(3, testValue);
}
void test_getDouplicateReturnsZeroWhenThereIsNoDuplicate(void){
unsigned int testArray[8] = {1, 2, 3, 4, 5, 6, 7, 8};
unsigned int testValue = getDuplicate(testArray, 8);
TEST_ASSERT_EQUAL(0, testValue);
}
void test_getDouplicateReturnsZeroWhenThereAreMultipleDuplicates(void){
unsigned int testArray[8] = {1, 3, 3, 4, 5, 6, 7, 7};
unsigned int testValue = getDuplicate(testArray, 8);
TEST_ASSERT_EQUAL(0, testValue);
}
void test_CreateNumbersCreatesDuplicatein2LenArray(void){
unsigned int *testArray = createNumbers(2);
unsigned int testValue = getDuplicate(testArray, 2);
if(!testValue)
testValue = 0;
else
testValue = 1;
TEST_ASSERT_EQUAL(1, testValue);
}
void setUp(void){}
void tearDown(void){}
int main(){
UNITY_BEGIN();
printf("\n============================\nNumbers tests\n============================\n");
RUN_TEST(test_ArrayIsCreatedAndNotNull);
RUN_TEST(test_CreateNumbersCreatesDuplicatein2LenArray);
RUN_TEST(test_getDouplicateReturnsDouplicateNumber);
RUN_TEST(test_getDouplicateReturnsZeroWhenThereIsNoDuplicate);
RUN_TEST(test_getDouplicateReturnsZeroWhenThereAreMultipleDuplicates);
return UNITY_END();
}
+33 -1
View File
@@ -10,6 +10,18 @@
// 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));
stack->stackData = data;
stack->below = NULL;
return stack;
}
StackNode* newStack = malloc(sizeof(StackNode));
newStack->below = stack;
newStack->stackData = data;
stack = newStack;
return stack;
} }
@@ -17,17 +29,37 @@ 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;
free(temp->data);
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)
{ {
if(stack) {
do {
stack = pop(stack);
} while (stack->below);
pop(stack);
}
} }
+5
View File
@@ -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 {
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);