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

作者在 2020-02-02 06:29:38 发布以下内容
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;

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