C语言
来源:互联网 发布:国内主机推荐 知乎 编辑:程序博客网 时间:2024/06/01 10:50
#include<stdbool.h>
#include<stdio.h>
#include<stdlib.h>
#define NUM_RANKS 13
#define NUM_SUITS 4
#define NUM_CARDS 5
/*函数声明*/
void read_cards(int num_in_rank[],int num_in_suit[]);
void analyze_hand(int num_in_rank[],int num_in_suit[],bool *straight,bool *flush,bool *four,bool *three,int *pairs);
void print_result(bool straight,bool flush,bool four,bool three,int pairs);
/**********************************************************
* main: 重复调用 read_cards, analyze_hand, and print_result *
* *
**********************************************************/
int main(void)
{
int num_in_rank[NUM_RANKS];
int num_in_suit[NUM_SUITS];
bool straight,flush,four,three;
int pairs;/*can be 0,1,or2*/
for (;;) {
read_cards(num_in_rank,num_in_suit);
analyze_hand(num_in_rank,num_in_suit,&straight,&flush,&four,&three,&pairs);
print_result(straight,flush,four,three,pairs);
}
}
/**********************************************************
* read_cards: Reads the cards into the parameters *
* num_in_rank and num_in_suit; checks *
* for bad cards and duplicate cards. *
**********************************************************/
void read_cards(int num_in_rank[],int num_in_suit[])
{
bool card_exists[NUM_RANKS][NUM_SUITS];//记录该张牌是否已出现
char ch, rank_ch, suit_ch;
int rank, suit;
bool bad_card;//是否是不合法的牌
int cards_read = 0;//记录输入的牌的张数
for (rank = 0; rank < NUM_RANKS; rank++) {
num_in_rank[rank] = 0;//清零
for (suit = 0; suit < NUM_SUITS; suit++)
card_exists[rank][suit] = false;//清零
}
for (suit = 0; suit < NUM_SUITS; suit++)
num_in_suit[suit] = 0;//清零
while (cards_read < NUM_CARDS)
{
bad_card = false;
printf("Enter a card: ");
rank_ch = getchar();//输入面值
switch (rank_ch)
{
case '0': exit(EXIT_SUCCESS);
case '2': rank = 0; break;
case '3': rank = 1; break;
case '4': rank = 2; break;
case '5': rank = 3; break;
case '6': rank = 4; break;
case '7': rank = 5; break;
case '8': rank = 6; break;
case '9': rank = 7; break;
case 't': case 'T': rank = 8; break;
case 'j': case 'J': rank = 9; break;
case 'q': case 'Q': rank = 10; break;
case 'k': case 'K': rank = 11; break;
case 'a': case 'A': rank = 12; break;
default: bad_card = true;
}
suit_ch = getchar();//输入花色
switch (suit_ch)
{
case 'c': case 'C': suit = 0; break;
case 'd': case 'D': suit = 1; break;
case 'h': case 'H': suit = 2; break;
case 's': case 'S': suit = 3; break;
default: bad_card = true;
}
while ((ch = getchar()) != '\n')//判断是否结束输入
if (ch != ' ') bad_card = true;
if (bad_card)
printf("Bad card; ignored.\n");
else if (card_exists[rank][suit])
printf("Duplicate card; ignored.\n");
else
{
num_in_rank[rank]++;
num_in_suit[suit]++;
card_exists[rank][suit] = true;
cards_read++;
}
}
}
/**********************************************************
* analyze_hand: Determines whether the hand contains a *
* straight, a flush, four-of-a-kind, *
* and/or three-of-a-kind; determines the *
* number of pairs; stores the results into *
* the external variables straight, flush, *
* four, three, and pairs. *
**********************************************************/
void analyze_hand(int num_in_rank[],int num_in_suit[],bool *straight,bool *flush,bool *four,bool *three,int *pairs)
{
int num_consec = 0;
int rank, suit;
*straight = false;//顺子
*flush = false;//同花
*four = false;//四条
*three = false;//三条
*pairs = 0;//对数
/* check for flush */
for (suit = 0; suit < NUM_SUITS; suit++)
if (num_in_suit[suit] == NUM_CARDS)//5张牌同一个花色 ,即同花
*flush = true;
/* check for straight */
rank = 0;
while (num_in_rank[rank] == 0) rank++;//跳过没有出现的牌
for (; rank < NUM_RANKS && num_in_rank[rank] > 0; rank++)//统计分值连续出现的牌数量
num_consec++;
if (num_consec == NUM_CARDS) { //若分值连续出现的数量为发牌数量,那么就是顺子
*straight = true;
return;
}
/* check for 4-of-a-kind, 3-of-a-kind, and pairs */
for (rank = 0; rank < NUM_RANKS; rank++) {
if (num_in_rank[rank] == 4) *four = true;
if (num_in_rank[rank] == 3) *three = true;
if (num_in_rank[rank] == 2) (*pairs)++;
}
}
/**********************************************************
* print_result: Prints the classification of the hand, *
* based on the values of the external *
* variables straight, flush, four, three, *
* and pairs. *
**********************************************************/
void print_result(bool straight,bool flush,bool four,bool three,int pairs)
{
if (straight && flush) printf("Straight flush");//同花顺
else if (four) printf("Four of a kind");//四条,即炸弹
else if (three &&
pairs == 1) printf("Full house");//葫芦
else if (flush) printf("Flush");//同花
else if (straight) printf("Straight");//顺子
else if (three) printf("Three of a kind");//三条
else if (pairs == 2) printf("Two pairs");//两对
else if (pairs == 1) printf("Pair");//一对
else printf("High card");//高分
printf("\n\n");
}
#include<stdio.h>
#include<stdlib.h>
#define NUM_RANKS 13
#define NUM_SUITS 4
#define NUM_CARDS 5
/*函数声明*/
void read_cards(int num_in_rank[],int num_in_suit[]);
void analyze_hand(int num_in_rank[],int num_in_suit[],bool *straight,bool *flush,bool *four,bool *three,int *pairs);
void print_result(bool straight,bool flush,bool four,bool three,int pairs);
/**********************************************************
* main: 重复调用 read_cards, analyze_hand, and print_result *
* *
**********************************************************/
int main(void)
{
int num_in_rank[NUM_RANKS];
int num_in_suit[NUM_SUITS];
bool straight,flush,four,three;
int pairs;/*can be 0,1,or2*/
for (;;) {
read_cards(num_in_rank,num_in_suit);
analyze_hand(num_in_rank,num_in_suit,&straight,&flush,&four,&three,&pairs);
print_result(straight,flush,four,three,pairs);
}
}
/**********************************************************
* read_cards: Reads the cards into the parameters *
* num_in_rank and num_in_suit; checks *
* for bad cards and duplicate cards. *
**********************************************************/
void read_cards(int num_in_rank[],int num_in_suit[])
{
bool card_exists[NUM_RANKS][NUM_SUITS];//记录该张牌是否已出现
char ch, rank_ch, suit_ch;
int rank, suit;
bool bad_card;//是否是不合法的牌
int cards_read = 0;//记录输入的牌的张数
for (rank = 0; rank < NUM_RANKS; rank++) {
num_in_rank[rank] = 0;//清零
for (suit = 0; suit < NUM_SUITS; suit++)
card_exists[rank][suit] = false;//清零
}
for (suit = 0; suit < NUM_SUITS; suit++)
num_in_suit[suit] = 0;//清零
while (cards_read < NUM_CARDS)
{
bad_card = false;
printf("Enter a card: ");
rank_ch = getchar();//输入面值
switch (rank_ch)
{
case '0': exit(EXIT_SUCCESS);
case '2': rank = 0; break;
case '3': rank = 1; break;
case '4': rank = 2; break;
case '5': rank = 3; break;
case '6': rank = 4; break;
case '7': rank = 5; break;
case '8': rank = 6; break;
case '9': rank = 7; break;
case 't': case 'T': rank = 8; break;
case 'j': case 'J': rank = 9; break;
case 'q': case 'Q': rank = 10; break;
case 'k': case 'K': rank = 11; break;
case 'a': case 'A': rank = 12; break;
default: bad_card = true;
}
suit_ch = getchar();//输入花色
switch (suit_ch)
{
case 'c': case 'C': suit = 0; break;
case 'd': case 'D': suit = 1; break;
case 'h': case 'H': suit = 2; break;
case 's': case 'S': suit = 3; break;
default: bad_card = true;
}
while ((ch = getchar()) != '\n')//判断是否结束输入
if (ch != ' ') bad_card = true;
if (bad_card)
printf("Bad card; ignored.\n");
else if (card_exists[rank][suit])
printf("Duplicate card; ignored.\n");
else
{
num_in_rank[rank]++;
num_in_suit[suit]++;
card_exists[rank][suit] = true;
cards_read++;
}
}
}
/**********************************************************
* analyze_hand: Determines whether the hand contains a *
* straight, a flush, four-of-a-kind, *
* and/or three-of-a-kind; determines the *
* number of pairs; stores the results into *
* the external variables straight, flush, *
* four, three, and pairs. *
**********************************************************/
void analyze_hand(int num_in_rank[],int num_in_suit[],bool *straight,bool *flush,bool *four,bool *three,int *pairs)
{
int num_consec = 0;
int rank, suit;
*straight = false;//顺子
*flush = false;//同花
*four = false;//四条
*three = false;//三条
*pairs = 0;//对数
/* check for flush */
for (suit = 0; suit < NUM_SUITS; suit++)
if (num_in_suit[suit] == NUM_CARDS)//5张牌同一个花色 ,即同花
*flush = true;
/* check for straight */
rank = 0;
while (num_in_rank[rank] == 0) rank++;//跳过没有出现的牌
for (; rank < NUM_RANKS && num_in_rank[rank] > 0; rank++)//统计分值连续出现的牌数量
num_consec++;
if (num_consec == NUM_CARDS) { //若分值连续出现的数量为发牌数量,那么就是顺子
*straight = true;
return;
}
/* check for 4-of-a-kind, 3-of-a-kind, and pairs */
for (rank = 0; rank < NUM_RANKS; rank++) {
if (num_in_rank[rank] == 4) *four = true;
if (num_in_rank[rank] == 3) *three = true;
if (num_in_rank[rank] == 2) (*pairs)++;
}
}
/**********************************************************
* print_result: Prints the classification of the hand, *
* based on the values of the external *
* variables straight, flush, four, three, *
* and pairs. *
**********************************************************/
void print_result(bool straight,bool flush,bool four,bool three,int pairs)
{
if (straight && flush) printf("Straight flush");//同花顺
else if (four) printf("Four of a kind");//四条,即炸弹
else if (three &&
pairs == 1) printf("Full house");//葫芦
else if (flush) printf("Flush");//同花
else if (straight) printf("Straight");//顺子
else if (three) printf("Three of a kind");//三条
else if (pairs == 2) printf("Two pairs");//两对
else if (pairs == 1) printf("Pair");//一对
else printf("High card");//高分
printf("\n\n");
}
0 0
- C语言
- C语言
- C语言
- C语言
- C语言
- c语言
- C语言
- C语言
- C语言
- C语言
- c语言
- C语言
- C语言
- C语言
- c语言
- C语言
- c语言
- c语言
- JSP系统开发学习之四关于用户登录界面的补充——通过request页面传值&wel.jsp的MVC的实现
- rpm命令参数
- node.js学习日记(3)
- OpenGL ES 3.0之着色器和程序
- jps 命令使用
- C语言
- 谈一下ACM的入门书籍及方法
- PHP中的数组(指针)
- 自定义绘图View之onDraw
- J2SE - 内存解析
- Android异步消息处理机制详解及源码分析
- Shell—函数
- Ubuntu安装中文版WordPress
- c#服务器验证控件基本用法