标题:扑克牌排序问题
只看楼主
yqiong
Rank: 1
等 级:新手上路
帖 子:315
专家分:0
注 册:2007-7-4
结帖率:83.33%
 问题点数:0 回复次数:13 
扑克牌排序问题
试题一:
有6副带不同底色的扑克牌, 其底色分别为红、绿、蓝、白、粉、紫6种颜色. 现要求编写程序对这6副牌进行排序, 排序规则及做题要求如下:

排序规则:
排序优先度为:
1. 属性优先度:  点数 > 底色 > 花色 (即: 在排序时, “点数”要比“颜色”和“花色”重要, 而“颜色”又比“花色”重要);
2. 点数优先度:  3 > 4 > 5 > 6 > 7 > 8 > 9 > 10 > J > Q > K > A > 2 > 小王 > 大王
3. 底色优先度:  红 > 绿 > 蓝 > 白 > 粉 > 紫
4. 花色优先度:  红心 > 黑桃 > 方块 > 梅花

要求:
1. 编写合理的数据结构表示每张牌, 并说明数据结构中每个域的含意; (5分)
2. 编写用来产生供排序使用的初始排列的6副牌(可以是任意顺序)的函数, 并对关键性语句进行必要的注释; (25分)
3. 编写对6副牌进行排序的函数, 并在适当位置进行注释, 以增强程序的可读性. (60分)
4. 编写主函数, 调用上述函数对6副牌进行排序. (5分)
5. 将排序前和排序后的扑克牌的排序情况, 分别输出到源程序所在目录(或称文件夹)中的两个文本文件中 (排序前的牌放在“Before.txt”中, 排序后的牌放在“After.txt”中); (5分)
搜索更多相关主题的帖子: 扑克牌 梅花 底色 花色 点数 
2008-03-29 15:36
yqiong
Rank: 1
等 级:新手上路
帖 子:315
专家分:0
注 册:2007-7-4
得分:0 
跟上呀!
2008-03-29 17:35
闪闪4521
Rank: 1
等 级:新手上路
帖 子:196
专家分:0
注 册:2007-11-30
得分:0 
可以将扑克牌都数据化,
用一个结构体包含三个整形量表示,
通过三个数组实现将扑克三个字符属性转化为整型的,
比较的时候直接比较整型数的大小,
Number[15]={3,4,5,6,7,8,9,10,j,q,k,1,2,queen,king}
Dcolor[6]={red,green,blue,white,pink,purple}
Fcolor[4]={heart,spade,block,flower}
产生初始的牌的时候就直接用random产生随机数就行了..
2008-03-29 17:59
yqiong
Rank: 1
等 级:新手上路
帖 子:315
专家分:0
注 册:2007-7-4
得分:0 
random?C中有这样的函数?
2008-03-29 18:08
闪闪4521
Rank: 1
等 级:新手上路
帖 子:196
专家分:0
注 册:2007-11-30
得分:0 
有的..
baidu一下就知道了
2008-03-29 18:12
yqiong
Rank: 1
等 级:新手上路
帖 子:315
专家分:0
注 册:2007-7-4
得分:0 
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
enum dotnum{3,4,5,6,7,8,9,10,J,Q,K,A,2,小王,大王};
enum dise{红,绿,蓝,白,粉,紫};
enum huase{红心,黑桃,方块,梅花};
void creatno();

struct card
{
enum dotnum a;
enum dise b;
enum huase c;
} cards[330];


void creatno()
{
    int i;
    for(i=0;i<324;i++)
    { cards[i].a=(enum dotnum)(rand()%13+1);
      cards[i].b=(enum dise)(rand()%6+1);
      cards[i].c=(enum huase)(rand()%5+1);
    }
    ofstream outfile("before.txt");
    if(!outfile)
    {cerr<<"can't open file."<<endl;
    exit(1);
    }
    for(i=0;i<324;i++)
    {
        outfile<<cards[i].a<<"    ";
        outfile<<cards[i].b<<"    ";
        outfile<<cards[i].c<<"    ";
        outfile<<endl<<endl;
    }
}

void main()
{
    creatno();
}
2008-03-29 18:44
yqiong
Rank: 1
等 级:新手上路
帖 子:315
专家分:0
注 册:2007-7-4
得分:0 
枚举类型要怎么用啊?
上述这样为什么会错了?
2008-03-29 18:44
闪闪4521
Rank: 1
等 级:新手上路
帖 子:196
专家分:0
注 册:2007-11-30
得分:0 
你去看看枚举类型的定义吧,
不是这么用的..
http://hi.baidu.com/bj2008%5F%D6%D0%B9%FA/blog/item/6900166623e0d825aa184c92.html
2008-03-29 19:02
yqiong
Rank: 1
等 级:新手上路
帖 子:315
专家分:0
注 册:2007-7-4
得分:0 
谢谢你,楼上的。能否帮忙写个函数实现第二问?
改成如下,还是不知道该怎么处理!
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
enum dotnum{three,four,five,six,seven,eight,nine,ten,J,Q,K,A,two,sking,bking};
enum dise{red,green,blue,white,pink,purple};
enum huase{hongxi,heitiao,fangkuai,meihua};
void creatno();

struct card
{
enum dotnum a;
enum dise b;
enum huase c;
} cards[330];


void creatno()
{
    int i;
    for(i=0;i<324;i++)
    { cards[i].a=(enum dotnum)(rand()%13+1);
      cards[i].b=(enum dise)(rand()%6+1);
      cards[i].c=(enum huase)(rand()%5+1);
    }
    ofstream outfile("before.txt");
    if(!outfile)
    {cerr<<"can't open file."<<endl;
    exit(1);
    }
    for(i=0;i<324;i++)
    {
        outfile<<cards[i].a<<"    ";
        outfile<<cards[i].b<<"    ";
        outfile<<cards[i].c<<"    ";
        outfile<<endl<<endl;
    }
}

void main()
{
    creatno();
}
2008-03-29 19:08
josen0205
Rank: 2
来 自:江苏
等 级:论坛游民
帖 子:307
专家分:52
注 册:2007-5-8
得分:0 
#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 18:10 编辑 [/it]]

只有想不到,没有做不到
2008-03-29 22:46



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-206012-1-1.html




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

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