这是一个使用C++语言编写的万年历程序,可以让用户输入年份和月份来查看对应的日历。
#include <iostream>
using namespace std;
int main() {
int year, month, daysInMonth, startDay;
cout << "请输入年份:";
cin >> year;
cout << "请输入月份:";
cin >> month;
// 计算该月有多少天
if (month == 2) {
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
daysInMonth = 29;
} else {
daysInMonth = 28;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
daysInMonth = 30;
} else {
daysInMonth = 31;
}
// 计算该月第一天是星期几
int century = year / 100;
int yearOfCentury = year % 100;
startDay = (1 + 2 * (3 - (century % 4))) * (yearOfCentury + yearOfCentury / 4) % 7;
// 输出日历
cout << "日\t一\t二\t三\t四\t五\t六\n";
for (int i = 0; i < startDay; i++) {
cout << "\t";
}
for (int i = 1; i <= daysInMonth; i++) {
cout << i << "\t";
if ((i + startDay) % 7 == 0) {
cout << endl;
}
}
cout << endl;
return 0;
}
</iostream>
暂无评论