Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion RandomWriter/RandomWriter.pro
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ win32 {
QMAKE_LFLAGS += -Wl,--stack,536870912
LIBS += -lDbghelp
LIBS += -lbfd
LIBS += -liberty
# LIBS += -liberty
LIBS += -limagehlp
}
macx {
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions RandomWriter/res/r.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Roses are red.
Violets are blue.
Sugar is sweet,
And so are you!
File renamed without changes.
135 changes: 133 additions & 2 deletions RandomWriter/src/RandomWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,140 @@
#include "random.h"
#include "strlib.h"
#include "vector.h"
#include "simpio.h"
#include "random.h"

using namespace std;

string promptUserForFile(ifstream & infile, string prompt); //ask for input file
Map<string, Vector<char>> makeMap(Vector<char> vec, int order);
string arToStr(Vector<char>);
string startOfRes(Map<string, Vector<char>>);
string makeRandomTail(string, Map<string, Vector<char>>, int);

int main() {
// TODO: fill in the code
return 0;
ifstream infile;
promptUserForFile(infile, "Enter filename: ");
int order = getInteger("Enter number of order (from 1 to 10):");
Vector<char> inputFileRead;
char ch;
while (infile.get(ch)){
if (ch == EOF) break;
inputFileRead.add(ch);
}
Map<string, Vector<char>> map = makeMap(inputFileRead, order);
string a = startOfRes(map);
cout << "RANDOM TEXT: " << makeRandomTail(a, map, order) << endl;
return 0;
}
/*
* function create random text
*
* @param first string from startOfRes,
* map from makeMap,
* oreder - current seed that entered user
* @return random text
*/
string makeRandomTail(string str, Map<string, Vector<char>> map, int order){
string res;
res += str;
for(int i = 2000 - order; i > 0; i--){
string lastOrder = res.substr(res.length() - order, res.length());
if(!map.containsKey(lastOrder)){
return res;
}
Vector<char> adding = map.get(lastOrder);
string addToRes = arToStr(adding);
int rnd = randomInteger(0, addToRes.length()-1);
char ch = addToRes[rnd];
res += ch;
}
return res;
}
/*
* choose random first word, To start random text.
*
* @param Map of seeds and next char <seed, char>
* @return first seed of random text
*/
string startOfRes(Map<string, Vector<char>> map){
Map<string, Vector<char>> bestKey;
Vector<string> whereGetStartWord;
string res = "";
int maxNum = 0;
for(string j : map){
Vector<char> tmp = map.get(j);
int curNum = tmp.size();
if(curNum > maxNum){
maxNum = curNum;
}
}
for(string u : map){
Vector<char> tmp = map.get(u);
if(tmp.size() == maxNum){
bestKey.put(u, tmp);
}

}
whereGetStartWord = bestKey.keys();
int rand = randomInteger(0,(whereGetStartWord.size() - 1));
res = whereGetStartWord[rand];
return res;
}

/*
* crate Map like key"re" - vector"a, b, g";
*
* @param vector<char>
* @return string from vector
*/
Map<string, Vector<char>> makeMap(Vector<char> vec, int order){
Map<string, Vector<char>> map;
for(int i = 0; i < vec.size() - order - 1; i++){
Vector<char> sub = vec.subList(i, order);
string kkey = arToStr(sub);
Vector<char> vvalue;
vvalue.add(vec.get(i + order));
if(map.containsKey(kkey)){
Vector<char> add = map.get(kkey);
map.remove(kkey);
add += vvalue;
map.put(kkey, add);
add.clear();
}
if(map.containsKey(kkey) == false){
map.put(kkey, vvalue);
}
}
return map;
}

/*
* transfors array of <char> in one string
*
* @param vector<char>
* @return string from vector
*/
string arToStr(Vector<char> vec){
string res;
for(char ch : vec){
res += ch;
}
return res;
}

//ask user to enter file name
string promptUserForFile(ifstream & infile, string prompt ){
while(true){
cout << prompt;
string filename;
getline(cin, filename);
infile.open(filename.c_str());
if(!infile.fail()) return filename;
infile.clear();
cout << "UNABLE TO OPEN FILE! Try again." << endl;
if(prompt == ""){
prompt = "Input filename: ";
}
}
}
2 changes: 1 addition & 1 deletion WordLadder/WordLadder.pro
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ win32 {
QMAKE_LFLAGS += -Wl,--stack,536870912
LIBS += -lDbghelp
LIBS += -lbfd
LIBS += -liberty
# LIBS += -liberty
LIBS += -limagehlp
}
macx {
Expand Down
108 changes: 106 additions & 2 deletions WordLadder/src/WordLadder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,113 @@
#include "queue.h"
#include "simpio.h"
#include "vector.h"
#include <queue>
using namespace std;

Queue<Vector<string>> myqueue;
Lexicon dictionary("EnglishWords.dat");
Vector<string> diffOneLetterWord(string);
Vector<string> wordLadder(string, string);

int main() {
// TODO: fill in the code
return 0;
while(true){
string startWord = getLine("Enter start word - ");
if(startWord.length() == 0){
break;
}
string endWord = getLine("Enter destination word - ");
Vector<string> res;
if(startWord.length() == endWord.length()){
res = wordLadder(startWord, endWord);
}
if(res.isEmpty()){ //if returned vector is empty print "no ladder"
cout << "No ladder found!" << endl;
}
for(int i = 0; i < res.size(); i++){ //formating result
if(i == (res.size() - 1)){
cout << res[i] << endl;
}
if(i!=(res.size() - 1)){
cout << res[i] << " -> ";
}

}
//if don't clear arry, we get the same array
res.clear();
}
return 0;
}

/*
* Make an vector of words from startWord to endWord
*
* @param user input - startWord, endWord
* @return Vector-Ladder of words
*/

Vector<string> wordLadder(string startWord, string endWord){
Lexicon usedWords;
usedWords.add(startWord);
Vector<string> res;
res.add(startWord);
myqueue.enqueue(res);
res.clear();

while (!myqueue.isEmpty()) {
//get first ladder from queue
Vector<string> vec = myqueue.dequeue();
//if last word is equal to endWord
if(vec[vec.size() - 1] == endWord){
res = vec;
return res;
}
//make an array of different words for last word in vec
Vector<string> tmpque = diffOneLetterWord(vec[vec.size() - 1]);
for(string diffWord : tmpque){
//if current word is not used earlier. Add new Vector to queue
if(!usedWords.contains(diffWord)){
usedWords.add(diffWord);
Vector<string> nVec = vec;
nVec.add(diffWord);
myqueue.enqueue(nVec);
}
}
}
return res;
}

/*
* Make an array of words that have one different letter
*
* @param user input - startWord
* @return Array of words
*/
Vector<string> diffOneLetterWord(string startWord){
Vector<string> res;
Vector<char> newWord;
//make from startWord an array newWord to change letter by letter
for(int w = 0; w < startWord.length(); w++){
newWord.add(startWord[w]);
}
//cout << newWord << endl;
string curWord = "";
string alphabet = "abcdefghijklmnopqrstuvwxyz";
//start to change each letter of word (array)
for(int i = 0; i < newWord.size(); i++){
for(int a = 0; a < alphabet.length(); a++){
newWord.set(i, alphabet[a]);
//convert array into word
for(int r = 0; r < newWord.size(); r++){
curWord += newWord[r];
}
if(curWord != startWord){
if(dictionary.contains(curWord)){
res.add(curWord);
}
}
curWord = "";
}
newWord.set(i, startWord[i]);
}
return res;
}