</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;
}
}
[c++]万年历
最新推荐文章于 2024-01-28 20:12:56 发布