用cpp0.5實現撲克牌排序

alansleep發表於2012-11-30
#include "stdio.h"
#include "time.h"

 #include <vector>
#include <algorithm>
//#include <iostream>
using namespace std;

const char *COLOR[] = {"黑桃","紅桃","梅花","方片"};

// initpokers 生成一副整齊的牌
void initpokers(vector<unsigned int> &pokers)
{
    pokers.clear();
    for (unsigned int uith = 0; uith != 52; ++uith)
    {
        // 黑桃 < 紅桃 < 梅花 < 方片
        pokers.push_back((uith / 13 << 4) + (uith % 13) + 1); 
    }
}

// shufflepor 洗牌
void shufflepor(vector<unsigned int> &pokers)
{
        srand((unsigned int)time(NULL));
    random_shuffle(pokers.begin(),pokers.end());
}

// sortpokers 快排
void sortpokers(vector<unsigned int> &pokers)
{
    sort(pokers.begin(),pokers.end());
}

// printpokers 列印
void printpokers(const vector<unsigned int> &pokers)
{
    for (vector<unsigned int>::const_iterator vit = pokers.begin(); vit != pokers.end();
        ++vit)
    {
        printf("%s%u\n",COLOR[*vit >> 4],*vit & 0xf);
    }

}

int main()
{
    vector<unsigned int> pokers;
    initpokers(pokers);
//        printpokers(pokers);
    shufflepor(pokers);
//        printpokers(pokers);
    sortpokers(pokers);
    printpokers(pokers);
    return 0;
} 

相關文章