#include<iostream>
#include<algorithm> //STL中的算法
#include<numeric>//标准库中的数学操作函数
#include "chain.h"
//对卡片进行排序
//template<typename T>
void cardSort(int n, chain<card>& L)
{
//先对面值进行排序(有5种面值)
for (int k = 1; k <= 3; ++k)
{
int theBin;
switch (k) {
case 1: //对面值排序
theBin = 5;
break;
case 2: //对样式排序
theBin = 5;
break;
case 3: //对组号排序
theBin = n;
break;
}
//先创建箱子
chainNode<card>**bottom, **top;
bottom = new chainNode<card>*[theBin];
top = new chainNode<card>*[theBin];
for (int b = 0; b < theBin; ++b)
top[b] = NULL;
//将链表的节点分配到箱子
for (; L.firstNode != NULL; L.firstNode = L.firstNode->next)
{
int i = 0;
if (k == 1)
{
float f = (L.firstNode->element).face;
if(f==5)
i = 0;
else if(f==10)
i = 1;
else if (f ==20)
i = 2;
else if (f ==50)
i = 3;
else if (f ==100)
i = 4;
}
if (k == 2)
{
string s = (L.firstNode->element).deck;
if (s == "a")
i = 0;
else if (s =="b")
i = 1;
else if (s=="c")
i = 2;
else if (s=="d")
i = 3;
else if (s=="e")
i = 4;
}
if (k == 3)
i = (L.firstNode->element).suit;
if (top[i] == NULL) //该面值只出现了一次
top[i] = bottom[i] = L.firstNode;
else //该面值出现了多次
{
bottom[i]->next = L.firstNode;
bottom[i] = L.firstNode;
}
}
//从箱子的节点中收集有序链表
chainNode<card>*y = NULL;
for (int i = 0; i < theBin; ++i)
if (top[i] != NULL)
{
if (y == NULL) //搜集第一个非空箱子
L.firstNode = top[i];
else //搜集其他的箱子
y->next = top[i];
y = bottom[i]; //顺序连接
}
if (y != NULL)
y->next = NULL;
}
for (chainNode<card>*p = L.firstNode; p != NULL; p = p->next)
cout << (p->element).suit << " " << (p->element).deck << " " << (p->element).face << " ";
}
int main()
{
//对卡片排序进行测试
chain<card>money(15);//10张卡片
//给这10张卡片赋值然后把他们插入链表,形成大小为10的链表
string s[15] = { "e","b","c","a","b","d","c","e","a","b","d","c","e","a","b" };//卡片样式(假设卡片样式有a,b,c,d和e这5种)
float f[15] = { 100,50,50,5,50,20,10,50,50,100,20,10,50,10,20 }; //卡片面值(假设面值有5,10,20,50和100这5种)
int su[15] = { 9,8,1,6,6,5,3,2,2,1,5,3,2,2,1 }; //卡片组号
for (int i = 0; i<15; ++i)
{
card cardRecord;
cardRecord.suit = su[i];
cardRecord.deck = s[i];
cardRecord.face = f[i];
money.insert(i, cardRecord);
}
cardSort(10,money);
return 0;
}
基于箱子排序对一堆n组卡片进行排序(C++单链表描述)
最新推荐文章于 2020-10-27 10:11:47 发布