C++实现对结构体排序并用重载函数定义多种排序类型

#define _CRT_SECURE_NO_WARNINGS
#include
#include
using namespace std;
struct Poker
{
char type;
int point;
};
enum{
SPADES,//黑桃
HEARTS,//红桃
CLUBS,//梅花
DIAMONDS,//方片
JOKER//王
};
void InputPoker(Poker * pk){
scanf("%c%d",&pk->type,&pk->point);
pk->type -= ‘a’;
if (pk->type == JOKER){
pk->point += 13;
}
getchar();
}
void outputPoker(Poker pk){
char type[5] = { “黑桃”, “红桃”, “梅花”, “方片”,"" };
char
point[16] = { “”, “A”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”, “J”, “Q”, “K”,“小王”,“大王” };
printf("%s%s\n",type[pk.type],point[pk.point]);
}
bool cmpPoker1(Poker a, Poker b){
return (a.point < b.point) || (a.point == b.point&&a.type < b.type);
}//按点数优先排序从小到大
bool cmpPoker2(Poker a, Poker b){
return (a.type < b.type) || (a.type == b.type&&a.point < b.point);
}//按花色优先排序从小到大
bool cmpPoker3(Poker a, Poker b){
return (a.point > b.point) || (a.point == b.point&&a.type > b.type);
}//按点数优先排序从大到小
bool cmpPoker4(Poker a, Poker b){
return (a.type > b.type) || (a.type == b.type&&a.point > b.point);
}//按花色优先排序从大到小
bool cmpPoker5(Poker a, Poker b){
if (a.point <= 2){
a.point += 11;
}
else if(a.point<=13)
{
a.point -= 2;
}
return (a.point > b.point) || (a.point == b.point&&a.type > b.type);
}//斗地主排序从大到小大>小王>2
void way1(Poker *src, int n,bool(*cmp)(Poker,Poker) = cmpPoker1){
int i, j;
Poker tmp;
for (i = 1; i < n; i++){
tmp = src[i];
for (j = i; j>0 &&cmp( tmp,src[j - 1]) ; j–){
src[j] = src[j - 1];
}
src[j] = tmp;
}
}

int main_(){
Poker p[5] = { SPADES, 3, HEARTS, 1,SPADES,1,CLUBS,2,DIAMONDS,3 };
/Poker p[5];/
/for (int i = 0; i < 5; i++){
InputPoker(&p [i]);
}
/
way1(p, 5,cmpPoker1);
for (auto i : p){
outputPoker(i);
}
printf("\n");
way1(p, 5, cmpPoker5);
for (auto i : p){
outputPoker(i);
}
printf("\n");
way1(p, 5, cmpPoker3);
for (auto i : p){
outputPoker(i);
}
printf("\n");
way1(p, 5, cmpPoker4);
for (auto i : p){
outputPoker(i);
}
printf("\n");
system(“pause”);
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值