forked from freudenreichan/info2Praktikum-DobleSpiel
comitting number.c
This commit is contained in:
1 parent
f8ae524374
commit
2cbe4c839c
3 files changed
+45
-42
No files matched your search
@@ -1,2 +1,5 @@
|
|||||||
|
Lukas;5999
|
||||||
|
Lukas;4999
|
||||||
Tobi;4999
|
Tobi;4999
|
||||||
|
Lukas;3999
|
||||||
player1;3999
|
player1;3999
|
||||||
@@ -35,6 +35,12 @@ $(program_obj_files): %.o: %.c
|
|||||||
# --------------------------
|
# --------------------------
|
||||||
# Unit Tests
|
# Unit Tests
|
||||||
# --------------------------
|
# --------------------------
|
||||||
|
unitTests: numbersTests
|
||||||
|
./numbersTests
|
||||||
|
|
||||||
|
numbersTests: numbersTests.c numbers.o bintree.o stack.o $(unityfolder)/unity.c
|
||||||
|
$(CC) $(FLAGS) -I$(unityfolder) $^ -o numbersTests
|
||||||
|
|
||||||
bintreeTests: bintree.o test_bintree.c stack.o $(unityfolder)/unity.c
|
bintreeTests: bintree.o test_bintree.c stack.o $(unityfolder)/unity.c
|
||||||
$(CC) $(CFLAGS) -I$(unityfolder) -o runBintreeTests test_bintree.c bintree.o stack.o $(unityfolder)/unity.c
|
$(CC) $(CFLAGS) -I$(unityfolder) -o runBintreeTests test_bintree.c bintree.o stack.o $(unityfolder)/unity.c
|
||||||
|
|
||||||
|
|||||||
+36
-42
@@ -14,75 +14,69 @@
|
|||||||
// 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.
|
||||||
static void fillarray(unsigned int array[], unsigned int len) {
|
|
||||||
//nur in create numbers wegen srand
|
|
||||||
for (int i = 0; i < len; i++) {
|
|
||||||
array[i] = rand() % (len * 2 - 1) + 1; // nochmal prüfen ob passt
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static unsigned int getrandomnumber(unsigned int array[], unsigned int len) {
|
static int compare(const void *a, const void *b);
|
||||||
unsigned int number = rand() % (len * 2 - 1) + 1;
|
|
||||||
for (int i = 0; i < len; i++) {
|
|
||||||
if (array[i] == number) {
|
|
||||||
number = rand() % (len * 2 - 1) + 1;
|
|
||||||
i = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return number;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int *createNumbers(unsigned int len) {
|
unsigned int *createNumbers(unsigned int len) {
|
||||||
srand(time(NULL));
|
if (len < 3
|
||||||
unsigned int *numArray = malloc(len * sizeof(unsigned int)); // TODO: schauen wann free machbar
|
) {
|
||||||
if (numArray == NULL) {
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
// deletes duplicates and fills in with random number
|
|
||||||
fillarray(numArray, len);
|
srand(time(NULL));
|
||||||
for (unsigned int i = 0; i < --len; i++) {
|
int duplicate = 0;
|
||||||
for (unsigned int j = i; j < len; j++) {
|
TreeNode *tree = NULL;
|
||||||
if (numArray[i] == numArray[j]) {
|
unsigned int *numArray = malloc(len * sizeof(unsigned int));
|
||||||
numArray[j] = getrandomnumber(numArray, len);
|
|
||||||
}
|
for (unsigned int i = 0; i < len; i++) {
|
||||||
|
unsigned int randNumber = rand() %(len * 2) + 1;
|
||||||
|
numArray[i] = randNumber;
|
||||||
|
tree = addToTree(tree, &randNumber, sizeof(randNumber), compare, &duplicate);
|
||||||
|
if (duplicate == 1) {
|
||||||
|
i--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// create duplicate
|
// create duplicate
|
||||||
unsigned int randNum1 = rand() % (len * 2 - 1) + 1;
|
unsigned int randNum1 = rand() % (len);
|
||||||
unsigned int randNum2 = rand() % (len * 2 - 1) + 1;
|
unsigned int randNum2 = rand() % (len);
|
||||||
while (randNum1 == randNum2) {
|
while (randNum1 == randNum2) {
|
||||||
randNum2 = rand() % (len * 2 - 1) + 1;
|
randNum2 = rand() % (len);
|
||||||
}
|
}
|
||||||
numArray[randNum2] = numArray[randNum1];
|
numArray[randNum2] = numArray[randNum1];
|
||||||
|
clearTree(tree);
|
||||||
|
|
||||||
return numArray;
|
return numArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 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) {
|
||||||
if (numbers == NULL) {
|
if (numbers == NULL || len == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
unsigned int duplicate = 0;
|
unsigned int *array = calloc(len, sizeof(unsigned int));
|
||||||
int compare(const void *a, const void *b);
|
if (array == NULL) {
|
||||||
int (*compareNumbers)(const void *a, const void *b) = &compare;
|
return 0;
|
||||||
qsort(numbers, len, sizeof(unsigned int), compareNumbers);
|
}
|
||||||
|
memcpy(array ,numbers, sizeof(unsigned int) * len);
|
||||||
|
|
||||||
for (unsigned int i = 0; i < len; i++) {
|
unsigned int duplicate = 0;
|
||||||
if (numbers[i] == numbers[i + 1] && duplicate != 0) {
|
int (*compareNumbers)(const void *a, const void *b) = &compare;
|
||||||
|
qsort(array, len, sizeof(unsigned int), compareNumbers);
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < len-1; i++) {
|
||||||
|
if (array[i] == array[i + 1] && duplicate != 0) {
|
||||||
|
free(array);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (numbers[i] == numbers[i + 1] && duplicate == 0) {
|
if (array[i] == array[i + 1] && duplicate == 0) {
|
||||||
duplicate = numbers[i];
|
duplicate = array[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (duplicate == 0) {
|
if (duplicate == 0) {
|
||||||
|
free(array);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
free(array);
|
||||||
return duplicate;
|
return duplicate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user