0

所以我正在尝试制作一个蛮力字符串生成器来匹配和比较CUDA中的字符串。在我开始尝试弄乱一门语言之前,我不知道我想让一个在 C++ 中工作。我目前有这个代码。

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;


int sLength = 0;
int count = 0;
int charReset = 0;
int stop = 0;
int maxValue = 0;
string inString = "";
static const char charSet[] = //define character set to draw from
"0123456789"
"!@#$%^&*"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int stringLength = sizeof(charSet) - 1;


char genChars()
{
        return charSet[count]; //Get character and send to genChars()
}

int main()
{
    cout << "Length of string to match?" << endl;
    cin >> sLength;
    cout << "What string do you want to match?" << endl;
    cin >> inString;
    string sMatch(sLength, ' ');
    while(true)
    {
        for (int y = 0; y < sLength; y++)
        {
            sMatch[y] = genChars(); //get the characters
            cout << sMatch[y];

            if (count == 74)
            {
                charReset + 1;
                count = 0;
            }
            if (count == 2147000000)
            {
                count == 0;
                maxValue++;
            }
        }
        count++;
        if (sMatch == inString) //check for string match
        {
            cout << endl;
            cout << "It took " << count + (charReset * 74) + (maxValue*2147000000) << " randomly generated characters to match the strings." << endl;
            cin >> stop;
        }
        cout << endl;
    }
}

现在这段代码运行和编译,但它并没有完全按照我的意愿去做。它将执行 4 个相同的字符,EX。aaaa 或 1111,然后继续下一个,而不像 aaab 或 1112 那样递增。我试过搞乱这样的事情

for (int x = 0; x < sLength; x++)
{
    return charSet[count-sLength+x];
}

在我看来,这应该有效,但无济于事。

4

1 回答 1

3

您基本上只需要增加一个计数器,而不是将计数转换为基数(char 数组的大小)

这是一个以 16 为底的普通数字的示例。

http://www.daniweb.com/code/snippet217243.html

你应该可以更换

   char NUMS[] = "0123456789ABCDEF";

用你的角色集,然后从那里弄清楚。这可能无法使用 uint 生成足够大的字符串,但您应该能够从那里将其分成块。

假设您的字符数组是“BAR”,因此您希望使用自己的符号而不是 0 1 和 2 转换为以 3 为底的数字。

这样做是执行模数来确定字符,然后除以基数,直到数字变为零。相反,您要做的是重复“B”,直到达到您的字符串长度,而不是在您达到零时停止。

例如:从数字 13 生成的四字符字符串:

  • 14%3 = 2,所以它会将 charSet[2] 推到空字符串“R”的开头;
  • 然后它将除以 3,使用整数数学将 = 4。4%3 再次为 1,因此为“A”。
  • 它会再次除以 3,(1) 1%3 是 1,所以“A”。
  • 它将再次除以 3,(0) -- 示例将在这里停止,但是由于我们正在生成一个字符串,我们继续推动 0 "B" 直到我们达到 4 我们的 4 个字符。

最终输出:BAAR

对于可以生成更大字符串的方法,您可以使用字符串大小的整数数组(调用它positions),将所有整数初始化为零并在每次迭代中执行以下操作:

   i = 0;
   positions[i]++;
   while (positions[i] == base)
   {
     positions[i] = 0;
     positions[++i]++;
   }

然后您将遍历整个数组,并使用 charSet[positions[i]] 构建字符串以确定每个字符是什么。

于 2011-02-26T04:43:26.497 回答