【C】随机抽取塔罗牌的小程序

目录

头文件内容

源文件内容

#程序主体

#内部函数

1.菜单函数

2.抽卡函数

part 1 确定抽几张牌

part 2 洗牌

part 3 用常量指针数组储存塔罗牌名称

part 4 塔罗牌正位\逆位的生成,以及结果的打印

重点

1.生成伪随机数

2.指针数组


头文件内容

#define _CRT_SECURE_NO_WARNINGS 1 
#include<stdio.h>
#include<stdlib.h>  //包含srand、rand函数声明
#include<time.h>  //包含time函数声明

源文件内容

#程序主体

主菜单循环

int main()
{
	int  input = 0;
	do //采用do……while循环,保证菜单能直接打印出来(至少打印一次)
	{
		menu(); //【菜单函数】
		printf("Please select[Input: 1/0]:>\n");
		scanf("%d", &input);
		switch (input)
		{
      输入1开始抽卡
		case 1: 
			printf("*DRAW CARDs*\n");
			tarot(); //【抽卡函数】
			break;
      输入0直接退出
		case 0:
			printf("EXIT:<\n");
			break;
      输入其他数报错
		default:
			printf("Error, please reselect:>\n");
		}
	} while (input);//抽完一次或输错数自动跳回主菜单 //输0直接跳出
	return 0;
}

#内部函数

1.菜单函数

打印菜单

void menu()
{
    printf("***************************\n");
    printf("		\n");
    printf("	1. DRAW CARDs\n");
    printf("		\n");
    printf("	0. EXIT\n");
    printf("		\n");
    printf("		\n");
    printf("***************************\n");
}
2.抽卡函数
void tarot()
{
//part 1/
	//确定抽几张牌
	int cardnum = 0;
	printf("The number of cards drawn:");
	scanf("%d", &cardnum);
//part 2/
	//初始化数组card[]
	int card[79] = { 0 };
	for (int i = 0; i < 79; i++) 
	{
		card[i] = i;
	}
	//随机打乱数组card[]
	for (int i = 0; i < 79; i++) 
	{
		srand(time(NULL)); //初始化随机数种子
		for (int i = 78; i > 0; i--) 
		{
			int j = rand() % (i + 1); //打乱范围逐步缩小,后面已经打乱过的数不变
			int temp = card[i];
			card[i] = card[j];
			card[j] = temp;
		}
	}
//part 3/
	const char* tarotcard[79] = {
		"Fool", "Magician", "High Priestess", "Empress", "Emperor",
		"Hierophant", "Lovers", "Chariot", "Strength", "Hermit",
		"Wheel of Fortune", "Justice", "Hanged Man", "Death", "Temperance",
		"Devil", "Tower", "Star", "Moon", "Sun",
		"Judgement", "World", "King of Swords", "Queen of Swords", "Knight of Swords",
		"Page of Swords", "Ace of Swords", "Two of Swords", "Three of Swords", "Four of Swords",
		"Five of Swords", "Six of Swords", "Seven of Swords", "Eight of Swords", "Nine of Swords",
		"Ten of Swords", "King of Wands", "Queen of Wands", "Knight of Wands", "Page of Wands",
		"Ace of Wands", "Two of Wands", "Three of Wands", "Four of Wands", "Five of Wands",
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值