From 3e16cbaa8194b38f2acbb8836d47d7b9e5db6f87 Mon Sep 17 00:00:00 2001 From: MrGeorgen Date: Thu, 26 Nov 2020 16:13:01 +0100 Subject: [PATCH] not finnished solution for a1 --- a1-Woerter-aufraeumen/CMakeLists.txt | 4 ++ a1-Woerter-aufraeumen/raetsel0.txt | 2 + a1-Woerter-aufraeumen/src/main.c | 80 ++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 a1-Woerter-aufraeumen/CMakeLists.txt create mode 100644 a1-Woerter-aufraeumen/raetsel0.txt create mode 100644 a1-Woerter-aufraeumen/src/main.c diff --git a/a1-Woerter-aufraeumen/CMakeLists.txt b/a1-Woerter-aufraeumen/CMakeLists.txt new file mode 100644 index 0000000..1414fd5 --- /dev/null +++ b/a1-Woerter-aufraeumen/CMakeLists.txt @@ -0,0 +1,4 @@ +file(GLOB A1SOURCES "src/*.c") +file(GLOB ACLSOURCES "../lib/acl/src/*.c") +add_executable(a1 ${A1SOURCES} ${ACLSOURCES}) +target_link_libraries(a1 PRIVATE m) diff --git a/a1-Woerter-aufraeumen/raetsel0.txt b/a1-Woerter-aufraeumen/raetsel0.txt new file mode 100644 index 0000000..dfd94b7 --- /dev/null +++ b/a1-Woerter-aufraeumen/raetsel0.txt @@ -0,0 +1,2 @@ +_h +oh diff --git a/a1-Woerter-aufraeumen/src/main.c b/a1-Woerter-aufraeumen/src/main.c new file mode 100644 index 0000000..606d5fd --- /dev/null +++ b/a1-Woerter-aufraeumen/src/main.c @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include +#include + + +char *substitutedWord; + +void pointerCheck(void *pointer) { + if(pointer == NULL) { + perror("Error: "); + exit(-1); + } +} + +bool tryFillWord(char *einsetzbaresWort, char *lueke) { + char *j = lueke; +// printf("len %lu\n", strlen(einsetzbaresWort)); + //printf("wort: %sb baum: %c%c%c\n", einsetzbaresWort, lueke[0], lueke[1], lueke[2]); + for(char *i = einsetzbaresWort; *i != '\0'; ++i, ++j) { + if(!(*j == '_' || *j == *i)) { + if(*i == '\0') printf("null\n"); + // printf("wwwort%cbaum%cb\n", *i, *j); + return false; + } + } + int w = *j; + if(*j != ' ' && *j != ',' && *j != '\n' && *j != '.' && *j != ';' && *j != ':' && *j != '\0' && *j != '?' && *j != '!') return false; + substitutedWord = lueke; + //printf("wort geöst\n"); + return true; +} + +int main(int argc, char *argv[]) { + if(argc != 2) printf("file argument required"); + bool sucess; + FILE *fp = fopen(argv[1], "rb"); + char *input = acl_ReadTextFile(fp, &sucess); + fclose(fp); + pointerCheck(input); + if(input[strlen(input) - 1] == '\n') input[strlen(input) - 1] = '\0'; +/* ein Weg um den Beginn der zweiten Zeile zu finden. + * Man Könnte auch die Datei im Textmodus öffnen, da bei zwei Zeilen der Geschwindigkeitsverlust nicht groß wäre, aber man es für beliebig große Lückentexte machen wollte, bräuchte man wieder zwei memory allocations. + * Dadurch wäre der Code auch nicht wirklich einfacher.*/ + char *words = input; + printf("%s\n", input); + while(*words != '\n') ++words; + *words = '\0'; + ++words; + // Alle Wörter, die in den Lückentext eingesetzt werden, werden einem array hinzugefügt. + char **availableWords = acl_arraylist_create(1, sizeof *availableWords); + availableWords[0] = words; + for(char *i = words; *i != '\0'; ++i) { + if(*i == ' ') { + printf("2\n"); + *i = '\0'; + char *nextWord = i + 1; + availableWords = acl_arraylist_append(availableWords, &nextWord); + pointerCheck(availableWords); + } + } + while(acl_arraylist_len(availableWords)) { // Wenn die Länge vom array 0 ist, haben wir alle Wörter eingesetzt. + for(uint32_t i = 0; i < acl_arraylist_len(availableWords); ++i) { + uint32_t matchCounter = 0; + matchCounter += tryFillWord(availableWords[i], input); + for(char *k = input; *k != '\0'; ++k) { + if(*k == ' ') matchCounter += tryFillWord(availableWords[i], k + 1); + } + if(matchCounter != 1) continue; + //availableWords = acl_arraylist_remove(availableWords, i); + memcpy(substitutedWord, availableWords[i], strlen(availableWords[i])); + } + printf("%s\n", input); + } + printf("%s", input); + free(input); + acl_arraylist_free(availableWords); +}