代码如下:
//*******万年历
#include "pch.h"
#include <iostream>
using namespace std;
bool isleap(int year);//判断平润年
int week_of_new_year_day(int year);//判断星期几
int main()
{
int year,month,day,weekday;
cout << "请输入年份:\n";
cin >> year;
cout << "\n ******** " << year << " *******\n";
weekday = week_of_new_year_day(year);
for (month=1; month<=12; month++)
{
cout << "\n " << month <<"月"<< endl;
cout << "---------------------------------------|\n";
cout << "Sun Mon Tue Wed Thu Fri Sat|\n";
cout << "---------------------------------------|\n";
for (int j = 0; j < weekday; j++)
{
cout << " ";
}
if (month == 4 || month == 6 || month == 9 || month == 11)
day = 30;
else if (month == 2)
{
if (isleap(year)) day = 29;
else day = 28;
}
else day = 31;
for (int i = 1; i <= day; i++)
{
if (i <= 9) cout << " " << i<<" ";
else cout << i<<" ";
weekday++;
if (weekday==7)
{
weekday = 0;
cout << "\n";
}
}
cout << "\n";
}
return 0;
}
bool isleap(int year)
{
bool k=0;
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
k = 1;
return k;
}
int week_of_new_year_day(int year)
{
int n;
n = year - 1900;
n = n + (n -1)/4 + 1;
n = n % 7;
return n;
}