该题处自C语言程序设计与方法
其中含有bool类型数组的应用
/*程序负责发一副标准纸牌,每张标准纸牌都有一个花色和一个等级。
程序需要用户指明手里应该握有几张牌*/
#include <stdio.h>
#include <stdlib.h> //srand 和 rand 函数都出于此
#include <stdbool.h>
#include <time.h> //time 函数出于此
#define NUM_SUITS 4
#define NUM_RANKS 13
int main(void)
{
bool in_hand[NUM_SUITS][NUM_RANKS] = {false};
int num_cards,rank,suit;
const char rank_code[] = {'2','3','4','5','6','7','8','9','t','j','q','k','a'};
const char suit_code[] = {'c','d','h','s'}; //代表四张牌的花色
srand((unsigned)time(NULL));
printf("Enter number of cards in hand: ");
scanf("%d",&num_cards);
printf("Your cards are : ");
while(num_cards>0){
suit = rand() % NUM_SUITS;
rank = rand() % NUM_RANKS;
if(!in_hand[suit][rank]){
in_hand[suit][rank] = true;
num_cards --;
printf(" %c %c",suit_code[suit],rank_code[rank]);
}
printf("\n");
}
//printf("\n");
return 0;
}