递归返回函数,在特殊情况下不返回

时间:2018-01-19 17:59:02

标签: c++ recursion

我需要通过bruteforce结合给定的密码。所以我决定使用返回string

的递归函数
string bruteforce(string password, string forcedPassword)
{
    if (password.length() == forcedPassword.length())
    {
        if (password == forcedPassword)
        {
            return forcedPassword;
        }
        // What can I do here to return nothing and continue from the previous step?
    }

    for (int j = 32; j <= 126; j++)
    {
        forcedPassword += char(j);
        bruteforce(password, forcedPassword);
    }
}

int main()
{
   ...
   cin >> password;
   cout << bruteforce(password, "");
   ...
}

问题是当我得到password.length() == forcedPassword.length()但它们不一样时。我只需要退出递归的最后一步而不返回任何值。有没有办法实现呢?

3 个答案:

答案 0 :(得分:0)

假设非空密码(否则使用optional),您可以写:

std::string bruteforce(const std::string& password, const std::string& forcedPassword)
{
    if (password.length() == forcedPassword.length())
    {
        if (password == forcedPassword)
        {
            return forcedPassword;
        }
        return "";
    }

    for (int j = 32; j <= 126; j++)
    {
        auto res = bruteforce(password, forcedPassword + char(j));
        if (!res.empty()) {
            return res;
        }
    }
    return "";
}

答案 1 :(得分:0)

我不知道在您的具体情况下这是否会对您有所帮助:

std::string bruteforce( const std::string& enteredPassword, std::string& forcedPassword ) {

    if ( enteredPassword.length() == forcedPassword.length() ) {
        if ( enteredPassword == forcedPassword ) {
            return forcedPassword;
        }    
        return "";
    }    

    static int j = 31;
    while ( j <= 126 ) {
        ++j;
        // Display forcedPassword to test it for each recursive iteration
        std::cout << forcedPassword << "\n";
        return bruteforce( enteredPassword, forcedPassword + char( j ) );    
    }    
    return "";      
}

int main() {    
    std::string password;
    std::string forcedPassword( "Hello" );
    std::cout << "Enter the password\n";
    std::cin >> password;

    std::cout << bruteforce( password, forcedPassword );

    std::cout << "\Press any key and enter to quit.\n";
    char q;
    std::cin >> q;
    return 0;
}

但是这确实可以编译,构建和运行而没有错误,并且这里没有堆栈溢出。

我将enteredPassword设为const ref,因为此功能不会对其进行修改。对于forcedPassword,因为您显示您正在连接到此字符串,所以我选择将其设置为将被修改的non const reference。我选择使用[32,126]计数器来使用while循环,而不是来自static int的for循环。我最初将此值设置为31,并在<= 126时检查此循环。然后在循环I pre-increment static计数器中,然后我显示forcedPassword值以进行调试。最后,我将这个递归函数与enteredPassword一起传递给原始的非修改版forcedPassword,同时用char(j)更新它。对于有no operations to be made的控制路径,我只是刚刚返回"";

答案 2 :(得分:0)

使用评论中建议的bool功能

#include <iostream>
using namespace std;

bool bruteforce(const string& password, string& forcedPassword, unsigned int length=0)
{
    if (password.length() == length)
    {
        if (password == forcedPassword)
        {
            return true;
        }
        return false;
    }

    for (int j = 32; j <= 126; ++j)
    {
        forcedPassword[length] = char(j);
        if ((bruteforce(password, forcedPassword, length+1)))
            return true;
    }
}

std::string findpassword(const string& password) {
    std::string forcedPassword = "";
    while (forcedPassword.length() < password.length())
        forcedPassword.push_back('a');
    if (bruteforce(password, forcedPassword))
        return forcedPassword;
    else
        return "failed";
}

int main()
{
   std::string password;
   cin >> password;
   cout << findpassword(password);
}