This commit is contained in:
Love 2024-09-04 23:03:52 +02:00
commit 19e6e8db4f
6 changed files with 267888 additions and 0 deletions

12
.gitignore vendored Executable file
View File

@ -0,0 +1,12 @@
*~
.DS_Store
*.pro.user.*
*.pro.user
build-*/
*.app
*.exe
build

44
WordChain.pro Executable file
View File

@ -0,0 +1,44 @@
TEMPLATE = app
CONFIG += console
CONFIG += no_include_pwd
SOURCES = $$PWD/src/*.cpp
#SOURCES += $$PWD/lib/*.cpp
HEADERS = $$PWD/src/*.h
HEADERS += $$PWD/lib/*.h
QMAKE_CXXFLAGS += -std=c++11
INCLUDEPATH += $$PWD/lib/
# Copies the given files to the destination directory
defineTest(copyToDestdir) {
files = $$1
for(FILE, files) {
DDIR = $$OUT_PWD
# Replace slashes in paths with backslashes for Windows
win32:FILE ~= s,/,\\,g
win32:DDIR ~= s,/,\\,g
!win32 {
QMAKE_POST_LINK += cp -r '"'$$FILE'"' '"'$$DDIR'"' $$escape_expand(\\n\\t)
}
win32 {
QMAKE_POST_LINK += xcopy '"'$$FILE'"' '"'$$DDIR'"' /e /y $$escape_expand(\\n\\t)
}
}
export(QMAKE_POST_LINK)
}
!win32 {
copyToDestdir($$files($$PWD/res/*))
}
win32 {
copyToDestdir($$PWD/res)
}
macx {
cache()
}

2
lib/readme.txt Executable file
View File

@ -0,0 +1,2 @@
This directory contains any libraries that should be linked
to your project when it is built.

267751
res/dictionary.txt Executable file

File diff suppressed because it is too large Load Diff

3
src/readme.txt Executable file
View File

@ -0,0 +1,3 @@
This directory contains the source code files (*.cpp, *.h)
that you will write as you complete the assignment.
We will also put any instructor-provided code here.

76
src/wordchain.cpp Executable file
View File

@ -0,0 +1,76 @@
#include <fstream>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <unordered_set>
const string ALPHABET = "abcdefghijklmnopqrstuvwxyz";
const char *const FORMAT_UNDERSCORE = "\033[4m";
const char *const END_FORMATTING = "\033[0m";
const char *const DICTIONARY_PATH = "./dictionary.txt";
std::string input(const char *message) {
if (message != nullptr)
std::cout << message;
std::cout << FORMAT_UNDERSCORE;
std::flush(std::cout);
std::string ret;
std::getline(std::cin, ret);
std::cout << END_FORMATTING;
return ret;
}
std::string fileRead(const std::string &filePath) {
std::ifstream file(filePath, std::ios::binary);
if (!file.is_open())
throw std::runtime_error("File couldn't be found");
std::string content;
const int BUF_LENGTH = 1024;
char buf[BUF_LENGTH];
while (file.read(buf, BUF_LENGTH))
content.append(buf, file.gcount());
content.append(buf, file.gcount());
if (!file.eof() && file.fail())
throw std::runtime_error("Error: Failed to read the file '" + filePath +
"'");
return content;
}
std::unordered_set<std::string> readDictionary() {
std::string fileContent = fileRead(DICTIONARY_PATH);
std::unordered_set<std::string> set;
size_t last = 0, idx;
while ((idx = fileContent.find('\n', last)) != std::string::npos) {
std::string sub = fileContent.substr(last, idx - last);
set.insert(sub);
last = idx + 1;
}
set.insert(fileContent.substr(last));
return set;
}
int main() {
std::cout << "Welcome to TDDD86 Word Chain.\n"
<< "If you give me two English words, I will transform the\n"
<< "first into the second by changing one letter at a time.\n\n"
<<"Please type two words: ";
std::flush(std::cout);
std::unordered_set<std::string> dictionary = readDictionary();
std::string userInput = input(nullptr);
size_t wordSep = userInput.find(' ');
std::string wordOne = userInput.substr(0, wordSep);
std::string wordTwo = userInput.substr(wordSep + 1);
std::cout << "Chain from " << wordTwo << " to " << wordOne << std::endl;
std::cout << "Have a nice day!" << std::endl;
return 0;
}