#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]]