forked from freudenreichan/info2Praktikum-DobleSpiel
Merge remote-tracking branch 'origin/main'
This commit is contained in:
commit
b3c2a74fc6
5 files changed
+90
-46
No files matched your search
@@ -1,3 +1,5 @@
|
|||||||
|
Lukas;19999
|
||||||
|
Lukas;7999
|
||||||
Lukas;5999
|
Lukas;5999
|
||||||
Henrik;5988
|
Henrik;5988
|
||||||
Lukas;4999
|
Lukas;4999
|
||||||
|
|||||||
+12
-21
@@ -5,31 +5,20 @@
|
|||||||
#include "numbers.h"
|
#include "numbers.h"
|
||||||
#include "bintree.h"
|
#include "bintree.h"
|
||||||
|
|
||||||
//TODO: getDuplicate und createNumbers implementieren
|
|
||||||
/* * * Erzeugen eines Arrays mit der vom Nutzer eingegebenen Anzahl an Zufallszahlen.
|
|
||||||
* Sicherstellen, dass beim Befüllen keine Duplikate entstehen.
|
|
||||||
* 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.
|
|
||||||
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
|
|
||||||
// creating random numbers.
|
|
||||||
|
|
||||||
static int compare(const void *a, const void *b);
|
static int compare(const void *a, const void *b);
|
||||||
|
|
||||||
unsigned int *createNumbers(unsigned int len) {
|
unsigned int *createNumbers(unsigned int len) {
|
||||||
if (len < 3
|
if (len < 3) {
|
||||||
) {
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
int duplicate = 0;
|
|
||||||
TreeNode *tree = NULL;
|
TreeNode *tree = NULL;
|
||||||
unsigned int *numArray = malloc(len * sizeof(unsigned int));
|
unsigned int *numArray = calloc(sizeof(unsigned int), len * sizeof(unsigned int));
|
||||||
|
|
||||||
for (unsigned int i = 0; i < len; i++) {
|
for (unsigned int i = 0; i < len; i++) {
|
||||||
unsigned int randNumber = rand() %(len * 2) + 1;
|
int duplicate = 0;
|
||||||
|
unsigned int randNumber = rand() % (len * 2) + 1;
|
||||||
numArray[i] = randNumber;
|
numArray[i] = randNumber;
|
||||||
tree = addToTree(tree, &randNumber, sizeof(randNumber), compare, &duplicate);
|
tree = addToTree(tree, &randNumber, sizeof(randNumber), compare, &duplicate);
|
||||||
if (duplicate == 1) {
|
if (duplicate == 1) {
|
||||||
@@ -37,7 +26,7 @@ unsigned int *createNumbers(unsigned int len) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// create duplicate
|
// create duplicate
|
||||||
unsigned int randNum1 = rand() % (len);
|
const unsigned int randNum1 = rand() % (len);
|
||||||
unsigned int randNum2 = rand() % (len);
|
unsigned int randNum2 = rand() % (len);
|
||||||
while (randNum1 == randNum2) {
|
while (randNum1 == randNum2) {
|
||||||
randNum2 = rand() % (len);
|
randNum2 = rand() % (len);
|
||||||
@@ -49,7 +38,7 @@ 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[],const unsigned int len) {
|
||||||
if (numbers == NULL || len == 0) {
|
if (numbers == NULL || len == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -57,21 +46,23 @@ unsigned int getDuplicate(const unsigned int numbers[], unsigned int len) {
|
|||||||
if (array == NULL) {
|
if (array == NULL) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
memcpy(array ,numbers, sizeof(unsigned int) * len);
|
memcpy(array, numbers, sizeof(unsigned int) * len);
|
||||||
|
|
||||||
unsigned int duplicate = 0;
|
unsigned int duplicate = 0;
|
||||||
int (*compareNumbers)(const void *a, const void *b) = &compare;
|
int (*compareNumbers)(const void *a, const void *b) = &compare;
|
||||||
qsort(array, len, sizeof(unsigned int), compareNumbers);
|
qsort(array, len, sizeof(unsigned int), compareNumbers);
|
||||||
|
for (unsigned int i = 0; i < len - 1; i++) {
|
||||||
for (unsigned int i = 0; i < len-1; i++) {
|
// returns 0 if multiple duplicates
|
||||||
if (array[i] == array[i + 1] && duplicate != 0) {
|
if (array[i] == array[i + 1] && duplicate != 0) {
|
||||||
free(array);
|
free(array);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (array[i] == array[i + 1] && duplicate == 0) {
|
if (array[i] == array[i + 1] && duplicate == 0) {
|
||||||
duplicate = array[i];
|
duplicate = array[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//returns 0 if no duplicate
|
||||||
if (duplicate == 0) {
|
if (duplicate == 0) {
|
||||||
free(array);
|
free(array);
|
||||||
return 0;
|
return 0;
|
||||||
@@ -83,6 +74,6 @@ unsigned int getDuplicate(const unsigned int numbers[], unsigned int len) {
|
|||||||
int compare(const void *a, const void *b) {
|
int compare(const void *a, const void *b) {
|
||||||
unsigned int x = *((unsigned int *) a);
|
unsigned int x = *((unsigned int *) a);
|
||||||
unsigned int y = *((unsigned int *) b);
|
unsigned int y = *((unsigned int *) b);
|
||||||
int comp = x - y;
|
const int comp = x - y;
|
||||||
return comp;
|
return comp;
|
||||||
}
|
}
|
||||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,74 @@
|
|||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "unity.h"
|
||||||
|
#include "numbers.h"
|
||||||
|
|
||||||
|
void setUp(void) {}
|
||||||
|
void tearDown(void) {}
|
||||||
|
|
||||||
|
|
||||||
|
static void test_arrayGetsNumbers() {
|
||||||
|
unsigned int returnValue = 0;
|
||||||
|
unsigned int* array = createNumbers(5);
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
returnValue += array[i];
|
||||||
|
}
|
||||||
|
int expectedValue = 10;// minimal Value when len is 5 => 11
|
||||||
|
TEST_ASSERT_GREATER_THAN_INT(expectedValue,returnValue);
|
||||||
|
free (array);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_arrayOnlyHasOneDulicate() {
|
||||||
|
unsigned int len = 5;
|
||||||
|
unsigned int *array = createNumbers(len);
|
||||||
|
int duplicateTrue = getDuplicate(array, len);
|
||||||
|
int expectedValue = 0;
|
||||||
|
free(array);
|
||||||
|
TEST_ASSERT_NOT_EQUAL_INT(expectedValue,duplicateTrue);
|
||||||
|
}
|
||||||
|
static void test_returnNullWhenLenIs0() {
|
||||||
|
unsigned int len = 0;
|
||||||
|
unsigned int *array = createNumbers(len);
|
||||||
|
free(array);
|
||||||
|
TEST_ASSERT_NULL(array);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_findsDuplicate() {
|
||||||
|
unsigned int array[] = {5, 10, 9, 3, 3};
|
||||||
|
unsigned int len = 5;
|
||||||
|
unsigned int returnValue = getDuplicate(array, len);
|
||||||
|
unsigned int expectedValue= 3;
|
||||||
|
TEST_ASSERT_EQUAL_INT(expectedValue,returnValue);
|
||||||
|
}
|
||||||
|
static void test_return0WhenNoDuplicate() {
|
||||||
|
unsigned int array[] = {5, 10, 9, 3, 2};
|
||||||
|
unsigned int len = 5;
|
||||||
|
unsigned int returnValue = getDuplicate(array, len);
|
||||||
|
unsigned int expectedValue = 0;
|
||||||
|
TEST_ASSERT_EQUAL_INT(expectedValue, returnValue);
|
||||||
|
}
|
||||||
|
static void test_return0WhenMultipleDuplicates() {
|
||||||
|
unsigned int array[] = {5, 5, 9, 3, 3};
|
||||||
|
unsigned int len = 5;
|
||||||
|
unsigned int returnValue = getDuplicate(array, len);
|
||||||
|
unsigned int expectedValue = 0;
|
||||||
|
TEST_ASSERT_EQUAL_INT(expectedValue, returnValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
UNITY_BEGIN();
|
||||||
|
|
||||||
|
printf("\n============================\nnumber tests\n============================\n");
|
||||||
|
RUN_TEST(test_arrayGetsNumbers);
|
||||||
|
RUN_TEST(test_arrayOnlyHasOneDulicate);
|
||||||
|
RUN_TEST(test_returnNullWhenLenIs0);
|
||||||
|
RUN_TEST(test_findsDuplicate);
|
||||||
|
RUN_TEST(test_return0WhenNoDuplicate);
|
||||||
|
RUN_TEST(test_return0WhenMultipleDuplicates);
|
||||||
|
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
|
|
||||||
+2
-25
@@ -1,7 +1,7 @@
|
|||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
|
|
||||||
#if __APPLE__
|
#include <time.h>
|
||||||
#include <sys/time.h>
|
|
||||||
static struct timespec start = {0, 0};
|
static struct timespec start = {0, 0};
|
||||||
|
|
||||||
// Starts the timer.
|
// Starts the timer.
|
||||||
@@ -30,26 +30,3 @@ double stopTimer()
|
|||||||
|
|
||||||
return measuredSeconds;
|
return measuredSeconds;
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
#include <time.h>
|
|
||||||
static clock_t startClocks = 0;
|
|
||||||
|
|
||||||
// Starts the timer.
|
|
||||||
void startTimer()
|
|
||||||
{
|
|
||||||
startClocks = clock();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns the time in seconds since startTimer() was called.
|
|
||||||
double stopTimer()
|
|
||||||
{
|
|
||||||
double measuredSeconds = (clock() - (double)startClocks) / CLOCKS_PER_SEC;
|
|
||||||
|
|
||||||
if(startClocks > 0)
|
|
||||||
startClocks = 0;
|
|
||||||
else
|
|
||||||
measuredSeconds = -1;
|
|
||||||
|
|
||||||
return measuredSeconds;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
Reference in new issue
Block a user