【VC++】万年历算法代码。根据年月日求星期。。

本文介绍了一个用于判断闰年的函数 BOOLIsLeapYear,并详细解释了如何使用此函数计算不同月份的天数以及日期所在的一周中的位置。通过简单的数学逻辑和条件判断,实现了对日期基本操作的理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

BOOL IsLeapYear(unsigned int nYear)
{
	if((nYear % 4 ==0 && nYear % 400 != 0) ||(nYear % 400 == 0))
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}
unsigned int MonthOfDays(unsigned int nYear,unsigned int nMonth)
{
	switch(nMonth)
	{
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
		{
			return 31;
		}
	case 4:
	case 6:
	case 9:
	case 11:
		{
			return 30;
		}
	case 2:
		{
			if (IsLeapYear(nYear))
			{
				return 29;
			}
			else
			{
				return 28;
			}
		}
	}
}
unsigned int DayOfWeeks(unsigned int nYear,unsigned int nMonth,unsigned int nDay)
{
	unsigned int nCount = 0;
	for (unsigned int i = 1;i < nMonth;i ++)
	{
		nCount += MonthOfDays(nYear,nMonth);
	}
	nCount += nDay;
	
	float fVal = nYear - 1 + (float)(nYear - 1) / 4 + (float)(nYear - 1) / 100 +(float)(nYear - 1) / 400 - 40 + nCount;

	return (int) fVal % 7; 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值