1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
#include <iostream>
#include <fstream>
#include <cassert>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
#include<map>
#include "hash.h"
#include "queueType.h"
#include "arrayBasedListType.h"
using namespace std;
struct node
{
string word;
int distance;
string predecesor;
node(string word1, string pre)
{
word = word1;
distance = 1;
predecesor = pre;
}
string getWord()
{
return word;
}
string getPredecesor()
{
return predecesor;
}
};
vector<string> exchange(string&, hashTableType<string, strHashFunctor>&ht, vector<node*>&, vector<vector<node*>>&);
void printAdjacency(const string &word, hashTableType<string, strHashFunctor>&ht);
int main()
{
int S = 100000;
hashTableType<string, strHashFunctor> ht(S);
vector<node*> wordlist;
vector<vector<node*>> vectorOfVectors;
ifstream infile;
infile.open("dictionary.txt");
string temp;
//scan the file and add contents to the hash table
while (!infile.eof())
{
infile >> temp;
ht.insert(temp);
}
ifstream inFile;
string first, second;
char choice = 'y';
vector<string> listOfWords;
arrayQueueType<string> queueofWords;
while (choice != 'n'&&choice != 'N')
{
cout << "Enter first word " << endl;
cin >> first;
cout << "Enter second word " << endl;
cin >> second;
printAdjacency(first, ht);//shoud I put this inside a loop?
/*listOfWords = exchange(first, ht,wordlist, vectorOfVectors);
for (int i = 0; i < listOfWords.size(); i++)
{
exchange(listOfWords[i], ht, wordlist,vectorOfVectors);
}
for (int i = 0; i < vectorOfVectors.size(); i++)
{
for (int j = 0; j < vectorOfVectors[i].size(); j++)
{
cout << vectorOfVectors[i][j]->word << " ";
}
cout << endl<<".................."<<endl;
}*/
cout << "Do you want to try again? (y/n): ";
cin >> choice;
}
return 0;
}
vector<string> exchange(string & word, hashTableType<string, strHashFunctor>&ht, vector<node*>&wordlist, vector<vector<node*>>& vectorOfVectors)
{
wordlist.clear();
string word1;
vector<string> words;
node *root = new node(word, word);
wordlist.push_back(root);
for (int i = 0; i < word.size(); i++)//loop for every letter of the word
{
word1 = word;
for (char letter = 'a'; letter <= 'z'; ++letter)//loop for every letter of the alphabet
{
word1[i] = letter;
if (ht.search(word1) && word1 != word)
{
//cout << word1 << endl;
node *root = new node(word1, word);
wordlist.push_back(root);
words.push_back(word1);
}
}
}
vectorOfVectors.push_back(wordlist);
return words;
}
void printAdjacency(const string &word, hashTableType<string, strHashFunctor>&ht)
{
vector<string> wordsToPrint;
wordsToPrint.push_back(word); // start with this one word to print
for (size_t i = 0; i < wordsToPrint.size(); ++i) {
string &cur(wordsToPrint[i]);
// get the adjacency list for current word. I'm passing wordList and v because your
// function requires them, but notice that this print algo doesn't need them at all,
// so you should consider getting rid of them in exchange().
vector<node*> wordList;
vector<vector<node*> > v;
vector<string> adjacent = exchange(cur, ht, wordList, v);
// now do the print adjacent words. If you find one that isn't in your list of words
// to print then add it.
if (adjacent.size()) {
cout << cur << " :";
for (int j = 0; j<adjacent.size(); ++j) {
cout << ' ' << adjacent[j];
// If the adjacent word isn't in our list or words to print,
// then add it to the end.
vector<string>::iterator it;
it = find(wordsToPrint.begin(), wordsToPrint.end(), adjacent[j]);
if (it != wordsToPrint.end()) {
wordsToPrint.push_back(adjacent[j]);
}
}
cout << '\n';
}
}
}
|