From aa9d7f6543982618e0b4c72cfbfd3ebfeda293c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Tue, 15 Sep 2020 13:47:27 +0200 Subject: TDDDD86 labb 2 redovisning --- labb2/wordchain/src/wordchain.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'labb2/wordchain') diff --git a/labb2/wordchain/src/wordchain.cpp b/labb2/wordchain/src/wordchain.cpp index 5eb44c6..93f66af 100644 --- a/labb2/wordchain/src/wordchain.cpp +++ b/labb2/wordchain/src/wordchain.cpp @@ -10,7 +10,7 @@ using namespace std; const string ALPHABET = "abcdefghijklmnopqrstuvwxyz"; -void openDictionary(set &words, string path) { +void openDictionary(set &words, const string &path) { ifstream input; input.open(path); string word; @@ -21,13 +21,14 @@ void openDictionary(set &words, string path) { } /* - * Populates `neighbours` with all valid word neighbours from `words`. + * Populate `neighbours` with all valid word neighbours + * (words with only one char differing) from `words`. */ -void getValidNeighbours(forward_list &neighbours, string &word, set &words) { +void getValidNeighbours(forward_list &neighbours, const string &word, const set &words) { for (int i = 0; i < word.length(); i++) { string new_word = word; char cur_c = word[i]; - for (char new_c: ALPHABET) { + for (char new_c : ALPHABET) { if (new_c != cur_c) { new_word[i] = new_c; if (words.count(new_word)) { @@ -38,7 +39,11 @@ void getValidNeighbours(forward_list &neighbours, string &word, set &chain, string w1, string w2, set &words) { +/* + * Find the shortest word chain between `w1` and `w2` + * and store it in `chain`. + */ +void wordchain(stack &chain, const string &w1, const string &w2, const set &words) { stack firstChain; firstChain.push(w1); @@ -51,7 +56,7 @@ void wordchain(stack &chain, string w1, string w2, set &words) { chains.pop(); forward_list validNeighbours; getValidNeighbours(validNeighbours, curChain.top(), words); - for (string &w: validNeighbours) { + for (string &w : validNeighbours) { if (w == w2) { // done chain.push(w); -- cgit v1.2.1