日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C語言實現隨機抽取紙牌程序_C 語言

作者:風葉翩翩 ? 更新時間: 2022-05-28 編程語言

本文實例為大家分享了C語言實現隨機抽取紙牌的具體代碼,供大家參考,具體內容如下

程序設計要求

本程序負責發一副標準紙牌,每張標準紙牌都有一種花色(梅花、方塊、黑桃、紅桃)和一個等級(2,3,4,5,6…K,A)。程序需要用戶指明手機有幾張牌。

程序設計流程

1 . 使用庫函數和時間函數,用time函數返回當前時間,用一個數表示,srand函數初始化C語言的隨機數生成器。通過把time函數返回值傳遞給srand可以避免程序每次運行發同樣的牌。rand函數產生隨機數,通過%縮放。

2 . 使用二位數組來進行數據記錄。4行表示每種花色,13列表示每種等級。

3 . 程序開始時,數組元素都為false,每隨機抽取一張紙牌時,檢查in_hand對應元素真假,如果為真,則抽取其他紙牌,如果為假,記錄到數組元素當中,提醒我們這張牌已經記錄過了。

效果展示

完整代碼

#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
#include <time.h>
#include <stdlib.h>
?
?
# define num_rates ((int) (sizeof(value)/sizeof(value[0])))
# define initial_balance 100.00
??
#define num_suits 4
#define num_ranks 13
??
int main(){
?
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'};
printf("enter number\n");
scanf("%d",&num_cards);
?
printf("your hands\n");
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",rank_code[rank],suit_code[suit]);
?}
}
printf("\n");
return 0;
}

原文鏈接:https://blog.csdn.net/weixin_45743799/article/details/104536686

欄目分類
最近更新