【问题标题】:C++ Anagram Maker ErrorC++ Anagram Maker 错误
【发布时间】:2013-10-09 04:49:12
【问题描述】:

我一直在尝试制作一个字谜生成器,使用文本框(加密文本)作为输入,文本为“Hello World”,输出文本框(加密文本)接收文本:

“嗯哼
WWWWWWooooooorrrrrrrlllllllllllddddddddddd"。

我还有一个名为“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);

【问题讨论】:

  • 你得到了什么输出,你期望什么输出?我还没有开始看代码,因为问题不清楚。
  • 我原以为这个词会混淆(字谜),例如Hello World = lelo oWrHdl

标签: c++ string for-loop anagram


【解决方案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;
            }
        }
    }
}

【讨论】:

    【解决方案2】:
            #include <algorithm> 
    

    .

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

    【讨论】:

      猜你喜欢
      • 2012-02-10
      • 2016-09-05
      • 1970-01-01
      • 1970-01-01
      • 2020-06-25
      • 2012-12-04
      • 1970-01-01
      • 2011-10-27
      • 2023-03-22
      相关资源
      最近更新 更多