【发布时间】:2018-05-18 16:45:30
【问题描述】:
我正在尝试使用递归并不断收到溢出错误。我不知道该怎么办。
我想用所有字谜来提供我的功能之一,但我不确定如何。我不能把所有的东西都放在一个函数中,所以我做了两个:一个是生成字谜,一个是在字典中搜索以找到所有匹配项。但它不起作用,我不知道还能做什么。在我的 anagram 函数中,我想返回我的排列函数,但我不能不返回一个字符串。我需要它什么都不返回并返回函数。
#include <iostream>
#include <fstream>
#include <istream>
#include <string>
using namespace std;
const int MAXRESULTS = 20; // Max matches that can be found
const int MAXDICTWORDS = 30000; // Max words that can be read in
const int MAXPERMUTATIONS = 720; //enough permutations for a 6 letter word
//Beginning of assignment functions
//Read Dictionary function that uses recursion
int readDictionary(istream &, string[]);
//Permuation function
int recursivePermute(string, const string[], int, string[]);
//Print function
void recurPrint(const string[], int);
//swap characters in a string
void swap(string*, string*);
//permutation function
string Permutator(string, int, int, int);
//
//End of Assignment functions
int main()
{
string Permutations[MAXPERMUTATIONS];
string results[MAXRESULTS];
string dict[MAXDICTWORDS];
ifstream dictfile; // file containing the list of words
int nwords; // number of words read from dictionary
string word;
dictfile.open("words.txt");
if (!dictfile) {
cout << "File not found!" << endl;
return (1);
}
nwords = readDictionary(dictfile, dict);
cout << "Please enter a string for an anagram: ";
cin >> word;
//Make all the permutations and store them in an array
int numMatches = recursivePermute(word, dict, nwords, results);
if (numMatches == 0)
cout << "No matches found" << endl;
else
recurPrint(results, numMatches);
}
/***************************************************************************************************
Name: readDictionary
input: ifstream reference, string array
Description: This function returns the number of words added into the array from the dictionary.
****************************************************************************************************/
int readDictionary(istream &file, string DicArr[])
{
int counter = 0;
if (counter > MAXDICTWORDS)
return counter;
if (getline(file, DicArr[0]))
{
counter++;
return counter += readDictionary(file, DicArr + 1);
}
else
return counter;
}
/*****************************************************************************************************
Name: recursivePermute
Input: string, const string array, int, string array
Description: 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.
*******************************************************************************************************/
int recursivePermute(string word, const string dict[], int size, string results[])
{
//count to iterate through the dictionary array and keep in bounds
//numresults to keep track of the number of results
int numResults = 0;
//if statement to if the number of results goes over the limit
if (numResults > MAXRESULTS)
return numResults;
if (size == 0)
return numResults;
//if there is a match check the dictionary
if (word == dict[0])
{
results[0] = word;
numResults++;
}
numResults += recursivePermute(Permutator(word, 0, word.length() - 1, 0), dict + 1, size - 1, results);
return numResults;
}
/*******************************************************************************************************
Name: recurPrint
Input:const string array, int
Description: Prints out the results
*********************************************************************************************************/
void recurPrint(const string results[], int size)
{
if (size == 1)
{
cout << "matching word \"" << results[0] << "\" found!\n" << endl;
return;
}
if (size == 0)
return;
cout << results[size - 1];
recurPrint(results, size - 1);
}
/****************************************************************************************************
name: swap
input: string pointer
description: This functions swaps two characters in a string
*****************************************************************************************************/
void swap(string* a, string* b)
{
string temp;
temp = *a;
*a = *b;
*b = temp;
}
/******************************************************************************************************
********************************************************************************************************/
string Permutator(string word, int beg, int end, int count)
{
string a;
if (count == end)
return word;
if (beg == end)
return;
if(count <= end)
{
swap(word[beg], word[count]);
Permutator(word, beg + 1, end, count);
swap(word[beg], word[count]);
Permutator(word, beg, end, count + 1);
}
}
/******************************************************************************************************
*******************************************************************************************************/
好的,所以我已经缩小了我的问题范围。我遇到了这个函数的问题,我用它来将每个排列输入到我的另一个函数中,该函数将对照作为字符串数组的字典检查每个排列。
string Permutator(string word, int beg, int end, int count)
{
if (count == end)
return word;
if (beg == end)
if (count <= end)
{
swap(word[beg], word[count]);
Permutator(word, beg + 1, end, count);
swap(word[beg], word[count]);
}
}
如果它是无效的,这会起作用,但我需要它来返回一个字符串,但是当我更改返回类型时,我的整个算法就会出错。在此分配中只能是递归的循环不能按预期工作。我没有想法,不知道我还能做什么。
【问题讨论】: