1 //这是一个纯C程序,会要求你输入5张牌,然后分析,根据同花顺 四张一样 三张一样和一对 。。。来比较大小,其中牌色分别用c d h s 表示 比如输入为 2 /* 3 Enter a card:3s 4 Enter a card:5s 5 Enter a card:6s 6 Enter a card:2s 7 Enter a card:4s 8 输出应为 9 Straight Flush 10 可是我的程序却输出为 11 Flush 12 经判定我的判定Straight==true 不工作,希望各位大神给看看,给修改下。。。 13 */ 14 #include<stdio.h> 15 #include<stdbool.h> 16 #include<stdlib.h> 17 #define NUM_RANKS 13//牌张总数 18 #define NUM_SUITS 4//花色 19 #define NUM_CARDS 5//抓牌数 20 int num_in_rank[NUM_RANKS]; 21 int num_in_suit[NUM_SUITS]; 22 _Bool straight,flush,four,three; 23 int pairs; 24 void read_cards(void); 25 void analyze_hand(void); 26 void print_result(void); 27 int main(void) 28 { 29 for(;;) 30 { 31 read_cards(); 32 analyze_hand(); 33 print_result(); 34 } 35 } 36 //输入牌张函数 37 void read_cards(void) 38 { 39 _Bool card_exists[NUM_RANKS][NUM_SUITS]; 40 char ch,rank_ch,suit_ch; 41 int rank,suit; 42 _Bool bad_card; 43 int cards_read = 0; 44 for (rank = 0; rank<NUM_RANKS; rank++) 45 { 46 num_in_rank[rank] = 0; 47 for(suit=0;suit<NUM_SUITS;suit++) 48 { 49 card_exists[rank][suit] = false; 50 } 51 } 52 for (suit=0;suit<NUM_SUITS;suit++) 53 { 54 num_in_suit[suit] = 0; 55 } 56 while (cards_read<NUM_CARDS) 57 { 58 bad_card = false; 59 60 printf ("Enter a card:"); 61 62 rank_ch = getchar(); 63 switch (rank_ch) 64 { 65 case '0': exit(EXIT_SUCCESS); 66 case '2': rank = 0; break; 67 case '3': rank = 1; break; 68 case '4': rank = 2; break; 69 case '5': rank = 3; break; 70 case '6': rank = 4; break; 71 case '7': rank = 5; break; 72 case '8': rank = 6; break; 73 case '9': rank = 7; break; 74 case 't': case 'T': rank = 8; break; 75 case 'j': case 'J': rank = 9; break; 76 case 'q': case 'Q': rank = 10; break; 77 case 'k': case 'K': rank = 11; break; 78 case 'a': case 'A': rank = 12; break; 79 default : bad_card = true ; 80 } 81 suit_ch = getchar(); 82 switch (suit_ch) 83 { 84 case 'c': case 'C': suit = 0;break; 85 case 'd': case 'D': suit = 1;break; 86 case 'h': case 'H': suit = 2;break; 87 case 's': case 'S': suit = 3;break; 88 default: bad_card = true; 89 } 90 while((ch = getchar()) != '\n') 91 { 92 if (ch != ' ') 93 bad_card = true; 94 } 95 if(bad_card) 96 { 97 printf("Bad Card;Ignored. \n"); 98 } 99 else if (card_exists[rank][suit]) 100 { 101 printf("Duplicate card; Ignored \n"); 102 } 103 else 104 { 105 num_in_rank[rank]++; 106 num_in_suit[suit]++; 107 card_exists[rank][suit] = true; 108 cards_read++; 109 } 110 } 111 } 112 //分析牌大小 113 void analyze_hand(void) 114 { 115 int num_consec = 0; 116 int rank,suit; 117 straight = false; 118 flush = false; 119 four = false; 120 three = false; 121 pairs = 0; 122 123 for(suit = 0; suit < NUM_SUITS; suit++) 124 { 125 if (num_in_suit[suit] == NUM_CARDS) 126 flush = true; 127 } 128 rank = 0; 129 while (num_in_rank[rank] == 0) 130 { 131 rank++; 132 } 133 for(;rank < NUM_RANKS && num_in_rank[rank] > 0;rank++) 134 { 135 num_consec++; 136 } 137 if(num_consec == NUM_CARDS) 138 { 139 straight = true; 140 return ; 141 } 142 for (rank = 0;rank<NUM_RANKS && num_in_rank[rank] > 0;rank++) 143 { 144 if(num_in_rank[rank] == 4) four = true; 145 if(num_in_rank[rank] == 3) three = true; 146 if(num_in_rank[rank] == 2) pairs++; 147 } 148 } 149 //输出判断 150 void print_result(void) 151 { 152 if(straight && flush) printf("Straight flush\n"); 153 else if(four) printf("Four of a kind\n"); 154 else if (three && pairs) printf("Full house\n"); 155 else if (flush) printf("Flush\n"); 156 else if (straight) printf("Straight\n"); 157 else if (three) printf("three of a kind\n"); 158 else if (pairs == 2) printf("Two pairs\n"); 159 else if (pairs == 1) printf("pair\n"); 160 else printf("High card\n"); 161 printf("\n\n"); 162 }
/*这是eclipse 运行结果
Enter a card:2s
Enter a card:3s
Enter a card:4s
Enter a card:5s
Enter a card:6s
Flush
这是终端运行结果:
[qing@bogon Card]$ ./Card
Enter a card:2s
Enter a card:3s
Enter a card:4s
Enter a card:5s
Enter a card:6s
Straight flush
我想知道我该怎么做才能让它们运行一致,这是什么原因造成了该怎么避免,我是一个新的成员也是一个新手,比较穷,希望各位大神不吝啬的解说,先谢谢了
*/
调试是没问题,可以达到效果。。。。这个该怎么解决。我已经不指望大神们了