From 00966ca17c1afd828c239221de7e50f0648b5bc2 Mon Sep 17 00:00:00 2001 From: shpp-skobzin Date: Fri, 4 Sep 2015 00:20:26 +0300 Subject: [PATCH 1/2] 1st task is done. --- WordLadder/WordLadder.pro | 2 +- WordLadder/src/WordLadder.cpp | 122 +++++++++++++++++++++++++++++++++- 2 files changed, 122 insertions(+), 2 deletions(-) diff --git a/WordLadder/WordLadder.pro b/WordLadder/WordLadder.pro index 85bdbce..4570e8d 100644 --- a/WordLadder/WordLadder.pro +++ b/WordLadder/WordLadder.pro @@ -110,7 +110,7 @@ win32 { QMAKE_LFLAGS += -Wl,--stack,536870912 LIBS += -lDbghelp LIBS += -lbfd - LIBS += -liberty +# LIBS += -liberty LIBS += -limagehlp } macx { diff --git a/WordLadder/src/WordLadder.cpp b/WordLadder/src/WordLadder.cpp index 01d35d7..c5b372b 100644 --- a/WordLadder/src/WordLadder.cpp +++ b/WordLadder/src/WordLadder.cpp @@ -6,7 +6,127 @@ #include "vector.h" using namespace std; +/** + * Find and return all "neighbor" words of the current word except the used words. + * + * @param word - The current word. + * @param englishWords - All english words. + * @param usedWords - The used words. + * @return - All "neighbor" words. + */ +Vector getNeighborWords(string word, Lexicon &englishWords, Lexicon &usedWords) { + Vector result; + // Add current word to the list of used words if it is not there. + if (!usedWords.contains(word)) + usedWords.add(word); + // Generate new words from the current word by change one char. + for (int i = 0; i < word.length(); i ++) { + string copyWord = word; + for (char c = 'a'; c <= 'z'; c ++){ + copyWord.replace(i, 1, 1, c); + // Add new generated word to the list of "neighbor" words and to the list of used words + // if it is a real english word and it is not used earlier. + if ((englishWords.contains(copyWord)) && (!usedWords.contains(copyWord))) { + result.add(copyWord); + usedWords.add(copyWord); + } + } + } + return result; +} + +/** + * Put the ladder to the queue. + * + * @param ladder - The ladder. + * @param ladders - The ladders queue. + */ +void putToQueue(Vector ladder, Queue> &ladders) { + ladders.enqueue(ladder); +} + +/** + * Put the word to the queue. Create the first ladder. + * + * @param word - The first word in the queue. + * @param ladders - The ladders queue. + */ +void putToQueue(string word, Queue> &ladders) { + Vector ladder(1, word); + ladders.enqueue(ladder); +} + +/** + * Get the ladder from the queue. + * + * @param ladders - The ladders queue. + * @return - The ladder. + */ +Vector getFromQueue(Queue> &ladders) { + return ladders.dequeue(); +} + +/** + * Find and return the shortest ladder from source word to the destination word. + * + * @param sourceWord - The source word. + * @param destWord - The destination word. + * @param englishWords - All english words. + * @return - The found shortest ladder if it exists or empty ladder in other case. + */ +Vector findShortestLadder(string sourceWord, string destWord, Lexicon & englishWords) { + Queue> ladders; + Lexicon usedWords; + // Put the sourse word to the queue. + putToQueue(sourceWord, ladders); + // Repeat search while ladders queue contains ladders. + while (!ladders.isEmpty()) { + // Get ladder from queue and get its last word. + Vector ladder = getFromQueue(ladders); + string lastWord = ladder[ladder.size() - 1]; + // Return the ladder if the last word of the ladder is the destination word. + if (lastWord == destWord) + return ladder; + // Get "neighbor" words of the last word in the ladder except the used words. + Vector neighborWords = getNeighborWords(lastWord, englishWords, usedWords); + // For each "neighbor" word create new ladder by add it word to the end of the ladders copy. + // The new ladder put to the queue. + for (string word : neighborWords) { + Vector copyLadder = ladder; + copyLadder.add(word); + putToQueue(copyLadder, ladders); + } + } + // Return the empty ladder if the shortest ladder is not found. + Vector ladder; + return ladder; +} + +/** + * Output the shortest ladder if it is found or info message in other case. + * + * @param shortestLadder - The ladder. + */ +void showResults(Vector ladder) { + if (ladder.size() != 0) + for (int i = 0; i < ladder.size(); i ++) + if (i != ladder.size() - 1) + cout << ladder[i] << " -> "; + else + cout << ladder[i] << endl; + else + cout << "No ladder found." << endl; +} + int main() { - // TODO: fill in the code + Lexicon englishWords("EnglishWords.txt"); + while (true) { + string sourceWord = getLine("Enter source word (RETURN to quit): "); + if (sourceWord.length() == 0) + break; + string destWord = getLine("Enter destination word: "); + Vector shortestLadder = findShortestLadder(sourceWord, destWord, englishWords); + showResults(shortestLadder); + } return 0; } From a5139eac383c7b6f4942edeb147d351083f25101 Mon Sep 17 00:00:00 2001 From: shpp-skobzin Date: Fri, 4 Sep 2015 22:00:55 +0300 Subject: [PATCH 2/2] All tasks is done. --- RandomWriter/RandomWriter.pro | 2 +- RandomWriter/res/Ham.txt | 80 +++++++++++++++ RandomWriter/src/RandomWriter.cpp | 155 +++++++++++++++++++++++++++++- 3 files changed, 235 insertions(+), 2 deletions(-) create mode 100644 RandomWriter/res/Ham.txt diff --git a/RandomWriter/RandomWriter.pro b/RandomWriter/RandomWriter.pro index 85bdbce..4570e8d 100644 --- a/RandomWriter/RandomWriter.pro +++ b/RandomWriter/RandomWriter.pro @@ -110,7 +110,7 @@ win32 { QMAKE_LFLAGS += -Wl,--stack,536870912 LIBS += -lDbghelp LIBS += -lbfd - LIBS += -liberty +# LIBS += -liberty LIBS += -limagehlp } macx { diff --git a/RandomWriter/res/Ham.txt b/RandomWriter/res/Ham.txt new file mode 100644 index 0000000..5b538a1 --- /dev/null +++ b/RandomWriter/res/Ham.txt @@ -0,0 +1,80 @@ +THE TRAGEDY OF HAMLET, PRINCE OF DENMARK + +by William Shakespeare + +ACT I. Scene I. +Elsinore. A platform before the Castle. + +Enter two Sentinels-[first,] Francisco, [who paces up and down +at his post; then] Bernardo, [who approaches him]. + + Ber. Who's there.? + Fran. Nay, answer me. Stand and unfold yourself. + Ber. Long live the King! + Fran. Bernardo? + Ber. He. + Fran. You come most carefully upon your hour. + Ber. 'Tis now struck twelve. Get thee to bed, Francisco. + Fran. For this relief much thanks. 'Tis bitter cold, + And I am sick at heart. + Ber. Have you had quiet guard? + Fran. Not a mouse stirring. + Ber. Well, good night. + If you do meet Horatio and Marcellus, + The rivals of my watch, bid them make haste. + + Enter Horatio and Marcellus. + + Fran. I think I hear them. Stand, ho! Who is there? + Hor. Friends to this ground. + Mar. And liegemen to the Dane. + Fran. Give you good night. + Mar. O, farewell, honest soldier. + Who hath reliev'd you? + Fran. Bernardo hath my place. + Give you good night. Exit. + Mar. Holla, Bernardo! + Ber. Say- + What, is Horatio there ? + Hor. A piece of him. + Ber. Welcome, Horatio. Welcome, good Marcellus. + Mar. What, has this thing appear'd again to-night? + Ber. I have seen nothing. + Mar. Horatio says 'tis but our fantasy, + And will not let belief take hold of him + Touching this dreaded sight, twice seen of us. + Therefore I have entreated him along, + With us to watch the minutes of this night, + That, if again this apparition come, + He may approve our eyes and speak to it. + Hor. Tush, tush, 'twill not appear. + Ber. Sit down awhile, + And let us once again assail your ears, + That are so fortified against our story, + What we two nights have seen. + Hor. Well, sit we down, + And let us hear Bernardo speak of this. + Ber. Last night of all, + When yond same star that's westward from the pole + Had made his course t' illume that part of heaven + Where now it burns, Marcellus and myself, + The bell then beating one- + + Enter Ghost. + + Mar. Peace! break thee off! Look where it comes again! + Ber. In the same figure, like the King that's dead. + Mar. Thou art a scholar; speak to it, Horatio. + Ber. Looks it not like the King? Mark it, Horatio. + Hor. Most like. It harrows me with fear and wonder. + Ber. It would be spoke to. + Mar. Question it, Horatio. + Hor. What art thou that usurp'st this time of night + Together with that fair and warlike form + In which the majesty of buried Denmark + Did sometimes march? By heaven I charge thee speak! + Mar. It is offended. + Ber. See, it stalks away! + Hor. Stay! Speak, speak! I charge thee speak! + +THE END diff --git a/RandomWriter/src/RandomWriter.cpp b/RandomWriter/src/RandomWriter.cpp index e591dac..622bc8b 100644 --- a/RandomWriter/src/RandomWriter.cpp +++ b/RandomWriter/src/RandomWriter.cpp @@ -6,9 +6,162 @@ #include "random.h" #include "strlib.h" #include "vector.h" +#include "filelib.h" +#include "simpio.h" using namespace std; +/** + * Request the file name and open the file if the name is valid. + * + * @param inputFile - The file for open. + */ +void openFile(ifstream &inputFile) { + promptUserForFile(inputFile, "Enter the file name: "); +} + +/** + * Close the file. + * + * @param inputFile - The file for close. + */ +void closeFile(ifstream &inputFile) { + inputFile.close(); +} + +/** + * Request the model level. + * + * @return - The model level value. + */ +int getModelLevel() { + int modelLevel; + while (true) { + modelLevel = getInteger("Enter model level (1-10): "); + if ((modelLevel >= 1) && (modelLevel <= 10)) + break; + cout << "Wrong input data." << endl; + } + return modelLevel; +} + +/** + * Read the first seed from the file beginning. + * + * @param inputFile - The file for read. + * @param modelLevel - Set the seed length. + * @param seed - The read seed. + * @param nextChar - The next char after the seed. + * @return - True if the seed and the next char are read, or false in other case. + */ +bool readFirstSeed(ifstream &inputFile, int modelLevel, string &seed, char &nextChar) { + for (int i = 0; i < modelLevel; i ++) { + char ch = inputFile.get(); + if (ch == EOF) + return false; + seed += ch; + } + if (!inputFile.get(nextChar)) + return false; + return true; +} + +/** + * Read the next seed after the current seed. + * + * @param inputFile - The file for read. + * @param seed - The current seed before reading. The next seed after reading. + * @param nextChar - The next char after the seed. + * @return - True if the seed and the next char are read, or false in other case. + */ +bool readNextSeed(ifstream &inputFile, string &seed, char &nextChar) { + seed.erase(0, 1); + seed += nextChar; + if (!inputFile.get(nextChar)) + return false; + return true; +} + +/** + * Read the all seeds from the file. + * + * @param inputFile - The file for read. + * @param modelLevel - Set the seed length. + * @return - The map of the read seeds and the next chars. + */ +Map> readAllSeeds(ifstream &inputFile, int modelLevel) { + Map> result; + string seed; + char nextChar; + cout << "The file is processed..." << endl; + // Read the first seed. If the first seed exists in the file, add it to result map. + if (readFirstSeed(inputFile, modelLevel, seed, nextChar)) + result[seed].add(nextChar); + // Read the next seeds while they exists in the file. + while (true) { + if (!readNextSeed(inputFile, seed, nextChar)) + break; + result[seed].add(nextChar); + } + return result; +} + +/** + * Find and return the most frequency seed. + * + * @param seeds - The map of the seeds and the next chars. + * @return - The most frequency seed. + */ +string getFirstSeed(Map> &seeds) { + Vector keys = seeds.keys(); + string result = keys[0]; + // Find the seed with the longest vector of the next chars. + for (string key : seeds) + if (seeds[key].size() > seeds[result].size()) + result = key; + return result; +} + +/** + * Find the next seed after the current seed by add the next char. + * + * @param seeds - The map of the seeds and the next chars. + * @param seed - The current seed before search. The next seed after search. + * @return - The next char from the current seeds next chars vector. + */ +char getNextSeed(Map> &seeds, string &seed) { + Vector nextChars = seeds[seed]; + // Choose the next char from the current seeds next chars vector. + char result = nextChars[randomInteger(0, nextChars.size() - 1)]; + // Create the next seed by the current seeds one char shifting left and add the choosed next char. + seed.erase(0, 1); + seed += result; + return result; +} + +/** + * Generate randomly english text lenth 2000 chars. + * + * @param seeds - The map of the seeds and the next chars. + * @return - The randomly generated text. + */ +string generateText(Map> &seeds) { + string seed = getFirstSeed(seeds); + string text = seed; + while (true) { + text += getNextSeed(seeds, seed); + if (text.length() > 2000) + break; + } + return text; +} + int main() { - // TODO: fill in the code + ifstream inputFile; + openFile(inputFile); + int modelLevel = getModelLevel(); + Map> seeds = readAllSeeds(inputFile, modelLevel); + closeFile(inputFile); + string text = generateText(seeds); + cout << text << endl; return 0; }