[c++]万年历

</pre><pre name="code" class="cpp">#include<iostream>
#include<iomanip>

using namespace std;

void main()
{
	int monthDay[12] = { 31,0,31,30,31,30,31,31,30,31,30,31 }; //确定每个月的天数

		cout << "please enter the year" << endl;
	int year;
	cin >> year;

	if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
		monthDay[1] = 29;
	else
		monthDay[1] = 28;                                 //判断是否为闰年

		int startDay;
	startDay = ((year - 1) + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + 1) % 7; //判断这一年第一天是星期几
		//cout << startDay << endl;

	int count = 1;
	int day = 1;
	int temp;
	for (int i = 0; i < 12; i++)
	{
		cout << "Mon   Tue   Wed   Thu   Fri   Sat   Sun" << endl; //打印每个月的星期
			while (count<startDay)
			{
				cout << setw(6) << "     ";                        //每个月开始空的几天放空格
					count++;
			}
		while (day <= monthDay[i])                                  //判断日期是否超过当月天数
		{
			while (count <= 7)				     //判断是否超出一周7天
			{
				if (day <= monthDay[i])
				{
					cout << setw(3) << day << "   ";     //对齐
						day++;
					count++;
					temp = count;
				}
				else                                          // 完成这一周的打印强制退出
				{
					temp = count;
		count = 8;
				}
			}
		cout << endl;                                          //一周换行
			count = 1;
		}
		cout << endl;                                                   //一月换行
			day = 1;                                                       //一个月结束重置日期
			startDay = temp;
	}
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值