0

我一直在尝试制作一个字谜制造商,使用文本框(encryption_text)作为输入,文本是“Hello World”,输出文本框(encrypted_text)接收文本:

“嗯嗯嗯
嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯嗯”。

我还有一个名为“anag_used”的文本框,它应该在要加密的字符串中记录使用的数字/位置。

我是否过于复杂了,还是有错误?

谢谢 :)

这是我的代码:

void anagram()
         {
             string toanagram = marshal_as<string>(encryption_text->Text);
             string out;
             int k;
             System::String^ rndstr;
             System::String^ ktostr;
             ostringstream kstr;
             anag_used->Clear();
             for (int i = 0; i < toanagram.size(); ++i)
             {
                anag_used->Text += "\n";
                int rnd = 0 + rand() % toanagram.size();
                ostringstream rndtostr;
                rndtostr << rnd;
                rndstr = gcnew System::String(rndtostr.str().c_str());
                for (int l = 0; l < i; ++l)
                {
                if (anag_used->Lines[l] == rndstr)
                {
                    k = rnd;
                    kstr << k;
                    ktostr = gcnew System::String(kstr.str().c_str());
                    for (System::String^ j = anag_used->Lines[l]; j == ktostr; k = 0 + rand() % toanagram.size())
                    {
                        kstr << k;
                        ktostr = gcnew System::String(kstr.str().c_str());
                        if (anag_used->Lines[l] == ktostr)
                        {
                            //Do someting if you want
                        }
                        else
                        {
                            out += toanagram[k];
                            anag_used->Lines[l] = ktostr;
                        }
                    }
                }
                else
                {
                    out += toanagram[i];
                    anag_used->Lines[i] = rndstr;
                }
                }
             }
             encrypted_text->Text = marshal_as<System::String^>(out);
         }

编辑:找到了一个更简单的工作代码

#include <algorithm>

.

            string toanagram = marshal_as<string>(encryption_text->Text);
            sort(toanagram.begin(), toanagram.end());
            encrypted_text->Text = marshal_as<System::String^>(toanagram);
4

2 回答 2

1

这适用于控制台,但您可以C++/CLI很容易地实现它

#include <iostream>
#include <sstream>
#include <vector>
#include <ctime>

void str_vect(std::vector<const char>* v, std::string& s)
{
    for (int i = 0; i < s.length(); ++i)
    {
        v->push_back(s[i]);
    }
}

int main()
{
    for (;;)
    {
        std::cout << "Please enter the word / phrase\n";
        std::string word;
        std::getline(std::cin, word);
        std::vector<const char> word_split;
        str_vect(&word_split, word);
        int sz = word_split.size();
        std::string anagram;
        for (int i = 0; i < sz; ++i)
        {
            srand(time(NULL));
            int r = (rand() % (word_split.size() - 0)) + 0;
            anagram += word_split[r];
            word_split.erase((word_split.begin()) + r);
        }
        system("cls");
        std::cout << "Please guess the anagrammed phrase / word - '" << anagram << "'\n";
        int max_tries = 3;
        int tries = max_tries;
        for (int i = 0; i <= max_tries; ++i)
        {
            std::string guess;
            std::getline(std::cin, guess);
            if (guess != word)
            {
                tries--;
                if (tries == 0)
                {
                    std::cout << "You have ran out of tries. The answer was: " << word << "\n";
                    break;
                }
                std::cout << tries << ((tries == 1) ? " try" : " tries") << " left\n";
            }
            else
            {
                std::cout << "Correct!\n";
                break;
            }
        }
    }
}
于 2014-01-19T17:38:34.183 回答
0
        #include <algorithm> 

.

        string toanagram = marshal_as<string>(encryption_text->Text);
        sort(toanagram.begin(), toanagram.end());
        encrypted_text->Text = marshal_as<System::String^>(toanagram);
于 2013-10-02T18:09:55.917 回答