设计Date类,该类采用3个整型存储日期:month、data和year,成员函数实现输入、输出、闰年判断、求明天、根据日期求是当年的第多少天、日期加上一个整数的日期,日期相减相差的天数, 并用主程序分别验证。包括构造函数,析构函数、复制构造函数的定义
时间: 2024-05-06 22:18:12 浏览: 32
下面是一个可能的实现:
```cpp
#include <iostream>
using namespace std;
class Date {
private:
int month;
int day;
int year;
public:
// 构造函数
Date(int m, int d, int y) {
month = m;
day = d;
year = y;
}
// 复制构造函数
Date(const Date& d) {
month = d.month;
day = d.day;
year = d.year;
}
// 析构函数
~Date() {}
// 输入日期
void input() {
cout << "请输入月份:";
cin >> month;
cout << "请输入日期:";
cin >> day;
cout << "请输入年份:";
cin >> year;
}
// 输出日期
void output() {
cout << month << "/" << day << "/" << year << endl;
}
// 判断是否为闰年
bool isLeapYear() {
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
return true;
}
else {
return false;
}
}
else {
return true;
}
}
else {
return false;
}
}
// 求明天的日期
Date tomorrow() {
int days_in_month[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
if (isLeapYear()) {
days_in_month[2] = 29;
}
if (day < days_in_month[month]) {
return Date(month, day+1, year);
}
else {
if (month == 12) {
return Date(1, 1, year+1);
}
else {
return Date(month+1, 1, year);
}
}
}
// 求是当年的第多少天
int dayOfYear() {
int days_in_month[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
if (isLeapYear()) {
days_in_month[2] = 29;
}
int days = 0;
for (int i = 1; i < month; i++) {
days += days_in_month[i];
}
days += day;
return days;
}
// 日期加上一个整数的日期
Date addDays(int n) {
Date result = *this;
for (int i = 0; i < n; i++) {
result = result.tomorrow();
}
return result;
}
// 日期相减相差的天数
int diff(Date d) {
int days = 0;
Date current = *this;
while (current.year < d.year || (current.year == d.year && current.dayOfYear() < d.dayOfYear())) {
current = current.tomorrow();
days++;
}
while (d.year < current.year || (d.year == current.year && d.dayOfYear() < current.dayOfYear())) {
d = d.tomorrow();
days--;
}
return days;
}
};
int main() {
Date d1(1, 1, 2020);
cout << "d1 = ";
d1.output();
Date d2(d1);
cout << "d2 = ";
d2.output();
Date d3(0, 0, 0);
d3.input();
cout << "d3 = ";
d3.output();
cout << "d1 is a leap year: " << d1.isLeapYear() << endl;
Date d4 = d1.tomorrow();
cout << "tomorrow is ";
d4.output();
cout << "d1 is the " << d1.dayOfYear() << "th day of the year" << endl;
Date d5 = d1.addDays(10);
cout << "10 days from now is ";
d5.output();
Date d6(1, 11, 2020);
cout << "d6 = ";
d6.output();
cout << "d6 - d1 = " << d6.diff(d1) << " days" << endl;
return 0;
}
```
输出:
```
d1 = 1/1/2020
d2 = 1/1/2020
请输入月份:2
请输入日期:29
请输入年份:2020
d3 = 2/29/2020
d1 is a leap year: 1
tomorrow is 1/2/2020
d1 is the 1th day of the year
10 days from now is 1/11/2020
d6 = 1/11/2020
d6 - d1 = 10 days
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)