C ++从最大到最小的数组编号

时间:2013-09-02 18:58:51

标签: c++ arrays

我是c ++的初学者程序员。我在一本旧的c ++书中发现了一个非常有趣的练习,它没有这个练习的解决方案,所以我希望你们可以帮助我:

它希望我创建一副52张牌,然后将牌组洗牌并将卡牌交给4名玩家:

这是代码示例:

cout << "Player 1: ";
for (int j=0; j<13; j++)
{
     card[j].display();
     cout << ", ";
}
  

球员1:8,7,3,2,A,K,5,4,Q,9,3,A,2

现在它要我将这些数字从最大到最小排列: A,A,K,Q,9,8,7,5,4,3,3,2,2

在互联网上做过研究之后,我学会了如何找到最大数量的数组,但我仍然不知道如何将这些数字从最大到最小排列。我正在使用GNU编译器。

感谢。

对于那些想要所有代码的人来说,这里是:

#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <ctime>
using namespace std;

enum Suit {clubs, diamonds, hearts, spades};

class deck
{
private:
    int number;
    Suit suit;
public:
    void setcards(int n, Suit s)
        { number = n; suit = s; }

    void display();
};

void deck::display()
{
     if (number >= 2 && number <= 10)
    cout << number;
    else
        switch(number)
    {
        case 11: cout << "J"; break;
        case 12: cout << "Q"; break;
        case 13: cout << "K"; break;
        case 14: cout << "A"; break;
    }
    switch(suit)
    {
        case clubs:    cout << static_cast <char> (5); break;
        case diamonds: cout << static_cast <char> (4); break;
        case hearts:   cout << static_cast <char> (3); break;
        case spades:   cout << static_cast <char> (6); break;
    }
}

class game
{
private:
    int j;
    enum {cardmax = 52};
    deck card[cardmax];
public:
    game()
    {
        for (int j=0; j<cardmax; j++)
        {
            int num = (j % 13) + 2;
            Suit su = Suit(j / 13);
            card[j].setcards(num, su);
        }
    }

    void shuffle()
    {
        char ans;

        cout << "Would you like to shuffle the deck? (y/n): ";
        cin >> ans;
        if (ans == 'y')
        {
            srand(time(NULL));
            for (j=0; j<cardmax; j++)
            {
                int k = rand() % 52;
                deck temp = card[j];
                card[j] = card[k];
                card[k] = temp;
            }
        }
        else if (ans =='n')
            deal();
    }

    void deal();
};

void game::deal()
{
    const char esc = 27;
    const char enter = '\r';
    char ans;

    cout << "Press ESCAPE to exit the game or ENTER to deal: ";
    ans = getche();
    if (ans == esc)
        exit(0);
    else if (ans == enter)
    {
        cout << "\n\nPlayer 1: ";
        for (j=0; j<13; j++)
        {
            card[j].display();
            cout << ", ";
        }
        cout << "\nPlayer 2: ";
        for (j=13; j<26; j++)
        {
            card[j].display();
            cout << ", ";
        }
        cout << "\nPlayer 3: ";
        for (j=26; j<39; j++)
        {
            card[j].display();
            cout << ", ";
        }
        cout << "\nPlayer 4: ";
        for (j=39; j<52; j++)
        {
            card[j].display();
            cout << ", ";
        }
        cout << endl;

        cout << "\nWould you like to deal again? (y/n): ";
        cin >> ans;
        if (ans == 'y')
        {
            shuffle();
            deal();
        }
        else if (ans == 'n')
            exit(0);
    }
}

int main()
{
    game cards;
    cards.shuffle();
    cards.deal();
}

4 个答案:

答案 0 :(得分:3)

首先,您需要在deck类中实现less than运算符,如下所示:

class deck {
    //...
    bool operator < (const deck& other) const {return number < other.number;}
    //...
}

然后我建议在std :: vector

中转换那一组卡片
std::vector<deck> card;
card.resize(cardmax);
//instead of
deck card[cardmax];

然后你可以像这样排序std :: vector

std::sort(card.begin(), card.end());

