Question: I have a problems for copier function please help me for copier function, This program is creating word scrabble int copier(string target, const vector dict,
I have a problems for copier function please help me for copier function, This program is creating word scrabble
int copier(string target, const vector
{
if(dict.size() == 0)
return 0;
if(target == *dict)
{
if(CopyChkr(target, results, 0, count)) //It is checker whereas result takes value or not.
return 0;
else
{
*AssignedResult = target;
return (1 + copier(target, dict + 1, size - 1, results, AssignedResult + 1, count));
}
}
else
return copier(target, dict + 1, size - 1, results, AssignedResult, count);
}
#include
#include
#include
#include
#include
#include
using namespace std;
const int MAXRESULTS = 20; // Max matches that can be found
const int MAXDICTWORDS = 30000; // Max words that can be read in
//loadDictionary(istream &dictfile, vector
int loadDictionary(istream &dictfile, vector
{
string line;
if(getline(dictfile, line))
{
dict.push_back(line);
//return dict.at(dict.size() + 1) + loadDictionary(dictfile, dict.push_back(line));
return static_cast
}
else
return 0;
}
//Places each string in dictfile into the array dict. Returns the number of words read into dict.
//This number should not be larger than MAXDICTWORDS since that is the size of the array.
bool CopyChkr(string target, vector
{
if(start >= max)
return false;
if(target == results[start])
return true;
else
return CopyChkr(target, results, start + 1, max);
}
// the function helps assigning target into *AssignedResult ( it is &results[count])
int copier(string target, const vector
{
if(dict.size() == 0)
return 0;
if(target == *dict)
{
if(CopyChkr(target, results, 0, count)) //It is checker whereas result takes value or not.
return 0;
else
{
*AssignedResult = target;
return (1 + copier(target, dict + 1, size - 1, results, AssignedResult + 1, count));
}
}
else
return copier(target, dict + 1, size - 1, results, AssignedResult, count);
}
void otherWord_Loop(int i, string otherWord, string rest, const vector
void printPermutation(string otherWord, string theWord, const vector
{
if (theWord.length() == 0)
{
cout <<"The possible word : "<< otherWord << endl;
// The results[count] is specific data in the result sequence
// it takes otherWord which is a possible word consist of letters from input word
count += copier(otherWord, dict, results, &results[count], count);
}
else
{
//cout << " check other word " << otherWord << " " << theWord.length() << endl;
otherWord_Loop(0, otherWord, theWord, dict, results, count);
//Using substr, it automatically delete a letter[index] in "theWord".
}
}
void otherWord_Loop(int i, string otherWord, string theWord, const vector
{
if(i >= otherWord.size())
return;
for(i=0;i { //cout<<"check " << otherWord << " "<< theWord[i] << " chk "< printPermutation(otherWord + theWord[i], theWord.substr(0, i) + theWord.substr(i+1), dict, results, count); //As increasing i,they add later order letters to the otherWord. //substr(i) peeks letter of index. // } } //permute(string word, vector int permute(string word, vector { int count = 0; //otherWord is assigned that "" printPermutation("", word, dict, results, count); return count; } // Places all the permutations of word, which are found in dict into results. // Returns the number of matched words found. // This number should not be larger than MAXRESULTS since that is the size of the array. // The size is the number of words inside the dict array. void recurPrint(const vector { if(results.size() == 0) { return; } else { for(int i =0; i< results.size(); i++) { cout << "Matching word " << results[i] << endl; } cout << "The number of matched words : "<< results.size() << endl; } } // Prints size number of strings from results. // The results can be printed in any order, which are found in dict into results int main() { vector vector ifstream dictfile; // file containing the list of words int nwords; // number of words read from dictionary string word; dictfile.open("/Users/jinhanhan/Documents/jinh/scrabble/words.txt"); if (!dictfile) { cout << "File not found!" << endl; return (1); } nwords = loadDictionary(dictfile, dict); //string exampleDict[] = {"kool", "moe", "dee"}; //int numResults = recursivePermute("look", exampleDict, 3, results); //assert(numResults == 1 && results[0] == "kool"); cout << "Please enter a string for an anagram: "; cin >> word; //permute(string word, vector //numMatches = permute(word, dict, nwords, results); int numMatches = permute(word, dict, results); if (!numMatches) cout << "No matches found" << endl; else recurPrint(results); //recurPrint(results, numMatches); dictfile.close(); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
