| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1109 人关注过本帖, 1 人收藏
标题:扑克排序【绝对原创】
只看楼主 加入收藏
josen0205
Rank: 2
来 自:江苏
等 级:论坛游民
帖 子:307
专家分:52
注 册:2007-5-8
结帖率:100%
收藏(1)
 问题点数:0 回复次数:4 
扑克排序【绝对原创】
/*
 有6副带不同底色的扑克牌, 其底色分别为红、绿、蓝、白、粉、紫6种颜色.
 排序规则:
 1. 属性优先度:  点数 > 底色 > 花色
 2. 点数优先度:  3 > 4 > 5 > 6 > 7 > 8 > 9 > 10 > J > Q > K > A > 2 > 小王 > 大王
 3. 底色优先度:  红 > 绿 > 蓝 > 白 > 粉 > 紫
 4. 花色优先度:  红心 > 黑桃 > 方块 > 梅花
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

typedef struct Card{
    int bcolor;     /*底色 6:红 5:绿 4:蓝 3:白 2:粉 1:紫*/
    int grade;      /*点数 3 4 5 6 7 8 9 10 11(J) 12(Q) 13(K) 14(A) 15(2) 16(小王) 17(大王)*/
    int color;      /*花色 4:红心 3:黑桃 2:方块 1:梅花*/
    struct Card *next;
}*Link;

char* getbcolor(int n, char *tBuf)
{
    switch(n)
    {
    case 1:
        strcpy(tBuf, "紫");
        break;
    case 2:
        strcpy(tBuf, "粉");
        break;
    case 3:
        strcpy(tBuf, "白");
        break;
    case 4:
        strcpy(tBuf, "蓝");
        break;
    case 5:
        strcpy(tBuf, "绿");
        break;
    case 6:
        strcpy(tBuf, "红");
        break;
    default:
        strcpy(tBuf, "Bad color");
        break;
    }
    return tBuf;
}

char* getcolor(int n, char *tBuf)
{
    switch(n)
    {
    case 0:
        strcpy(tBuf, "00");
        break;
    case 1:
        strcpy(tBuf, "梅花");
        break;
    case 2:
        strcpy(tBuf, "方块");
        break;
    case 3:
        strcpy(tBuf, "黑桃");
        break;
    case 4:
        strcpy(tBuf, "红心");
        break;
    default:
        strcpy(tBuf, "Bad color");
        break;
    }
    return tBuf;
}

char* getgrade(int n, char *tBuf)
{
    switch(n)
    {
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
    case 10:
        sprintf(tBuf, "%d", n);
        break;
    case 11:
        strcpy(tBuf, "J");
        break;
    case 12:
        strcpy(tBuf, "Q");
        break;
    case 13:
        strcpy(tBuf, "K");
        break;
    case 14:
        strcpy(tBuf, "A");
        break;
    case 15:
        strcpy(tBuf, "2");
        break;
    case 16:
        strcpy(tBuf, "小王");
        break;
    case 17:
        strcpy(tBuf, "大王");
        break;
    default:
        strcpy(tBuf, "Bad grade");
        break;
    }
    return tBuf;
}

int InitCards(Link &L) /*生成6副牌324张,存入链表L中*/
{
    int i = 1, j = 1, k = 3;
    Link tmp, p;

    L = (Link)malloc(sizeof(struct Card));
    if(L == NULL)
    {
        printf("动态分配内存失败!");
        return -1;
    }
    p = L;
    for(i = 1; i <= 6; i++) /* 6种底色*/
    {
        /** 小王 **/
        tmp=(Link)malloc(sizeof(struct Card));
        if(tmp==NULL)
        {
            printf("动态分配内存失败!");
            return -1;
        }
        tmp->bcolor = i;
        tmp->color = 0;
        tmp->grade = 16;
        p->next = tmp;
        p = tmp;

        /** 大王 **/
        tmp=(Link)malloc(sizeof(struct Card));
        if(tmp==NULL)
        {
            printf("动态分配内存失败!");
            return -1;
        }
        tmp->bcolor = i;
        tmp->color = 0;
        tmp->grade = 17;
        p->next = tmp;
        p = tmp;
        for(j = 1; j <= 4; j++)/* 4种花色 */
        {
            for(k = 3; k <= 15; k++)
            {
                tmp=(Link)malloc(sizeof(struct Card));
                if(tmp==NULL)
                {
                    printf("动态分配内存失败!");
                    return -1;
                }
                tmp->bcolor = i;
                tmp->color = j;
                tmp->grade = k;
                p->next = tmp;
                p = tmp;
            }
        }
    }
    p->next=L->next;

    return 0;
}

