编程语言
首页 > 编程语言> > 【C语言程序设计第四版】例11-6代码

【C语言程序设计第四版】例11-6代码

作者:互联网

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct card{
    int suit;
    int face;
};

void deal(struct card *wdeck){
    int i , m, t;
    static int temp[52] = {0};
    
    srand(time(NULL));   // 放入时间戳作为随机种子
    for (i = 0; i < 52; i++) {
        while (1) {
            m = rand() % 52;
            if (temp[m] == 0) {
                break;
            }
        }
        temp[m] =  1;     // 找出一张还没定义的排
        // 发牌的逻辑设计巧妙,值得学习
        t = (i % 4) * 13 + (i /4);
        wdeck[t].suit = m / 13;
        wdeck[t].face = m % 13;
    }
}

int main(void){
    
    int i;
    struct card deck[52];
    const char *suit[] = {"Heart", "Diamond", "Club", "Spade"};
    const char *face[] = {"A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2"};
    
    deal(deck);
    for (i=0; i<52; i++) {
        if (i % 13 == 0) {
            printf("Player %d:\n", i/13+1);
        }
        printf("%s of %s\n", face[deck[i].face], suit[deck[i].suit]);
    }
    
    return 0;
}

 

标签:11,include,struct,int,52,第四版,suit,C语言,wdeck
来源: https://www.cnblogs.com/sidianok/p/15334622.html