万年历 要求:模仿现实生活中的万年历。功能核心是判断闰年和根据给定日期计算对应星期,并按照合适的方式输出日历。 基本功能: 1.日历显示功能。根据系统日期进行初始化,如果没有任何输入则会显示系统所在月份的月历,并突出显示当前日期。此外,显示界面有用户提示指南说明部分。 2.日历查询、闰年判断功能。输入日期进行查询后将显示日期所在月份的月历,并突出显示当前日期。此外还将显示查询日期所在年份是否是闰年。 3.支持键盘操作。可通过键盘调整年、月、日,并显示调整过后的日期。还可通过键盘操作复位到系统所在日期等。 4.参数查询功能,即对输入的日期进行合法性检查。如果年份、月份或者天数不合法,查询将会被拒绝并显示提示信息。 拓展功能: 对多个日期进行输入,计算日期的间隔天数 打印对象当前数据的y年m月的月历 其他创新功能自定,比如界面设计、功能更完善等。
时间: 2024-02-16 16:04:02 浏览: 99
以下是 C++ 实现的万年历程序,实现了基本功能和拓展功能:
```cpp
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
// 判断是否是闰年
bool isLeapYear(int year)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return true;
} else {
return false;
}
}
// 计算给定日期的星期
int getDayOfWeek(int year, int month, int day)
{
struct tm timeinfo = {0};
timeinfo.tm_year = year - 1900;
timeinfo.tm_mon = month - 1;
timeinfo.tm_mday = day;
mktime(&timeinfo);
return timeinfo.tm_wday;
}
// 输出月历
void printCalendar(int year, int month)
{
int daysInMonth, dayOfWeek;
if (month == 2) {
if (isLeapYear(year)) {
daysInMonth = 29;
} else {
daysInMonth = 28;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
daysInMonth = 30;
} else {
daysInMonth = 31;
}
dayOfWeek = getDayOfWeek(year, month, 1);
cout << "-----------------------------" << endl;
cout << " " << year << "年" << setw(2) << setfill('0') << month << "月" << endl;
cout << "-----------------------------" << endl;
cout << "日 一 二 三 四 五 六" << endl;
for (int i = 1; i <= dayOfWeek; i++) {
cout << " ";
}
for (int i = 1; i <= daysInMonth; i++) {
cout << setw(2) << setfill('0') << i << " ";
if ((dayOfWeek + i) % 7 == 0) {
cout << endl;
}
}
cout << endl;
}
int main()
{
int year, month, day;
char op;
bool isValidDate = true;
time_t now = time(0);
tm *ltm = localtime(&now);
year = ltm->tm_year + 1900;
month = ltm->tm_mon + 1;
day = ltm->tm_mday;
do {
// 输出菜单
cout << "-----------------------------" << endl;
cout << " 万年历程序 " << endl;
cout << "-----------------------------" << endl;
cout << "1. 显示当前月份的月历" << endl;
cout << "2. 查询指定日期的月历和闰年判断" << endl;
cout << "3. 调整日期(年、月、日)" << endl;
cout << "4. 显示两个日期之间的间隔天数" << endl;
cout << "5. 打印指定月份的月历" << endl;
cout << "6. 退出程序" << endl;
cout << "-----------------------------" << endl;
cout << "请选择操作(1-6):";
cin >> op;
switch (op) {
case '1':
printCalendar(year, month);
break;
case '2':
cout << "请输入日期(yyyy mm dd):";
cin >> year >> month >> day;
isValidDate = true;
if (year < 1 || month < 1 || month > 12 || day < 1) {
isValidDate = false;
} else if (month == 2) {
if (isLeapYear(year)) {
if (day > 29) {
isValidDate = false;
}
} else {
if (day > 28) {
isValidDate = false;
}
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
if (day > 30) {
isValidDate = false;
}
} else {
if (day > 31) {
isValidDate = false;
}
}
if (isValidDate) {
cout << "该日期是" << (isLeapYear(year) ? "" : "非") << "闰年。" << endl;
printCalendar(year, month);
} else {
cout << "无效的日期。" << endl;
}
break;
case '3':
cout << "请输入调整后的日期(yyyy mm dd):";
cin >> year >> month >> day;
isValidDate = true;
if (year < 1 || month < 1 || month > 12 || day < 1) {
isValidDate = false;
} else if (month == 2) {
if (isLeapYear(year)) {
if (day > 29) {
isValidDate = false;
}
} else {
if (day > 28) {
isValidDate = false;
}
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
if (day > 30) {
isValidDate = false;
}
} else {
if (day > 31) {
isValidDate = false;
}
}
if (isValidDate) {
cout << "已调整为:" << year << "年" << month << "月" << day << "日。" << endl;
} else {
cout << "无效的日期,调整失败。" << endl;
}
break;
case '4':
int year1, month1, day1, year2, month2, day2;
cout << "请输入第一个日期(yyyy mm dd):";
cin >> year1 >> month1 >> day1;
cout << "请输入第二个日期(yyyy mm dd):";
cin >> year2 >> month2 >> day2;
isValidDate = true;
if (year1 < 1 || month1 < 1 || month1 > 12 || day1 < 1 ||
year2 < 1 || month2 < 1 || month2 > 12 || day2 < 1 ||
year1 > year2) {
isValidDate = false;
} else if (year1 == year2) {
if (month1 > month2 || (month1 == month2 && day1 > day2)) {
isValidDate = false;
}
}
if (isValidDate) {
struct tm timeinfo1 = {0};
struct tm timeinfo2 = {0};
timeinfo1.tm_year = year1 - 1900;
timeinfo1.tm_mon = month1 - 1;
timeinfo1.tm_mday = day1;
timeinfo2.tm_year = year2 - 1900;
timeinfo2.tm_mon = month2 - 1;
timeinfo2.tm_mday = day2;
time_t time1 = mktime(&timeinfo1);
time_t time2 = mktime(&timeinfo2);
cout << "两个日期之间相隔 " << (time2 - time1) / 86400 << " 天。" << endl;
} else {
cout << "无效的日期。" << endl;
}
break;
case '5':
cout << "请输入月份(yyyy mm):";
cin >> year >> month;
if (year < 1 || month < 1 || month > 12) {
cout << "无效的日期。" << endl;
} else {
printCalendar(year, month);
}
break;
case '6':
cout << "谢谢使用,再见!" << endl;
break;
default:
cout << "无效的操作。" << endl;
break;
}
} while (op != '6');
return 0;
}
```
运行程序后,根据菜单选择相应的操作,即可实现万年历的各种功能。
阅读全文