int FreshCards(Link &L) /* 洗牌 */
{
    int m = 0,n = 0,r = 0,i = 0;
    Link T, p, q;

    T = L;
    srand((unsigned)time(NULL));
    r = rand();

    m = r%325;
    n = r%324 + 2;

    p = L->next;
    q = T;
    while(m-1)
    {
        p = p->next;
        m--;
    }
    while(p->next != p)
    {
        i++;
        if(i == n-1)
        {
            q->next= p->next;
            p->next = p->next->next;
            q = q->next;
            i = 0;
        }
        p = p->next;
    }
    q->next = p;
    q = q->next;
    q->next = T->next;
    
    return 0;
}

int GetCards(struct Card *CArray) /* 产生供排序使用的初始排列的6副牌 */
{
    int i = 0;
    Link L, p, s;

    InitCards(L);

    FreshCards(L);
    FreshCards(L);
    FreshCards(L);
    
    p = L->next;
    p = p->next;
    while(p != L->next)
    {
        s = p;
        p = p->next;
        CArray[i].bcolor = s->bcolor;
        CArray[i].color = s->color;
        CArray[i].grade = s->grade;
        i ++;
        free(s);
    }
    CArray[i].bcolor = p->bcolor;
    CArray[i].color = p->color;
    CArray[i].grade = p->grade;
    free(p);
    free(L);

    return 0;
}

int OrderCards(struct Card *inArray, struct Card *outArray)
{
    int i =0, j = 0;
    struct Card tmp;

    for(i = 0; i < 324; i++)
        outArray[i] = inArray[i];

    for(i = 0; i < 323; i++)
    {
        for(j = i+1; j < 324; j++)
        {
            if(outArray[j].grade < outArray[i].grade)
            {
                tmp = outArray[i];
                outArray[i] = outArray[j];
                outArray[j] = tmp;
            }
            else if(outArray[j].grade == outArray[i].grade)
            {
                if(outArray[j].bcolor > outArray[i].bcolor)
                {
                    tmp = outArray[i];
                    outArray[i] = outArray[j];
                    outArray[j] = tmp;
                }
                else if(outArray[j].bcolor == outArray[i].bcolor)
                {
                    if(outArray[j].color > outArray[i].color)
                    {
                        tmp = outArray[i];
                        outArray[i] = outArray[j];
                        outArray[j] = tmp;
                    }
                }
            }

        }
    }

    return 0;
}

int SaveCards(char *tFileName, struct Card *CArray)
{
    int i = 0;
    char tBcolor[16], tGrade[16], tColor[16];
    FILE *fp;

    fp = fopen(tFileName, "w");
    if(fp == NULL)
    {
        printf("打开文件[%s]失败\n", tFileName);
        return -1;
    }

    for(i = 0; i < 324; i++)
    {
        memset(tBcolor, 0, sizeof(tBcolor));
        memset(tGrade, 0, sizeof(tGrade));
        memset(tColor, 0, sizeof(tColor));

        getbcolor(CArray[i].bcolor, tBcolor);
        getgrade(CArray[i].grade, tGrade);
        getcolor(CArray[i].color, tColor);

        fprintf(fp, "[%s,%s,%2s] ", tBcolor, tColor, tGrade);
        
    }
    fclose(fp);
    return 0;
}

int main()
{
    struct Card inArray[326];
    struct Card outArray[326];

    GetCards(inArray);
    OrderCards(inArray, outArray);
    SaveCards("Before.txt", inArray);
    SaveCards("After.txt", outArray);

    printf("Over!\n");

    return 0;
}

[[it] 本帖最后由 josen0205 于 2008-3-30 17:52 编辑 [/it]]
搜索更多相关主题的帖子: 扑克 
2008-03-29 22:50
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
比较繁琐

Fight  to win  or  die...
2008-03-30 00:04
随心
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:12
帖 子:2577
专家分:250
注 册:2007-8-12
收藏
得分:0 
还可以简化
利用掩码操作

天之道,利而不害。圣人之道,为而不争。信言不美,美言不信。善者不辩,辩者不善。知者不博,博者不知。
2008-03-30 04:10
随心
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:12
帖 子:2577
专家分:250
注 册:2007-8-12
收藏
得分:0 
LZ精神不错,再顶一个

天之道,利而不害。圣人之道,为而不争。信言不美,美言不信。善者不辩,辩者不善。知者不博,博者不知。
2008-03-30 04:10
chinasmu
Rank: 2
等 级:论坛游民
帖 子:50
专家分:22
注 册:2009-10-31
收藏
得分:0 
我也顶一个~~
2009-11-04 22:41
快速回复:扑克排序【绝对原创】
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.057222 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved