C++ C+的字谜发生器+;(不使用STL)

C++ C+的字谜发生器+;(不使用STL),c++,string,fstream,anagram,C++,String,Fstream,Anagram,我正在尝试使用一种非常基本的程序方法创建一个字谜解算器。我发现我可能应该使用类来完成这项工作,但现在已经太晚了,我的作业即将到期。任何关于如何解决这个问题的建议都将非常好 基本上,这就是算法应该做的: 把所有的单词都记在字典里;把它们放在一个容器里 从用户那里得到一个单词;适当时退出 获取用户输入的单词的所有排列 从排列中删除用户输入的单词 去掉排列集合中我在第1部分中收集的词典中没有的所有单词 现在进入最后一步,我必须确保不显示重复的字谜(即包含相同字母的字谜,如“loop”)。我似乎无法使这

我正在尝试使用一种非常基本的程序方法创建一个字谜解算器。我发现我可能应该使用类来完成这项工作,但现在已经太晚了,我的作业即将到期。任何关于如何解决这个问题的建议都将非常好

基本上,这就是算法应该做的:

  • 把所有的单词都记在字典里;把它们放在一个容器里
  • 从用户那里得到一个单词;适当时退出
  • 获取用户输入的单词的所有排列
  • 从排列中删除用户输入的单词
  • 去掉排列集合中我在第1部分中收集的词典中没有的所有单词
  • 现在进入最后一步,我必须确保不显示重复的字谜(即包含相同字母的字谜,如“loop”)。我似乎无法使这张支票生效,这在下面的TODO注释块下有注释

    任何建议都会很棒

    #include <iostream>
    #include <fstream>
    #include <string>
    
    //
    // Change size below to accomodate more anagrams and dictionary words
    //
    #define MAX_ANGM_SIZE  4096
    #define MAX_WORD_SIZE  1048576
    
    using namespace std;
    
    
    //
    // Determines whether anagram is valid or not; will not display word
    // which user entered or words not contained in dictionary
    //
    bool isValidAnagram(string word, string userWord,
                    string dictionary[], unsigned int listIdx)
    {
        for(unsigned int idx = 0; idx < listIdx; ++idx)
        {
            if(word == userWord)
                return false;
            else if (word == dictionary[idx])
                return true;
        }
    
        return false;
    }
    
    
    //
    // Determines whether user's word is contained in the dictionary
    // or not
    //
    bool isValidWord(string word, string dictionary[], 
                 unsigned int listIdx)
    {
        for(unsigned int idx = 0; idx < listIdx; ++idx)
        {
            if(word == dictionary[idx])
                return true;
        }
    
        return false;
    }
    
    
    //
    // TODO:This function should test for duplicate anagrams and return
    // true if duplicates are found.
    //
    bool isRepeated(string anagrams[], unsigned int anaIdx)
    {
        for(unsigned int idx = anaIdx; idx != 0; --idx)
        {
            if(anagrams[idx] == anagrams[anaIdx])
                return true;
            else 
                return false;
        }
    
        return false;
    }
    
    
    //
    // Only display elements in array which aren't blank and don't 
    // display duplicate anagrams; notify user if no anagrams
    // were found.
    //
    void displayAnagrams(string anagrams[], unsigned int next)
    {
        int flag = 0;
    
        for (unsigned int idx = 0; idx < next; ++idx)
        {
    
            if((anagrams[idx] != "") || (!(isRepeated(anagrams, idx))))
            {
                if(idx == 1)
                    cout << "  Anagrams: ";
                if(idx > 0)
                    flag = 1;
    
                cout << anagrams[idx] << " ";
            }
            else 
                continue;
        }
    
        if(flag == 0)
            cout << "  no anagrams found" << endl;
    }
    
    
    static void swap(char &c1, char &c2)
    {
        char temp = c1;
    
        c1 = c2;
        c2 = temp;
    }
    
    
    //
    // Pass in word to be altered, the userWord for comparison, the array to store
    // anagrams, the dictionary for comparison, the count for the number of anagrams
    // and the count for number of dictionary words
    //
    static void permute(string word, string userWord, int k, string anagrams[],
                    string dictionary[], unsigned int &next, unsigned    int listIdx)
    {   
        if(k == word.length()-1)
        {
            if(isValidAnagram(word, userWord, dictionary, listIdx))
                anagrams[next] = word;
    
            ++next;
        }
        else
        {
            for(int idx = k; idx < word.length(); ++idx)
            {
                swap(word[k], word[idx]);
                permute(word, userWord, k+1, anagrams, dictionary, next, listIdx);
            }
        }
    }
    
    
    //
    // Create container to store anagrams, validate user's word in dictionary, get all
    // of the anagrams, then display all valid anagrams
    //
    void getAnagrams(string word, string dictionary[], unsigned int listIdx)
    {
        string anagrams[MAX_ANGM_SIZE];
        unsigned int next = 0;
    
        if(isValidWord(word, dictionary, listIdx))
        {
            permute(word, word, 0, anagrams, dictionary, next, listIdx);
        }
        else
        {
            cerr << "  \"" << word << "\"" << " is not a valid word" << endl;
            return;
        }
    
        displayAnagrams(anagrams, next);
    }
    
    
    //
    // Read in dictionary file, store contents of file in a list, prompt
    // the user to type in words to generate anagrams
    //
    int main()
    {
        string file;
        string word;
        string quit = "quit";
        string dictionary[MAX_WORD_SIZE];
    
        unsigned int idx = 0;
    
        cout << "Enter a dictionary file: ";
        cin  >> file;
        cout << "Reading file \"" << file << "\"" << endl;
        cout << endl;
    
        ifstream inFile(file.c_str());
    
            if(!(inFile.is_open())) 
        {
            cerr << "Can't open file \"" << file << "\""
             << endl;
    
            exit(EXIT_FAILURE);
        }
    
        while(!inFile.eof())
        {
            inFile >> dictionary[idx];
            ++idx;
        }
    
        inFile.close();
    
        while(true)
        {
            cout << "Enter a word: ";
            cin  >> word;
    
            if(word == quit) break;
    
            getAnagrams(word, dictionary, idx);
    
            cout << endl;
        }
    
        return 0;
    }
    
    #包括
    #包括
    #包括
    //
    //更改下面的大小以容纳更多的字谜和字典单词
    //
    #定义最大尺寸4096
    #定义最大单词大小1048576
    使用名称空间std;
    //
    //确定字谜是否有效;不会显示word
    //哪些用户输入了词典中未包含的单词
    //
    bool isValidAnagram(字符串字、字符串用户字、,
    字符串字典[],无符号整数列表IDX)
    {
    for(无符号整数idx=0;idxcout您可能需要重新考虑第(3)步。如果用户输入一个12个字母的单词,您有479001600个排列,这可能不可能一次将所有单词组合在一起(如果不是,那么16个字母的单词将是…)

    相反,试着想想如何存储单词,并以一种不需要您这样做的方式查找潜在的字谜


    编辑:我知道,在这一点上,解决大词的能力可能不是你最关心的问题,但如果你通过组合一组有效词,而不是从所有可能的词开始,删除所有不匹配的词,那么实际上可能会使你的第四步和第五步更容易。从数组中删除一个项有点麻烦d由于您必须将以下所有项目重新排列以填补空白(这正是STL为您管理的类型)。

    您可能需要重新考虑您的步骤(3)。如果用户输入一个12个字母的单词,您将有479001600个排列,这可能不可能一次全部组合起来(如果不是,那么一个16个字母的单词将是…)

    相反,试着想想如何存储单词,并以一种不需要您这样做的方式查找潜在的字谜


    编辑:我知道,在这一点上,解决大词的能力可能不是你最关心的问题,但如果你通过组合一组有效词,而不是从所有可能的词开始,删除所有不匹配的词,那么实际上可能会使你的第四步和第五步更容易。从数组中删除一个项有点麻烦d因为您必须将以下所有项重新排列以填补空白(这正是STL为您管理的类型)。

    更好的算法:不存储单词,而是存储一个包含(您的单词,已排序字母)的元组。此外,您可以按第二个键对大的存储进行排序(提示,您可以使用sqlite数据库来完成这项工作,并使用索引(不能是唯一的!))

    例如储存

    "Florent", "Abraham","Zoe"
    
    你会储存在记忆中

    ("aaabhmr", "abraham"),("eflnort","florent"),("eoz","zoe")
    
    当你从用户那里得到你的单词时,你只需要使用相同的“单词内部字母排序”算法

    然后在存储中查找该模式,并在排序时很快找到所有的字谜(
    log(字典大小)
    ),当然,原始单词是元组的第二个元素


    您可以使用类、标准结构、数据库来实现这一点,这取决于您选择最简单的实现(以及适合您的需求的实现)

    更好的算法:不存储单词,而是存储包含(单词、已排序字母)的元组。此外,您还可以按第二个键对大的存储进行排序(提示,您可以使用sqlite数据库来完成这项工作,并使用索引(不能是唯一的!))

    例如储存

    "Florent", "Abraham","Zoe"
    
    你会储存在记忆中

    ("aaabhmr", "abraham"),("eflnort","florent"),("eoz","zoe")
    
    当你从用户那里得到你的单词时,你只需要使用相同的“单词内部字母排序”算法

    然后在存储中查找该模式,并在排序时很快找到所有的字谜(
    log(字典大小)
    ),当然,原始单词是元组的第二个元素

    你可以用类来做,standar