c++ 这算递归吗?

d6kp6zgx  于 2023-02-20  发布在  其他
关注(0)|答案(5)|浏览(159)

我正在学习递归,我必须稍后将它应用到一个项目中。我的项目将包括一副纸牌,我只是好奇这是否算递归?对我来说,它似乎是因为它调用自己。然而,由于我仍在学习,我想确保我理解的概念,我没有错。请让我知道。

string Card::suitName(Suit S) {
    string suits;

    switch (S) {
        case clubs:
            suits = "Clubs";
            break;
        case diamonds:
            suits = "Diamonds";
            break;
        case hearts:
            suits = "Hearts";
            break;
        case spades:
            suits = "Spades";
            break;
    }
    return (suits);
}

// declare the value of the cards
string Card::cardValue(Value Card) {
    string card;

    switch (Card) {
        case two:
            card = "Two";
            break;
        case three:
            card = "Three";
            break;
        case four:
            card = "Four";
            break;
        case five:
            card = "Five";
            break;
        case six:
            card = "Six";
            break;
        case seven:
            card = "Seven";
            break;
        case eight:
            card = "Eight";
            break;
        case nine:
            card = "Nine";
            break;
        case ten:
            card = "Ten";
            break;
        case jack:
            card = "Jack";
            break;
        case queen:
            card = "Queen";
            break;
        case king:
            card = "King";
            break;
        case ace:
            card = "Ace";
            break;
    }
    return (card);
}
ryhaxcpt

ryhaxcpt1#

您已经定义了两个函数Card::suitName()Card::cardValue()。这两个函数都不调用自身或任何其他用户定义的函数(不包括任何std::string函数)。代码中没有递归。

mec1mxoz

mec1mxoz2#

递归被定义为一个函数在它自己内部调用它自己,所以suitName需要调用suitName或者cardValue调用cardValue.,都不做这样的调用,所以

您的代码中没有递归

vbopmzt1

vbopmzt13#

递归要求函数调用自身(最常见的是直接调用,但也可以通过另一个函数调用)。
示例:

bool doesHandContainAceRecursion(std::vector<Card> const& playersCards, int pos) {
    // If we have tried all the cards and gone past the end
    // Then return false.
    //
    // This is a classic part of recursion, checking if you have reached the end.
    if (pos >= playersCards.size()) {
        return false;
    }

    // Otherwise do the work you want to do.
    if (playersCards[pos].value == 1) {
        return true;
    }

    // This is the recursive call.
    // It calls itself. This is a classic example of tail recursion.
    return doesHandContainAceRecursion(playersCards, pos + 1);
}

// This is the function you call and it sets up a call to the
// recursive function above.
bool doesHandContainAce(std::vector<Card> const& playersCards) {
    return doesHandContainAceRecursion(playersCards, 0);
}
ycl3bljg

ycl3bljg4#

不,这不是递归。递归是方法调用自身的时候
示例:

string Card::suitName(Suit S) {
    string suits;

    switch (S) {
        case clubs:
            suitName(diamonds); // Recursions
            break;
        case diamonds:
            suits = "Diamonds";
            break;
    }
    return (suits);
}
wgmfuz8q

wgmfuz8q5#

否,您提供的代码不涉及递归。递归是一种编程技术,函数通过直接或间接调用自身来解决问题。在您提供的代码中,没有引用函数自身的函数调用。
函数suitName和cardValue只是接受一个输入,然后根据switch语句返回相应的输出,它们不涉及任何类型的迭代或递归过程来获得输出。
在一副牌的上下文中,您可以考虑将递归用于 Shuffle 的函数,因为 Shuffle 涉及到重复交换成对的牌,直到这副牌被随机化。但是,您提供的代码不涉及递归。

相关问题