first running version
This commit is contained in:
parent
3cc67f0343
commit
38e5ff6a0a
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,5 +1,6 @@
|
|||||||
doble_initial
|
doble_initial
|
||||||
stackTests
|
stackTests
|
||||||
bintreeTests
|
bintreeTests
|
||||||
|
doble
|
||||||
*.o
|
*.o
|
||||||
*.exe
|
*.exe
|
||||||
@ -54,7 +54,7 @@ void test_addToTree_multipleElements(void){
|
|||||||
TEST_ASSERT_EQUAL_INT(60, *(int *)tree->right->right->right->right->data);
|
TEST_ASSERT_EQUAL_INT(60, *(int *)tree->right->right->right->right->data);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_addToTree_multipleElementsOptimised(void){
|
void test_addToTree_multipleElements_optimized(void){
|
||||||
TreeNode *tree = NULL;
|
TreeNode *tree = NULL;
|
||||||
int values[] = {50, 30, 70, 20, 40, 60, 80};
|
int values[] = {50, 30, 70, 20, 40, 60, 80};
|
||||||
|
|
||||||
@ -303,7 +303,7 @@ int main(){
|
|||||||
//addToTree()
|
//addToTree()
|
||||||
RUN_TEST(test_addToTree_singleElement);
|
RUN_TEST(test_addToTree_singleElement);
|
||||||
RUN_TEST(test_addToTree_multipleElements);
|
RUN_TEST(test_addToTree_multipleElements);
|
||||||
RUN_TEST(test_addToTree_multipleElementsOptimised);
|
RUN_TEST(test_addToTree_multipleElements_optimized);
|
||||||
RUN_TEST(test_addToTree_withDuplicatesAccept);
|
RUN_TEST(test_addToTree_withDuplicatesAccept);
|
||||||
RUN_TEST(test_addToTree_withoutDuplicatesAccept);
|
RUN_TEST(test_addToTree_withoutDuplicatesAccept);
|
||||||
|
|
||||||
|
|||||||
@ -1,2 +1,3 @@
|
|||||||
|
test;24999
|
||||||
test;9999
|
test;9999
|
||||||
player1;3999
|
player1;3999
|
||||||
|
|||||||
3
main.c
3
main.c
@ -1,5 +1,6 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <time.h>
|
||||||
#include "numbers.h"
|
#include "numbers.h"
|
||||||
#include "timer.h"
|
#include "timer.h"
|
||||||
#include "highscore.h"
|
#include "highscore.h"
|
||||||
@ -37,6 +38,8 @@ void showNumbers(const unsigned int *numbers, unsigned int len)
|
|||||||
// Main game loop: generate numbers, ask user for duplicate, measure time, update highscores.
|
// Main game loop: generate numbers, ask user for duplicate, measure time, update highscores.
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
int exitCode = EXIT_FAILURE;
|
int exitCode = EXIT_FAILURE;
|
||||||
|
|
||||||
if(argc != 2)
|
if(argc != 2)
|
||||||
|
|||||||
73
numbers.c
73
numbers.c
@ -14,13 +14,86 @@
|
|||||||
// 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.
|
||||||
|
|
||||||
|
|
||||||
|
int compareUInts(const void *arg1, const void *arg2){
|
||||||
|
unsigned int val1 = *(unsigned int *)arg1;
|
||||||
|
unsigned int val2 = *(unsigned int *)arg2;
|
||||||
|
|
||||||
|
if(val1 < val2) return -1;
|
||||||
|
if(val1 > val2) return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
unsigned int* createNumbers(unsigned int len)
|
unsigned int* createNumbers(unsigned int len)
|
||||||
{
|
{
|
||||||
|
if(len == 0){
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int* numbers = (unsigned int*)malloc(len * sizeof(unsigned int));
|
||||||
|
if(numbers == NULL){
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
TreeNode *tree = NULL;
|
||||||
|
|
||||||
|
unsigned int i = 0;
|
||||||
|
while(i < len){
|
||||||
|
unsigned int tmp = (rand() % (2*len)) + 1;
|
||||||
|
|
||||||
|
int isDuplicate = 0;
|
||||||
|
tree = addToTree(tree, &tmp, sizeof(unsigned int), compareUInts, &isDuplicate);
|
||||||
|
|
||||||
|
if(!isDuplicate){
|
||||||
|
numbers[i] = tmp;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
clearTree(tree);
|
||||||
|
|
||||||
|
unsigned int rIdx = rand() % len;
|
||||||
|
unsigned int duplicate = numbers[rIdx];
|
||||||
|
|
||||||
|
unsigned int tIdx = rand() % len;
|
||||||
|
while(tIdx == rIdx){
|
||||||
|
tIdx = rand() % len;
|
||||||
|
}
|
||||||
|
numbers[tIdx] = duplicate;
|
||||||
|
|
||||||
|
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 == NULL || len < 2){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int* sorted = (unsigned int*)malloc(len * sizeof(unsigned int));
|
||||||
|
if(sorted == NULL){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
TreeNode *tree = NULL;
|
||||||
|
for(unsigned int i = 0; i < len; i++){
|
||||||
|
tree = addToTree(tree, &numbers[i], sizeof(unsigned int), compareUInts, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
sorted[0] = *(unsigned int *)nextTreeData(tree);
|
||||||
|
|
||||||
|
for(unsigned int j = 1; j < len; j++){
|
||||||
|
sorted[j] = *(unsigned int*)nextTreeData(NULL);
|
||||||
|
}
|
||||||
|
unsigned int duplicate = 0;
|
||||||
|
for(unsigned int k = 0; k < len - 1; k++){
|
||||||
|
if(sorted[k] == sorted[k+1]){
|
||||||
|
duplicate = sorted[k];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(sorted);
|
||||||
|
return duplicate;
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user