generated from freudenreichan/info2Praktikum-DobleSpiel
133 lines
3.7 KiB
C
133 lines
3.7 KiB
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <time.h>
|
|
#include <string.h>
|
|
#include "numbers.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.
|
|
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <time.h>
|
|
#include <string.h>
|
|
#include "numbers.h"
|
|
#include "bintree.h"
|
|
|
|
// Vergleichsfunktion für unsigned int (für Baum)
|
|
static int compareUnsignedInt(const void *a, const void *b)
|
|
{
|
|
unsigned int ua = *(const unsigned int *)a;
|
|
unsigned int ub = *(const unsigned int *)b;
|
|
|
|
if (ua < ub) return -1;
|
|
if (ua > ub) return 1;
|
|
return 0;
|
|
}
|
|
|
|
// Vergleichsfunktion für qsort (unsigned int)
|
|
static int compareUnsignedIntQsort(const void *a, const void *b)
|
|
{
|
|
unsigned int ua = *(const unsigned int *)a;
|
|
unsigned int ub = *(const unsigned int *)b;
|
|
|
|
if (ua < ub) return -1;
|
|
if (ua > ub) return 1;
|
|
return 0;
|
|
}
|
|
|
|
// 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.
|
|
unsigned int *createNumbers(unsigned int len)
|
|
{
|
|
if (len < 2)
|
|
return NULL;
|
|
|
|
unsigned int *numbers = (unsigned int *)malloc(len * sizeof(unsigned int));
|
|
if (numbers == NULL)
|
|
return NULL;
|
|
|
|
static int seeded = 0;
|
|
if (!seeded)
|
|
{
|
|
srand((unsigned int)time(NULL));
|
|
seeded = 1;
|
|
}
|
|
|
|
TreeNode *root = NULL;
|
|
|
|
// Zuerst len verschiedene Zufallszahlen erzeugen
|
|
for (unsigned int i = 0; i < len; i++)
|
|
{
|
|
int isDuplicate = 0;
|
|
unsigned int value;
|
|
|
|
do
|
|
{
|
|
value = (unsigned int)(rand() % (2 * len)) + 1; // [1, 2*len]
|
|
isDuplicate = 0;
|
|
root = addToTree(root, &value, sizeof(value), compareUnsignedInt, &isDuplicate);
|
|
}
|
|
while (isDuplicate != 0); // so lange wiederholen, bis eine neue Zahl eingefügt wurde
|
|
|
|
numbers[i] = value;
|
|
}
|
|
|
|
// Jetzt genau EINEN Eintrag duplizieren: zwei verschiedene Indizes wählen
|
|
if (len >= 2)
|
|
{
|
|
unsigned int idx1 = (unsigned int)(rand() % len);
|
|
unsigned int idx2 = (unsigned int)(rand() % len);
|
|
while (idx2 == idx1)
|
|
idx2 = (unsigned int)(rand() % len);
|
|
|
|
numbers[idx2] = numbers[idx1];
|
|
}
|
|
|
|
clearTree(root);
|
|
return numbers;
|
|
}
|
|
|
|
// 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)
|
|
{
|
|
if (numbers == NULL || len < 2)
|
|
return 0;
|
|
|
|
unsigned int *copy = (unsigned int *)malloc(len * sizeof(unsigned int));
|
|
if (copy == NULL)
|
|
return 0;
|
|
|
|
memcpy(copy, numbers, len * sizeof(unsigned int));
|
|
|
|
qsort(copy, len, sizeof(unsigned int), compareUnsignedIntQsort);
|
|
|
|
unsigned int duplicate = 0;
|
|
int found = 0;
|
|
|
|
for (unsigned int i = 1; i < len; i++)
|
|
{
|
|
if (copy[i] == copy[i - 1])
|
|
{
|
|
duplicate = copy[i];
|
|
found = 1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
free(copy);
|
|
|
|
if (!found)
|
|
return 0;
|
|
|
|
return duplicate;
|
|
} |