文档详情

第10章 程序结构.ppt

发布:2017-01-25约1.43万字共56页下载文档
文本预览下载声明
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++; } } } * void analyze_hand(void) { 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) flush = true; ? * C D H S 0 1 2 3 5 ? /* 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++; } } * 2 3 4 5 6 7 8 9 10 J Q K A 0 1 2 3 4 5 6 7 8 9 10 11 12 /********************************************************** * 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(void) { if (straight flush) printf(Straight flush); else if (four) printf(Four of a kind); else if (three pairs == 1) printf(Full house); else i
显示全部
相似文档