generated from freudenreichan/info2Praktikum-DobleSpiel
Compare commits
No commits in common. "7c6f6b0370f358b028f14e158a79704a7fbd7329" and "08fbfefa32bf7e83b6c40a8bd101ad50a9851c9b" have entirely different histories.
7c6f6b0370
...
08fbfefa32
10
makefile
10
makefile
@ -35,14 +35,8 @@ $(program_obj_filesobj_files): %.o: %.c
|
|||||||
# --------------------------
|
# --------------------------
|
||||||
# Unit Tests
|
# Unit Tests
|
||||||
# --------------------------
|
# --------------------------
|
||||||
unitTests: stacktest
|
unitTests:
|
||||||
|
echo "needs to be implemented"
|
||||||
|
|
||||||
test_stack: stacktest.o stack.o
|
|
||||||
$(CC) $(FLAGS) $^ -o stacktest
|
|
||||||
|
|
||||||
test_stack.o: stacktest.c
|
|
||||||
$(CC) -c $(FLAGS) $< -o $@
|
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Clean
|
# Clean
|
||||||
|
|||||||
56
numbers.c
56
numbers.c
@ -14,69 +14,13 @@
|
|||||||
// 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 compare(const void* a, const void* b) {
|
|
||||||
return (*(unsigned int*)a - *(unsigned int*)b);
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int *createNumbers(unsigned int len)
|
unsigned int *createNumbers(unsigned int len)
|
||||||
{
|
{
|
||||||
//array erstellen
|
|
||||||
if(len < 2)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
unsigned int *arr = malloc(sizeof(unsigned int)*len);
|
|
||||||
|
|
||||||
//sicherstellen dass zahlen einmalig vorkommen
|
|
||||||
srand(time(NULL));
|
|
||||||
for(int i = 0; i < len; i++){
|
|
||||||
while(1){
|
|
||||||
unsigned int value = (rand() % (2 * len)) + 1;
|
|
||||||
int alreadyUsed = 0;
|
|
||||||
for(int j = 0; j < i; j++) {
|
|
||||||
if(arr[j] == value) {
|
|
||||||
alreadyUsed = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(!alreadyUsed) {
|
|
||||||
arr[i] = value;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//eine zahl duplizieren und irgendwo hinkopieren
|
|
||||||
int dupIndex = rand() % len;
|
|
||||||
int targetIndex;
|
|
||||||
do{
|
|
||||||
targetIndex = rand() % len;
|
|
||||||
}while (targetIndex == dupIndex);
|
|
||||||
arr[targetIndex] = arr[dupIndex];
|
|
||||||
|
|
||||||
return arr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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(len < 2){
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int *copy = malloc(sizeof(unsigned int)*len);
|
|
||||||
|
|
||||||
memcpy(copy, numbers, len * sizeof(unsigned int));
|
|
||||||
|
|
||||||
qsort(copy, len, sizeof(unsigned int), compare);
|
|
||||||
|
|
||||||
unsigned int duplicate = 0;
|
|
||||||
for(int i = 1; i < len; i++){
|
|
||||||
if(copy[i] == copy[i-1]){
|
|
||||||
duplicate = copy[i];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
free(copy);
|
|
||||||
return duplicate;
|
|
||||||
}
|
}
|
||||||
27
stack.c
27
stack.c
@ -10,43 +10,24 @@
|
|||||||
// Pushes data as pointer onto the stack.
|
// Pushes data as pointer onto the stack.
|
||||||
StackNode *push(StackNode *stack, void *data)
|
StackNode *push(StackNode *stack, void *data)
|
||||||
{
|
{
|
||||||
StackNode *newNode = (StackNode *)malloc(sizeof(StackNode));
|
|
||||||
if (newNode == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
newNode ->data = data;
|
|
||||||
newNode ->next = stack;
|
|
||||||
return newNode;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
|
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
|
||||||
// freed by caller.)
|
// freed by caller.)
|
||||||
StackNode *pop(StackNode *stack)
|
StackNode *pop(StackNode *stack)
|
||||||
{
|
{
|
||||||
if(stack == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
StackNode *next = stack->next;
|
|
||||||
free(stack);
|
|
||||||
return next;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the data of the top element.
|
// Returns the data of the top element.
|
||||||
void *top(StackNode *stack)
|
void *top(StackNode *stack)
|
||||||
{
|
{
|
||||||
if(stack == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return stack->data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clears stack and releases all memory.
|
// Clears stack and releases all memory.
|
||||||
void clearStack(StackNode *stack)
|
void clearStack(StackNode *stack)
|
||||||
{
|
{
|
||||||
StackNode *temp;
|
|
||||||
while(stack != NULL) {
|
|
||||||
temp = stack;
|
|
||||||
stack = stack->next;
|
|
||||||
free(temp);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
5
stack.h
5
stack.h
@ -8,12 +8,7 @@ The latest element is taken from the stack. */
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
//TODO: passenden Datentyp als struct anlegen
|
//TODO: passenden Datentyp als struct anlegen
|
||||||
typedef struct{
|
|
||||||
void *data;
|
|
||||||
struct StackNode *next;
|
|
||||||
|
|
||||||
|
|
||||||
}StackNode;
|
|
||||||
// Pushes data as pointer onto the stack.
|
// Pushes data as pointer onto the stack.
|
||||||
StackNode *push(StackNode *stack, void *data);
|
StackNode *push(StackNode *stack, void *data);
|
||||||
|
|
||||||
|
|||||||
58
stacktest.c
58
stacktest.c
@ -1,58 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include "stack.h"
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
|
|
||||||
//Funktion, um nachzuprüfen, ob Test bestanden wurde
|
|
||||||
void check(char *msg, int expected, int actual){
|
|
||||||
printf("%s: ", msg);
|
|
||||||
printf("Erwartet: %d, Tatsächlich: %d\n", expected, actual);
|
|
||||||
if(expected == actual){
|
|
||||||
printf("Test bestanden\n");
|
|
||||||
}else{
|
|
||||||
printf("Test nicht bestanden\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
//Schnappt sich oberstes Element
|
|
||||||
int getTop(StackNode *stack){
|
|
||||||
void *data = top(stack);
|
|
||||||
return(int)data;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//Tests der einzelnen Stackfunktionen
|
|
||||||
int main(){
|
|
||||||
StackNode *stack = NULL;
|
|
||||||
//Test push
|
|
||||||
|
|
||||||
stack = push(stack, (void*)10);
|
|
||||||
check("Nach Push 10", 10, getTop(stack));
|
|
||||||
|
|
||||||
stack = push(stack, (void*)20);
|
|
||||||
check("Nach Push 20", 20, getTop(stack));
|
|
||||||
|
|
||||||
//pop testen
|
|
||||||
stack = pop(stack);
|
|
||||||
check("Nach Pop", 10, getTop(stack));
|
|
||||||
|
|
||||||
//clearStack testen
|
|
||||||
clearStack(stack);
|
|
||||||
stack = NULL;
|
|
||||||
check("Nach ClearStack", 0, getTop(stack));
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user