# 扑克牌排序（结构体排序）

1. 对结构体/类进行排序
2. 设置flag将不方便比较的字符转化为相应数值

3 5 黑桃4 红桃10 梅花Q 方块K 黑桃A 10 大王 梅花10 红桃K 方块9 黑桃2 梅花A 方块Q 小王 黑桃8 黑桃J 5

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;
struct poke
{
char type[10];
int num;
int flag;
};

void Switch(poke *newpoker)
{
///flower
if(strstr(newpoker->type, "大王"))
{
newpoker->flag=6;
}
else if(strstr(newpoker->type, "小王"))
{
newpoker->flag=5;
}
else if(strstr(newpoker->type, "黑桃"))
{
newpoker->flag=4;
}
else if(strstr(newpoker->type, "红桃"))
{
newpoker->flag=3;
}
else if(strstr(newpoker->type, "梅花"))
{
newpoker->flag=2;
}
else if(strstr(newpoker->type, "方块"))
{
newpoker->flag=1;
}

///number
if(strstr(newpoker->type, "2"))
{
newpoker->num=2;
}
else if(strstr(newpoker->type, "3"))
{
newpoker->num=3;
}
else if(strstr(newpoker->type, "4"))
{
newpoker->num=4;
}
else if(strstr(newpoker->type, "5"))
{
newpoker->num=5;
}
else if(strstr(newpoker->type, "6"))
{
newpoker->num=6;
}
else if(strstr(newpoker->type, "7"))
{
newpoker->num=8;
}
else if(strstr(newpoker->type, "8"))
{
newpoker->num=8;
}
else if(strstr(newpoker->type, "9"))
{
newpoker->num=9;
}
else if(strstr(newpoker->type, "10"))
{
newpoker->num=10;
}
else if(strstr(newpoker->type, "J"))
{
newpoker->num=11;
}
else if(strstr(newpoker->type, "Q"))
{
newpoker->num=12;
}
else if(strstr(newpoker->type, "K"))
{
newpoker->num=13;
}
else if(strstr(newpoker->type, "A"))
{
newpoker->num=14;
}
}

bool comp(poke p1, poke p2)
{
if(p1.flag>p2.flag)
{
return true;
}
else if(p1.flag<p2.flag)
{
return false;
}
if(p1.num>p2.num)
{
return true;
}
else if(p1.num<p2.num)
return false;
return 1;
}

int main()
{
int t, n, i;
//struct poke group[100];
cin>>t;
while(t--)
{
cin>>n;
poke *group = new poke[n];

for(i=0; i<n; i++)
{
cin>>group[i].type;
Switch(&group[i]);
}

sort(group, group+n, comp);

for(i=0; i<n; i++)
{
cout<<group[i].type<<' ';
}
cout<<endl;

delete[] group;
}
return 0;
}

• 0
点赞
• 0
评论
• 9
收藏
• 一键三连
• 扫一扫，分享海报

10-21 2597

03-20 3279
09-10 22
04-28 1万+
10-12 1110
08-18 789
02-15 3348
11-13 1106
11-10 856
07-06 2259
03-12 2145
03-26 3419