请注意,为了使用std :: vector类型,您必须在使用它的文件中的某处添加#include <vector>,并且为了使用std :: sort,您必须添加{ {1}}

答案 1 :(得分:1)

查找std::sort

您应该可以将它应用于您的阵列:

   std::sort(&card[0], &card[cardmax]);

如果您想以不同的方式订购卡片,则必须编写一个可以更改订单的功能。

来自http://www.cplusplus.com/reference/algorithm/sort/?kw=sort

comp
    Binary function that accepts two elements in the range as arguments, and returns a value convertible to bool. The value returned indicates whether the element passed as first argument is considered to go before the second in the specific strict weak ordering it defines.
    The function shall not modify any of its arguments.
    This can either be a function pointer or a function object.

示例:

class deck
{
  [...]  // All your existing code.
  public:
    static bool descending_order(const deck& first_card, const deck& second_card)
    {
      return first_card.number > second_card.number;
    }
};

答案 2 :(得分:1)

在我看来,最容易理解的排序算法是bubblesort。

算法如下:(从最大到最小)

首先它比较元素0和1。    如果(元素0小于元素1)那么交换元素 然后它做同样的事情,但是使用元素1和2。

当你到达数组的末尾时,如果已经进行了任何交换,你必须从元素0和1开始,然后再次在数组中工作。

我知道只是阅读它可能很难理解,但HERE是一个youtube初学者c ++视频,应该让它很容易理解。

答案 3 :(得分:0)

这是我如何在没有太多重构的情况下完成的。为了完整的方式,我将拆分提示并“再次处理”循环处理,因此它不是递归的,而是制作逻辑,因此您不需要exit(0)

#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <functional>
#include <iomanip>

//If no c++ 11 you may need to remove the next line
#include <tuple>

using namespace std;

enum Suit {clubs, diamonds, hearts, spades};
const char* CardLetters[] = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
const char SuitLetters[] = { 5, 4, 3, 6 };

class deck
{
private:
    int number;
    Suit suit;
public:
    void setcards(int n, Suit s)
    { 
        number = n; suit = s; 
    }

    void display()
    {
        cout << std::right<< std::setw(2) << CardLetters[number - 2] << SuitLetters[suit];
    }

    bool operator >(const deck& rhs) const
    {
        //If you have c++ 11, this is cleaner
        //return std::tie(number, suit) > std::tie(rhs.number, rhs.suit);
        //otherwise
        if(number == rhs.number)
        {
            return suit > rhs.suit;
        }
        return number > rhs.number;
    }
};

class game
{
private:
    enum {cardmax = 52};
    deck card[cardmax];
public:
    game()
    {
        for (int j=0; j<cardmax; j++)
        {
            int num = (j % 13) + 2;
            Suit su = Suit(j / 13);
            card[j].setcards(num, su);
        }
    }

    void shuffle()
    {
        char ans;

        cout << "Would you like to shuffle the deck? (y/n): ";
        cin >> ans;
        if (ans == 'y')
        {
            std::random_shuffle(&card[0], &card[cardmax]);
        }
        else if (ans =='n')
        {
            deal();
        }
    }

    void deal();
};

void game::deal()
{
    const char esc = 27;
    const char enter = '\r';

    cout << "Press ESCAPE to exit the game or ENTER to deal: ";
    char ans = getche();
    if (ans == esc)
    {
        exit(0);
    }
    else if (ans == enter)
    {
        for(int playernum = 0; playernum < 4; ++playernum)
        {
            const int offset = 13 * playernum;
            std::sort(&card[offset], &card[offset + 13], std::greater<deck>());
            cout << "\n\nPlayer " << (playernum + 1) << ": ";
            for (int j=0; j<13; j++)
            {
                if(j > 0) 
                { 
                    cout << ", ";
                }
                card[offset + j].display();
            }
        }

        cout << "\n\nWould you like to deal again? (y/n): ";
        cin >> ans;
        if (ans == 'y')
        {
            shuffle();
            deal();
        }
        else if (ans == 'n')
        {
            exit(0);
        }
    }
}

int main()
{
    srand(time(NULL));
    game cards;
    cards.shuffle();
    cards.deal();
    return 0;
}