/* This is just like poker1, except that it reads any number of input hands, and prints its evaluation of each hand. The program stops when it reaches the end of input. To mark the end of input from a terminal, type ^D. Or create a file with your input, and have the program read from the file: poker2 < inputfile Make sure you understand these language features: for(;;) break continue break and continue look for loops, not if()s using scanf() as a number To see the differences between poker1.c and poker2.c: diff -uw poker1.c poker2.c | more */ #include int main(void) { char r0; char s0; /* rank and suit of card #0 */ char r1; char s1; char r2; char s2; char r3; char s3; char r4; char s4; int flush; /* we have a flush (all cards in the same suit) */ int have2; /* number of cards with rank 2 */ int have3; int have4; int have5; int have6; int have7; int have8; int have9; int haveT; int haveJ; int haveQ; int haveK; int haveA; char straight; /* the highest rank in a straight; 0 if not straight */ int pairs; int threes; for (;;) { if (scanf(" %c%c",&r0,&s0) < 2) break; if (scanf(" %c%c",&r1,&s1) < 2) break; if (scanf(" %c%c",&r2,&s2) < 2) break; if (scanf(" %c%c",&r3,&s3) < 2) break; if (scanf(" %c%c",&r4,&s4) < 2) break; printf("%c%c ",r0,s0); printf("%c%c ",r1,s1); printf("%c%c ",r2,s2); printf("%c%c ",r3,s3); printf("%c%c: ",r4,s4); flush = (s0 == s1 && s1 == s2 && s2 == s3 && s3 == s4); have2 = (r0 == '2') + (r1 == '2') + (r2 == '2') + (r3 == '2') + (r4 == '2'); have3 = (r0 == '3') + (r1 == '3') + (r2 == '3') + (r3 == '3') + (r4 == '3'); have4 = (r0 == '4') + (r1 == '4') + (r2 == '4') + (r3 == '4') + (r4 == '4'); have5 = (r0 == '5') + (r1 == '5') + (r2 == '5') + (r3 == '5') + (r4 == '5'); have6 = (r0 == '6') + (r1 == '6') + (r2 == '6') + (r3 == '6') + (r4 == '6'); have7 = (r0 == '7') + (r1 == '7') + (r2 == '7') + (r3 == '7') + (r4 == '7'); have8 = (r0 == '8') + (r1 == '8') + (r2 == '8') + (r3 == '8') + (r4 == '8'); have9 = (r0 == '9') + (r1 == '9') + (r2 == '9') + (r3 == '9') + (r4 == '9'); haveT = (r0 == 'T') + (r1 == 'T') + (r2 == 'T') + (r3 == 'T') + (r4 == 'T'); haveJ = (r0 == 'J') + (r1 == 'J') + (r2 == 'J') + (r3 == 'J') + (r4 == 'J'); haveQ = (r0 == 'Q') + (r1 == 'Q') + (r2 == 'Q') + (r3 == 'Q') + (r4 == 'Q'); haveK = (r0 == 'K') + (r1 == 'K') + (r2 == 'K') + (r3 == 'K') + (r4 == 'K'); haveA = (r0 == 'A') + (r1 == 'A') + (r2 == 'A') + (r3 == 'A') + (r4 == 'A'); straight = 0; if (haveA && have2 && have3 && have4 && have5) straight = '5'; if (have2 && have3 && have4 && have5 && have6) straight = '6'; if (have3 && have4 && have5 && have6 && have7) straight = '7'; if (have4 && have5 && have6 && have7 && have8) straight = '8'; if (have5 && have6 && have7 && have8 && have9) straight = '9'; if (have6 && have7 && have8 && have9 && haveT) straight = 'T'; if (have7 && have8 && have9 && haveT && haveJ) straight = 'J'; if (have8 && have9 && haveT && haveJ && haveQ) straight = 'Q'; if (have9 && haveT && haveJ && haveQ && haveK) straight = 'K'; if (haveT && haveJ && haveQ && haveK && haveA) straight = 'A'; if (straight) { if (flush) { if (straight == 'A') { printf("royal flush.\n"); continue; } printf("straight flush, %c high.\n",straight); continue; } printf("straight, %c high.\n",straight); continue; } if (flush) printf("flush. "); if (haveA == 5) printf("five A's. "); if (haveK == 5) printf("five K's. "); if (haveQ == 5) printf("five Q's. "); if (haveJ == 5) printf("five J's. "); if (haveT == 5) printf("five T's. "); if (have9 == 5) printf("five 9's. "); if (have8 == 5) printf("five 8's. "); if (have7 == 5) printf("five 7's. "); if (have6 == 5) printf("five 6's. "); if (have5 == 5) printf("five 5's. "); if (have4 == 5) printf("five 4's. "); if (have3 == 5) printf("five 3's. "); if (have2 == 5) printf("five 2's. "); if (haveA == 4) printf("four A's. "); if (haveK == 4) printf("four K's. "); if (haveQ == 4) printf("four Q's. "); if (haveJ == 4) printf("four J's. "); if (haveT == 4) printf("four T's. "); if (have9 == 4) printf("four 9's. "); if (have8 == 4) printf("four 8's. "); if (have7 == 4) printf("four 7's. "); if (have6 == 4) printf("four 6's. "); if (have5 == 4) printf("four 5's. "); if (have4 == 4) printf("four 4's. "); if (have3 == 4) printf("four 3's. "); if (have2 == 4) printf("four 2's. "); threes = 0; if (haveA == 3) { printf("three A's. "); ++threes; } if (haveK == 3) { printf("three K's. "); ++threes; } if (haveQ == 3) { printf("three Q's. "); ++threes; } if (haveJ == 3) { printf("three J's. "); ++threes; } if (haveT == 3) { printf("three T's. "); ++threes; } if (have9 == 3) { printf("three 9's. "); ++threes; } if (have8 == 3) { printf("three 8's. "); ++threes; } if (have7 == 3) { printf("three 7's. "); ++threes; } if (have6 == 3) { printf("three 6's. "); ++threes; } if (have5 == 3) { printf("three 5's. "); ++threes; } if (have4 == 3) { printf("three 4's. "); ++threes; } if (have3 == 3) { printf("three 3's. "); ++threes; } if (have2 == 3) { printf("three 2's. "); ++threes; } pairs = 0; if (haveA == 2) { printf("pair of A's. "); ++pairs; } if (haveK == 2) { printf("pair of K's. "); ++pairs; } if (haveQ == 2) { printf("pair of Q's. "); ++pairs; } if (haveJ == 2) { printf("pair of J's. "); ++pairs; } if (haveT == 2) { printf("pair of T's. "); ++pairs; } if (have9 == 2) { printf("pair of 9's. "); ++pairs; } if (have8 == 2) { printf("pair of 8's. "); ++pairs; } if (have7 == 2) { printf("pair of 7's. "); ++pairs; } if (have6 == 2) { printf("pair of 6's. "); ++pairs; } if (have5 == 2) { printf("pair of 5's. "); ++pairs; } if (have4 == 2) { printf("pair of 4's. "); ++pairs; } if (have3 == 2) { printf("pair of 3's. "); ++pairs; } if (have2 == 2) { printf("pair of 2's. "); ++pairs; } if (haveA == 1) printf("A. "); if (haveK == 1) printf("K. "); if (haveQ == 1) printf("Q. "); if (haveJ == 1) printf("J. "); if (haveT == 1) printf("T. "); if (have9 == 1) printf("9. "); if (have8 == 1) printf("8. "); if (have7 == 1) printf("7. "); if (have6 == 1) printf("6. "); if (have5 == 1) printf("5. "); if (have4 == 1) printf("4. "); if (have3 == 1) printf("3. "); if (have2 == 1) printf("2. "); if (threes && pairs) printf("full house. "); printf("\n"); } return 0; }