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

Recursion returning function without returning in special cases

本文关键字:返回 在特殊情况下 函数 递归      更新时间:2023-10-16

我需要通过蛮力组合给定的密码。所以我决定使用递归函数来返回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()但它们不一样时。我只需要退出递归的最后一步,没有任何返回值。有什么办法吗?

假设密码不为空(否则使用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 "";
}
我不知道

这在您的特定情况下是否会对您有所帮助:

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 passwordn";
    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。我选择使用 while 循环而不是来自[32,126]的 for 循环,而是使用static int计数器。我最初将此值设置为 31,并在<= 126时检查此循环。然后在循环中,我pre-increment static计数器,然后显示forcedPassword值以进行调试。最后,我返回这个递归函数,将原始未修改的enteredPasswordforcedPassword一起传递,同时使用 char(j) 更新它。对于有no operations to be made的控制路径,我只是简单地返回"";

按照评论中的建议使用布尔函数,您可以这样做

#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);
}