编译器:C++ (g++)改造练习13-1(日复一日)中的Date类并提交,使其可以与一个整数n相加或相减,得到该日期N天后/前的日期。

编译器:C++ (g++)

改造练习13-1(日复一日)中的Date类并提交,使其可以与一个整数n相加或相减,得到该日期N天后/前的日期。

提示:

  1. 请参考题目(日复一日)中的Date类实现;

  2. 注意考虑闰月;

  3. 整数n的取值范围为[1,10000]。

裁判测试程序样例:

#include <iostream>

#include <string>

#include <assert.h>

using namespace std;

//在此处补充Date类的定义

int main()

{

    int y, m, d;

    cin >> y >> m >> d;

    Date d1(y,m,d);

    int n;

    cin >> n;

    cout << d1.toText() << " + " << n << " = " << (d1 + n).toText() << endl;

    cout << d1.toText() << " - " << n << " = " << (d1 - n).toText() << endl;

    return 0;

}

输入样例:

2022 8 31

2

说明:意为求2022年8月31日的后两天和前两天的日期。

输出样例:

2022-8-31 + 2 = 2022-9-2

2022-8-31 - 2 = 2022-8-29

请注意:函数题只需要提交相关代码片段,不要提交完整程序。


#include  <iostream>
#include  <string>
#include  <assert.h>
using  namespace  std;

//在此处补充Date类的定义
class Date {
public:
    Date(int year, int month, int day) : year_(year), month_(month), day_(day) {}
    int getYear() const { return year_; }
    int getMonth() const { return month_; }
    int getDay() const { return day_; }
    string toText() const {
        string str = to_string(year_) + "-" + to_string(month_) + "-" + to_string(day_);
        return str;
    }
    Date operator+(int n) const {
        int daysInMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
        int days = day_ + n;
        int month = month_;
        int year = year_;
        while (days > daysInMonth[month - 1]) {
            if (month == 2 && isLeapYear()) {
                if (days > 29) {
                    days -= 29;
                    month++;
                } else {
                    break;
                }
            } else {
                days -= daysInMonth[month - 1];
                month++;
            }
            if (month > 12) {
                year++;
                month = 1;
            }
        }
        return Date(year, month, days);
    }
    Date operator-(int n) const {
        int days = day_ - n;
        int month = month_;
        int year = year_;
        while (days <= 0) {
            if (month == 3 && isLeapYear()) {
                days += 29;
                month--;
            } else {
                month--;
                if (month == 0) {
                    year--;
                    month = 12;
                }
                days += daysInMonth(month - 1);
            }
        }
        return Date(year, month, days);
    }
private:
    int year_, month_, day_;
    bool isLeapYear() const {
        if (year_ % 4 == 0 && year_ % 100 != 0 || year_ % 400 == 0) {
            return true;
        } else {
            return false;
        }
    }
    int daysInMonth(int month) const {
        if (month == 1 && isLeapYear()) {
            return 29;
        } else {
            return daysInMonth_[month];
        }
    }
    static const int daysInMonth_[12];
};

const int Date::daysInMonth_[12] = {31,28,31,30,31,30,31,31,30,31,30,31};



int  main()
{
        int  y,  m,  d;
        cin  >>  y  >>  m  >>  d;
        Date  d1(y,m,d);

        int  n;
        cin  >>  n;

        cout  <<  d1.toText()  <<  "  +  "  <<  n  <<  "  =  "  <<  (d1  +  n).toText()  <<  endl;
        cout  <<  d1.toText()  <<  "  -  "  <<  n  <<  "  =  "  <<  (d1  -  n).toText()  <<  endl;
        return  0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
```cpp #include <iostream> #include <string> #include <assert.h> using namespace std; class Date { private: int year, month, day; static const int monthDays[13]; public: Date(int y = 1, int m = 1, int d = 1) { year = y; month = m; day = d; } Date(const Date& date) { year = date.year; month = date.month; day = date.day; } Date& operator=(const Date& date) { year = date.year; month = date.month; day = date.day; return *this; } bool isLeap() const { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } int monthDaysNum() const { if (month == 2 && isLeap()) { return 29; } return monthDays[month]; } void addOneDay() { day++; if (day > monthDaysNum()) { day = 1; month++; if (month > 12) { month = 1; year++; } } } void minusOneDay() { day--; if (day == 0) { month--; if (month == 0) { month = 12; year--; } day = monthDaysNum(); } } string toText() const { string res = ""; res += to_string(year) + "-"; if (month < 10) { res += "0"; } res += to_string(month) + "-"; if (day < 10) { res += "0"; } res += to_string(day); return res; } Date operator+(int n) const { Date res = *this; while (n--) { res.addOneDay(); } return res; } Date operator-(int n) const { Date res = *this; while (n--) { res.minusOneDay(); } return res; } }; const int Date::monthDays[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; int main() { int y, m, d; cin >> y >> m >> d; Date d1(y,m,d); int n; cin >> n; cout << d1.toText() << " + " << n << " = " << (d1 + n).toText() << endl; cout << d1.toText() << " - " << n << " = " << (d1 - n).toText() << endl; return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

那不勒斯的萤火丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值