C++计算日期两个经典算法,返回年总天数及年天数.

作者在 2020-01-16 12:02:58 发布以下内容

[附件1406]


int 返回月的天数(int 年, int 月)
{//缘由https://bbs.csdn.net/topics/395074486
	return (月 == 2 ? 
		((((!(年 % 4) && 年 % 100) || !(年 % 400)) ? 1 : 0) ? 29 : 28) :
		(((月 <= 7 && 月 % 2) || (月>7 && !(月 % 2))) ? 31 : 30));
}
int 返回年总天数(int 年)
{
	return ((年) ? 365 * 年 + ((年 / 4) - (年 / 100) + (年 / 400)) : 0);
}

	int a = 24;
	cout << "该年天数" << (返回年总天数(a) - 返回年总天数(a - 1)) 
		<< "\t总天数" << 返回年总天数(a-1) 
		<<"\t周几"<< (返回年总天数(a-1)+1) % 7 << endl;
	int n = a, y = 1, r = 1, nn = n;
	while (n)
	{
		while (nn)if (--y)r += 返回月的天数(nn, y); else --nn, y = 13;
		cout << n << "总天数" << r << "年1月1日是星期" << r % 7 << endl;//if (r % 7 == 1)
		nn = --n, y = 1, r = 1;
	}


默认分类 | 阅读 1648 次
文章评论,共0条
游客请输入验证码