题意:
对扑克的数字大小以及花色进行规定,然后顺序发牌,在东南西北都抓完牌的时候,对手中的牌进行排序,然后按照题意给定的输出规格进行输出。
思路:
对于顺序拿牌,可以通过%4来实现循环的顺序发牌,在读入一开始的方向后,便可顺序依次的膜4进行顺序拿牌。在拿牌的时候,定义结构体,其中有两个int的变量,即存放牌的大小和花色,每次读入两个字符,通过两个函数,分别将读进来的字符转换成对应大小的数字,存放到结构体中的两个int中去。然后结构体中重载<,即先按照花色升序,再按照牌的大小输出。再输出的时候,同样有两个函数,即将之前转化后的数字重新转化位字符输出。按照规定的格式输出即可,注意每组数据后要有一个空行。
总结:
主要实现难度在于一个循环的抓牌的过程,另外一个即是对抽象的字符代表的花色和牌进行排序输出。这里主要运用的是先用函数将字符和数字之间进行大小对应的相互转换即可。
代码:
#include <iostream>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
int count(char c){ //字符转换为数字的函数
char m[13]={'2','3','4','5','6','7','8','9','T','J','Q','K','A'};
int i=0;
for(; i<13; i++){
if(c==m[i]) break;
}
return 2+i;
}
char tocount(int a){ //数字转换为字符
char m[13]={'2','3','4','5','6','7','8','9','T','J','Q','K','A'};
return m[a-2];
}
char tocolor(int a){ //数字转换为颜色
char m[4]={'C','D','S','H'};
return m[a];
}
int color(char c){ //颜色转换为数字
char m[4]={'C','D','S','H'};
int i=0;
for(; i<4; i++){
if(c==m[i]) break;
}
return i;
}
struct P{ //结构体,存放颜色和数字
int a; //
int b;
bool operator<(const P &p){
if(a!=p.a) return a<p.a;
if(b!=p.b) return b<p.b;
}
};
int dire(char c){ //方向
char a[4]={'E','S','W','N'};
int i=0;
for(; i<4; i++){
if(a[i]==c) break;
}
return i;
}
int main(){
ios::sync_with_stdio(false);
char c;
cin>>c;
while(c!='#'){
P E[13]; //四个人的手牌数组
P S[13];
P W[13];
P N[13];
char l,k;
int i=dire(c);
for(int j=0; j<52;j++){ //读入手牌
if(i==0){
cin>>l>>k;
E[j/4].a=color(l);
E[j/4].b=count(k);
}else if(i==1){
cin>>l>>k;
S[j/4].a=color(l);
S[j/4].b=count(k);
}else if(i==2){
cin>>l>>k;
W[j/4].a=color(l);
W[j/4].b=count(k);
}else{
cin>>l>>k;
N[j/4].a=color(l);
N[j/4].b=count(k);
}
i=(i+1)%4;
}
sort(E, E+13); //分别排序
sort(S, S+13);
sort(W, W+13);
sort(N, N+13);
cout<<"South player:"<<endl; //输出
cout<<"+---+---+---+---+---+---+---+---+---+---+---+---+---+"<<endl;
for(int i=0; i<13; i++){
cout<<"|"<<tocount(E[i].b)<<" "<<tocount(E[i].b);
} cout<<"|"<<endl;
for(int i=0; i<13; i++){
cout<<"|"<<" "<<tocolor(E[i].a)<<" ";
} cout<<"|"<<endl;
for(int i=0; i<13; i++){
cout<<"|"<<tocount(E[i].b)<<" "<<tocount(E[i].b);
} cout<<"|"<<endl;
cout<<"+---+---+---+---+---+---+---+---+---+---+---+---+---+"<<endl;
cout<<"West player:"<<endl;
cout<<"+---+---+---+---+---+---+---+---+---+---+---+---+---+"<<endl;
for(int i=0; i<13; i++){
cout<<"|"<<tocount(S[i].b)<<" "<<tocount(S[i].b);
} cout<<"|"<<endl;
for(int i=0; i<13; i++){
cout<<"|"<<" "<<tocolor(S[i].a)<<" ";
} cout<<"|"<<endl;
for(int i=0; i<13; i++){
cout<<"|"<<tocount(S[i].b)<<" "<<tocount(S[i].b);
} cout<<"|"<<endl;
cout<<"+---+---+---+---+---+---+---+---+---+---+---+---+---+"<<endl;
cout<<"North player:"<<endl;
cout<<"+---+---+---+---+---+---+---+---+---+---+---+---+---+"<<endl;
for(int i=0; i<13; i++){
cout<<"|"<<tocount(W[i].b)<<" "<<tocount(W[i].b);
} cout<<"|"<<endl;
for(int i=0; i<13; i++){
cout<<"|"<<" "<<tocolor(W[i].a)<<" ";
} cout<<"|"<<endl;
for(int i=0; i<13; i++){
cout<<"|"<<tocount(W[i].b)<<" "<<tocount(W[i].b);
} cout<<"|"<<endl;
cout<<"+---+---+---+---+---+---+---+---+---+---+---+---+---+"<<endl;
cout<<"East player:"<<endl;
cout<<"+---+---+---+---+---+---+---+---+---+---+---+---+---+"<<endl;
for(int i=0; i<13; i++){
cout<<"|"<<tocount(N[i].b)<<" "<<tocount(N[i].b);
} cout<<"|"<<endl;
for(int i=0; i<13; i++){
cout<<"|"<<" "<<tocolor(N[i].a)<<" ";
} cout<<"|"<<endl;
for(int i=0; i<13; i++){
cout<<"|"<<tocount(N[i].b)<<" "<<tocount(N[i].b);
} cout<<"|"<<endl;
cout<<"+---+---+---+---+---+---+---+---+---+---+---+---+---+"<<endl;
cout<<endl;
cin>>c;
}
return 0;
}