#include <iostream> using namespace std; // 判断闰年 bool isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } class Date { private: int year; int month; int day; public: // 构造函数 Date(int y = 1, int m = 1, int d = 1) : year(y), month(m), day(d) {} // 设置日期 void setDate(int y, int m, int d) { year = y; month = m; day = d; } // 验证日期有效性 bool isValid() { if (month < 1 || month > 12) return false; if (day < 1) return false; static int daysInMonth[] = { 31, 28 + isLeapYear(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (day > daysInMonth[month - 1]) return false; return true; } // 输出日期 void printDate() const { cout << month << "月" << day << "日"; } // 返回一年中的第几天 int getDayOfYear() const { static int cumulativeDays[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; int result = cumulativeDays[month - 1] + day; if (isLeapYear(year) && month > 2) { ++result; } return result; } }; int main() { Date date1(2019, 2, 24); if (!date1.isValid()) { cout << "无效日期!" << endl; } else { date1.printDate(); cout << "是" << date1.getYear() << "年的第" << date1.getDayOfYear() << "天" << endl; } Date date2(2019, 7, 1); if (!date2.isValid()) { cout << "无效日期!" << endl; } else { date2.printDate(); cout << "是" << date2.getYear() << "年的第" << date2.getDayOfYear() << "天" << endl; } return 0; }实现日期交换的功能,将两个日期对象的日期值交换,
时间: 2025-03-15 09:04:42 浏览: 35
要在现有的 `Date` 类基础上实现日期交换功能,可以通过以下步骤完成。我们将新增一个方法来帮助两个日期对象相互交换它们的年、月、日数据。
### 步骤说明
1. **添加成员函数**:我们可以在类内定义一个新的非静态成员函数 `swapDates` 用于内部的数据交互。
2. **外部调用封装**:为了让代码更简洁优雅,我们可以再额外提供一个公共静态接口 `exchangeDates` 来供用户直接调用并传入两日期对象引用。
下面是基于您给出代码的具体改进版本:
#### 修改后的 `Date` 类代码
```cpp
// 添加了新的头文件 iostream 和命名空间 using declaration.
#include <iostream>
using namespace std;
// 判断闰年函数保持不变
bool isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
class Date {
private:
int year;
int month;
int day;
public:
// 构造函数初始化部分也保留原样未改。
Date(int y = 1, int m = 1, int d = 1) : year(y), month(m), day(d) {}
// 设置日期同样维持原有结构。
void setDate(int y, int m, int d) {
year = y;
month = m;
day = d;
}
// 这里加入了一个新方法——判断当前设置是否合法的检查。
bool isValid() {
if (month < 1 || month > 12) return false;
if (day < 1) return false;
static int daysInMonth[] = { 31, 28 + isLeapYear(year), 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
if (day > daysInMonth[month - 1]) return false;
return true;
}
// 输出打印依然沿用了之前的方式输出格式化字符串形式展示出来。
void printDate() const {
cout << year << "年" << month << "月" << day << "日";
}
// 原有的获取一年中哪一天算法逻辑继续存在不做改动。
int getDayOfYear() const {
static int cumulativeDays[] = { 0, 31, 59, 90, 120, 151, 181,
212, 243, 273, 304, 334 };
int result = cumulativeDays[month - 1] + day;
if (isLeapYear(year) && month > 2){
++result;
}
return result;
}
// 新增:用于交换自身和其他实例间数据的方法swapDates()
void swapDates(Date &other){
int tmp_year = this->year;
int tmp_month = this->month;
int tmp_day = this->day;
this->setDate(other.year, other.month, other.day);
other.setDate(tmp_year, tmp_month, tmp_day);
}
// 另外提供给外界使用的简单易记的名字即 exchangeDates 的静态辅助方法。
static void exchangeDates(Date& A, Date& B){
A.swapDates(B);
}
};
int main(){
// 示例测试内容开始...
Date first_date(2019, 2, 24);
Date second_date(2020, 7, 1);
// 先分别显示这两个初始状态下的时间点情况以便对比查看效果区别。
cout << "First date before swapping:" ;
first_date.printDate();cout<<"\n";
cout << "Second date before swapping:";
second_date.printDate();cout<<"\n\n";
// 看到此处就是关键地方啦! 使用我们的自定义工具把两者进行对换位置动作咯~
Date::exchangeDates(first_date ,second_date );
// 最后再重新确认一遍经过上述操作之后各自的最新数值状况如何吧?
cout << "\nAfter Swap:\n";
cout << "New First date after swapped to Second's previous value :";
first_date.printDate();cout<<"\n";
cout << "And New Second date now becomes Original one of the Former side.";
second_date.printDate();cout<<"\n";
return 0;
}
```
---
### 输出解释
在上面程序运行完后会看到类似下面这样的结果呈现给你看哦!
原始第一第二个日子分别为“2019-2-24”以及“2020-7-1”。然后执行完毕互相切换命令以后就会变成相反顺序的关系啦~
比如原先第一个现在变作了后者之前的设定;同理原来第二者此刻成为了前者先前记录下来的时刻啰~
---
阅读全文
相关推荐



















