numbers.c bearbeitet
This commit is contained in:
parent
1bd8475a96
commit
2689130b55
93
numbers.c
93
numbers.c
@ -14,13 +14,106 @@
|
|||||||
// 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 int compareUInt(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
unsigned int A = *(unsigned int*)a;
|
||||||
|
unsigned int B = *(unsigned int*)b;
|
||||||
|
if (A < B) return -1;
|
||||||
|
if (A > B) return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
// Sortiervergleich für qsort
|
||||||
|
int compareQsort(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
unsigned int A = *(const unsigned int*)a;
|
||||||
|
unsigned int B = *(const unsigned int*)b;
|
||||||
|
if (A < B) return -1;
|
||||||
|
if (A > B) return +1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
unsigned int *createNumbers(unsigned int len)
|
unsigned int *createNumbers(unsigned int len)
|
||||||
{
|
{
|
||||||
|
if (len < 2) return NULL;
|
||||||
|
|
||||||
|
srand((unsigned int)time(NULL));
|
||||||
|
|
||||||
|
// Speicher für das Array
|
||||||
|
unsigned int *numbers = malloc(sizeof(unsigned int) * len);
|
||||||
|
if (!numbers) return NULL;
|
||||||
|
|
||||||
|
TreeNode *root = NULL; // Baumwurzel
|
||||||
|
unsigned int value;
|
||||||
|
int isDuplicate;
|
||||||
|
|
||||||
|
//Array mit eindeutigen Zufallszahlen füllen
|
||||||
|
for (unsigned int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
value = (rand() % (2 * len)) + 1; // Zufallszahl 1..2*len
|
||||||
|
isDuplicate = 0;
|
||||||
|
|
||||||
|
TreeNode *newRoot = addToTree(
|
||||||
|
root,
|
||||||
|
&value,
|
||||||
|
sizeof(unsigned int),
|
||||||
|
compareUInt,
|
||||||
|
&isDuplicate
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!isDuplicate)
|
||||||
|
{
|
||||||
|
// Neue Zahl - akzeptieren
|
||||||
|
root = newRoot;
|
||||||
|
numbers[i] = value;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// Sonst neue Zahl generieren
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//genau eine Zufallszahl duplizieren
|
||||||
|
unsigned int idx1 = rand() % len;
|
||||||
|
unsigned int idx2 = rand() % len;
|
||||||
|
|
||||||
|
while (idx2 == idx1)
|
||||||
|
idx2 = rand() % len;
|
||||||
|
|
||||||
|
numbers[idx2] = numbers[idx1];
|
||||||
|
|
||||||
|
// Baum wieder freigeben
|
||||||
|
clearTree(root);
|
||||||
|
|
||||||
|
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)
|
||||||
{
|
{
|
||||||
|
if (!numbers || len < 2) return 0;
|
||||||
|
|
||||||
|
unsigned int *copy = malloc(len * sizeof(unsigned int));
|
||||||
|
if (!copy) return 0;
|
||||||
|
|
||||||
|
// Array kopieren
|
||||||
|
memcpy(copy, numbers, len * sizeof(unsigned int));
|
||||||
|
|
||||||
|
// Sortieren
|
||||||
|
qsort(copy, len, sizeof(unsigned int), compareQsort);
|
||||||
|
|
||||||
|
// Doppelte Zahl finden
|
||||||
|
unsigned int duplicate = 0;
|
||||||
|
for (unsigned int i = 0; i < len - 1; i++)
|
||||||
|
{
|
||||||
|
if (copy[i] == copy[i + 1])
|
||||||
|
{
|
||||||
|
duplicate = copy[i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(copy);
|
||||||
|
return duplicate;
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user