简化长代码行

时间:2013-10-21 08:01:38

标签: c++

此代码用于列出字符串(Kh,6c,5h等)中的卡片组(从51开始为0),反之亦然。

我已为它编写代码,但似乎很长。有没有更有效的方法来写这个?

我也想这样做,将一个字符串发送到一个函数并获得一个int。

std::string Card::getString(int card) {
    std::string cardstring;

    switch (card) {

    case 0:
        return "2c";
    case 1:
        return "3c";
    case 2:
        return "4c";
    case 3:
        return "5c";
    case 4:
        return "6c";
    case 5:
        return "7c";
    case 6:
        return "8c";
    case 7:
        return "9c";
    case 8:
        return "Tc";
    case 9:
        return "Jc";
    case 10:
        return "Qc";
    case 11:
        return "Kc";
    case 12:
        return "Ac";
    case 13:
        return "2d";
    case 14:
        return "3d";
    case 15:
        return "4d";
    case 16:
        return "5d";
    case 17:
        return "6d";
    case 18:
        return "7d";
    case 19:
        return "8d";
    case 20:
        return "9d";
    case 21:
        return "Td";
    case 22:
        return "Jd";
    case 23:
        return "Qd";
    case 24:
        return "Kd";
    case 25:
        return "Ad";
    case 26:
        return "2h";
    case 27:
        return "3h";
    case 28:
        return "4h";
    case 29:
        return "5h";
    case 30:
        return "6h";
    case 31:
        return "7h";
    case 32:
        return "8h";
    case 33:
        return "9h";
    case 34:
        return "Th";
    case 35:
        return "Jh";
    case 36:
        return "Qh";
    case 37:
        return "Kh";
    case 38:
        return "Ah";
    case 39:
        return "2s";
    case 40:
        return "3s";
    case 41:
        return "4s";
    case 42:
        return "5s";
    case 43:
        return "6s";
    case 44:
        return "7s";
    case 45:
        return "8s";
    case 46:
        return "9s";
    case 47:
        return "Ts";
    case 48:
        return "Js";
    case 49:
        return "Qs";
    case 50:
        return "Ks";
    case 51:
        return "As";
    }
    return cardstring;}

感谢

3 个答案:

答案 0 :(得分:17)

std::string get_card_string(int card)
{
    if (card >= 0 && card < 52)
    {
        std::string s(2,' ');
        s[0] = "23456789TJQKA"[card % 13];
        s[1] = "cdhs"[card / 13];
        return s;
    }
    return "";
}

反向过程稍微复杂一些。如果我想了一会儿,我可能会想出一个更聪明的方法,但显而易见的选择是这样的:

std::unordered_map<std::string, int> initialize_card_map()
{
    std::unordered_map<std::string, int> m;
    for (int i=0; i<52; ++i)
        m[get_card_string(i)] = i;
    return m;
}

int get_card_number(std::string const & card_string)
{
    static std::unordered_map<std::string, int> const m = initialize_card_map();
    auto it = m.find(card_string);
    if (it != m.end())
        return it->second;

    return ??? value not found
}

答案 1 :(得分:3)

使用std::arraystd::vector

std::vector<std::string> cards{
   "2c",  // index 0
   "3c",  // index 1 
   "4c"...
};

std::string Card::getString(int card) { return cards[card]; }

assert(getString(0) == "2c"); 

答案 2 :(得分:1)

本杰明林德利的上述答案很棒,但如果您首先编写原始帖子的代码,则需要向自己提出更多有关您的设计选择的问题:

为什么你必须通过int访问卡值?

表示卡片值的元组怎么样?

Peter Norvig&#34;计算机程序设计&#34;在Udacity.com上的第一堂课/讲座似乎与你的关系非常重要。我建议看看它。