CH11_1
void pay_amount(int dollars, int* twenties, int* tens, int* fives, int* ones)
{
*twenties = dollars / 20;
*tens = dollars % 20 / 10;
*fives = dollars % 20 % 10 / 5;
*ones = dollars % 20 % 10 % 5 / 1;
}
CH11_2
void find_closest_flight(int desired_time, int* departure_time, int* arrival_time)
{
if (desired_time <= ((8 * 60) + (103 / 2))) {
*departure_time = 8 * 60;
*arrival_time = 10 * 60 + 16;
}
else if (desired_time < ((9 * 60) + 43) + (96 / 2)) {
*departure_time = 9 * 60 + 43;
*arrival_time = 11 * 60 + 52;
}
else if (desired_time < ((11 * 60) + 19) + (88 / 2)) {
*departure_time = 11 * 60 + 19;
*arrival_time = 13 * 60 + 31;
}
else if (desired_time <= ((12 * 60) + 47) + (73 / 2)) {
*departure_time = 12 * 60 + 47;
*arrival_time = 15 * 60;
}
else if (desired_time <= ((14 * 60) + (105 / 2))) {
*departure_time = 14 * 60;
*arrival_time = 15 * 60 + 8;
}
else if (desired_time <= ((15 * 60) + 45) + (195 / 2)) {
*departure_time = 15 * 60 + 45;
*arrival_time = 17 * 60 + 55;
}
else if (desired_time <= ((19 * 60) + (165 / 2))) {
*departure_time = 19 * 60;
*arrival_time = 21 * 60 + 20;
}
else {
*departure_time = 21 * 60 + 45;
*arrival_time = 23 * 60 + 58;
}
}
CH11_3
void reduce(int numerator, int denominator, int* reduced_numerator, int* reduced_denominator)
{
int t;
if (numerator == 0) {
printf("In lowest terms: 0\n");
exit (0);
}
*reduced_numerator = numerator;
*reduced_denominator = denominator;
while (denominator != 0) {
t = denominator;
denominator = numerator % denominator;
numerator = t;
}
*reduced_numerator = *reduced_numerator / numerator;
*reduced_denominator = *reduced_denominator / numerator;
}
CH11_4
#include <stdbool.h> /* C99 only */
#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);
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, or 2 */
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);
}
}
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++;
}
}
}
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)
*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++;
}
}
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");
}