From 295bfe0829702e5f80eb94f76d752f249df5471c Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Fri, 10 Mar 2023 12:26:51 +0100 Subject: [PATCH 1/3] updated --- .vscode/launch.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 9bae310..f8d1b90 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,7 +8,7 @@ "type": "cppdbg", "request": "launch", "program": "${workspaceRoot}/build/tipplektionen", - "args": ["--min-length", "2", "--max-length", "6", "-c", "ctienrsg"], + "args": ["--min-length", "2", "--max-length", "6", "-c", "ctienrsgul", "--capital"], "cwd": "${workspaceRoot}", }] } \ No newline at end of file From 72cd2d8f972cd2ae8f47a5ac17532462883b75a4 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Fri, 10 Mar 2023 12:27:15 +0100 Subject: [PATCH 2/3] updated --- README.md | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index c37fadf..1b50e74 100644 --- a/README.md +++ b/README.md @@ -28,12 +28,16 @@ Anschließend liegt im build-Verzeichnis die ausführbare Datei. Die Option `-h` listet alle Möglichkeiten auf: ``` Erlaubte Optionen: - -h [ --help ] Ausgabe der Hilfe - -c [ --characters ] arg Enthaltene Buchstaben - --min-length arg Minimale Wortlänge - --max-length arg Maximale Wortlänge - --line-length arg Maximale Zeilenlänge - -n [ --num-lines ] arg Anzahl Zeilen - --lowercase Ausgabe nur klein geschrieben - -d [ --dictfile ] arg Pfad zur Wörterbuchdatei + -h [ --help ] Ausgabe der Hilfe + -c [ --characters ] arg Erlaubte Buchstaben + -m [ --must-have ] arg Buchstaben, die im Wort vorhanden sein *müssen* + (nur in Verbindung mit --characters; Buchstaben + müssen davon Subset sein.) + --min-length arg Minimale Wortlänge + --max-length arg Maximale Wortlänge + --line-length arg Maximale Zeilenlänge + -n [ --num-lines ] arg Anzahl Zeilen + -d [ --dictfile ] arg Pfad zur Wörterbuchdatei + -l [ --lowercase ] Ausgabe nur klein geschrieben + -s [ --starts-capital ] Wort muss mit einem Großbuchstaben beginnen ``` \ No newline at end of file From 04a29581d11cb396433a6ffd80973e2048830dcd Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Fri, 10 Mar 2023 12:27:41 +0100 Subject: [PATCH 3/3] more options implemented --- main.cpp | 82 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 67 insertions(+), 15 deletions(-) diff --git a/main.cpp b/main.cpp index 0b0cdd5..9808f0c 100644 --- a/main.cpp +++ b/main.cpp @@ -16,39 +16,66 @@ namespace po = boost::program_options; int main(int argc, char *argv[]) { - std::string letters; + icu::UnicodeString letters; + icu::UnicodeString mustHaveLetters; int wordLengthMin{1}; int wordLengthMax{1024}; int lineLength{60}; int numLines{4}; std::string wordsFile{"/usr/share/dict/ngerman"}; bool lowercase{false}; + bool startsCapital{false}; po::options_description desc("Erlaubte Optionen"); desc.add_options()("help,h", "Ausgabe der Hilfe")( - "characters,c", po::value(&letters), "Enthaltene Buchstaben")( - "min-length", po::value(&wordLengthMin), - "Minimale Wortlänge")("max-length", po::value(&wordLengthMax), "Maximale Wortlänge")( - "line-length", po::value(&lineLength), - "Maximale Zeilenlänge")("num-lines,n", po::value(&numLines), "Anzahl Zeilen")( - "lowercase", po::bool_switch(&lowercase), "Ausgabe nur klein geschrieben")( - "dictfile,d", po::value(&wordsFile), "Pfad zur Wörterbuchdatei"); + "characters,c", po::value(&letters), "Erlaubte Buchstaben")( + "must-have,m", po::value(&mustHaveLetters), + "Buchstaben, die im Wort vorhanden sein *müssen* (nur in Verbindung mit --characters; " + "Buchstaben müssen davon Subset sein.)")("min-length", po::value(&wordLengthMin), + "Minimale Wortlänge")( + "max-length", po::value(&wordLengthMax), + "Maximale Wortlänge")("line-length", po::value(&lineLength), "Maximale Zeilenlänge")( + "num-lines,n", po::value(&numLines), "Anzahl Zeilen")( + "dictfile,d", po::value(&wordsFile), "Pfad zur Wörterbuchdatei")( + "lowercase,l", po::bool_switch(&lowercase), + "Ausgabe nur klein geschrieben")("starts-capital,s", po::bool_switch(&startsCapital), + "Wort muss mit einem Großbuchstaben beginnen"); po::variables_map vm; - po::store(po::parse_command_line(argc, argv, desc), vm); - po::notify(vm); + try { + po::store(po::parse_command_line(argc, argv, desc), vm); + po::notify(vm); + } catch (po::error &err) { + std::cerr << "Fehler beim Verarbeiten der Optionen:\n"; + std::cerr << err.what() << std::endl; + return 1; + } if (vm.contains("help")) { std::cout << desc << std::endl; return 1; } + if (mustHaveLetters.length() > 0) { + for (int i{0}; i < mustHaveLetters.length(); i++) { + auto ch = mustHaveLetters.charAt(i); + if (letters.indexOf(ch) == -1) { + std::cout + << "Die Buchstaben von --must-have müssen ein Subset von --characters sein." + << std::endl; + return 1; + } + } + } + std::ifstream infile(wordsFile); std::string line; icu::UnicodeString uline; std::vector words; - icu::UnicodeString ucLetters = icu::UnicodeString::fromUTF8(letters); - icu::UnicodeString ucLettersUpper = icu::UnicodeString::fromUTF8(letters).toUpper(); + icu::UnicodeString lettersUpper = letters; + lettersUpper.toUpper(); + icu::UnicodeString mustHaveLettersUpper = mustHaveLetters; + mustHaveLettersUpper.toUpper(); long counter{0}; if (!std::filesystem::exists(wordsFile)) { @@ -63,11 +90,30 @@ int main(int argc, char *argv[]) if (length < wordLengthMin || length > wordLengthMax) continue; - if (ucLetters.length() > 0) { + if (mustHaveLetters.length() > 0) { bool valid = true; - for (int i{0}; i < uline.length(); i++) { + for (int i{0}; i < mustHaveLetters.length(); i++) { + auto ch = mustHaveLetters.charAt(i); + auto chU = mustHaveLettersUpper.charAt(i); + if (uline.indexOf(ch) == -1 && uline.indexOf(chU) == -1) { + valid = false; + break; + } + } + if (!valid) + continue; + } + + if (letters.length() > 0) { + bool valid = true; + for (int i{0}; i < length; i++) { auto ch = uline.charAt(i); - if (ucLetters.indexOf(ch) == -1 && ucLettersUpper.indexOf(ch) == -1) { + if (startsCapital && i == 0) { + if (lettersUpper.indexOf(ch) == -1) { + valid = false; + break; + } + } else if (letters.indexOf(ch) == -1 && lettersUpper.indexOf(ch) == -1) { valid = false; break; } @@ -83,11 +129,17 @@ int main(int argc, char *argv[]) counter++; } + if (counter == 0) { + std::cout << "Keine Wörter mit den angegebenen Kriterien gefunden." << std::endl; + return 0; + } + std::cout << "Anzahl infrage kommender Wörter: " << counter << "\n" << std::endl; std::random_device rdev; std::mt19937 rng(rdev()); std::uniform_int_distribution distrib(0, words.size() - 1); + for (int i{0}; i < numLines; i++) { icu::UnicodeString lectionLine; while (lectionLine.length() < lineLength) {