| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1258 人关注过本帖
标题:扑克排序问题
取消只看楼主 加入收藏
josen0205
Rank: 2
来 自:江苏
等 级:论坛游民
帖 子:307
专家分:52
注 册:2007-5-8
结帖率:100%
收藏
 问题点数:0 回复次数:2 
扑克排序问题
由于版主不承认我是原创,所以我删除了,接近一个小时的努力没有得到承认。我觉得没有必要拿别人的东西来说是自己做的,那样有意思吗?我回复了别人的【扑克牌排序问题】里面有程序,请大家帮我看看算不算原创?!

[[it] 本帖最后由 josen0205 于 2008-3-29 22:58 编辑 [/it]]
搜索更多相关主题的帖子: 扑克 
2008-03-29 22:45
josen0205
Rank: 2
来 自:江苏
等 级:论坛游民
帖 子:307
专家分:52
注 册:2007-5-8
收藏
得分:0 
/*
 有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:51 编辑 [/it]]

只有想不到,没有做不到
2008-03-29 23:05
josen0205
Rank: 2
来 自:江苏
等 级:论坛游民
帖 子:307
专家分:52
注 册:2007-5-8
收藏
得分:0 
回复 3# 的帖子
原创,就是完全你自己想出来的,自己创作出来的,自己写出来的东西。
 
转载,就是把别人“原创”的东西搬到另外一个地方(例如博客,论坛之类)。
通常是看到别人的“原创”有同感,觉得很不错,想把它放到另外一个地方让更多人看到之类的就叫转载。
敢问楼上的,你认为什么是原创呢?

只有想不到,没有做不到
2008-03-30 00:45
快速回复:扑克排序问题
数据加载中...
 
   



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

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