Date operator

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Date
{
public:
    Date(int year=1900, int month=2, int day=0) {
        this->_year = year;
        this->_month = month;
        this->_day = day;
    }
    bool operator==(const Date& d) {
        if (_year > d._year) {
            return true;
        }
        else if (_year == d._year && _month > d._month) {
            return true;
        }
        else if (_year == d._year && _month == d._month && _day >d._day) {
            return true;
        }
        else {
            return false;
        }
    }
private:
    int _year;int _month;int _day;
};
int main() {
    Date d(2018,9,26);
    Date d1(2018, 9, 26);
    cout << (d == d1) << endl;
    return 0;
}

#include <iostream> #include <string> #include <assert.h> using namespace std; //在此处补充Date类的定义 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; }}纠正这个代码使得云行不出现错误输出: 2022-08-31 + 2 = 2022-09-02 2022-08-31 - 2 = 2022-08-29 期望输出: 2022-8-31 + 2 = 2022-9-2 2022-8-31 - 2 = 2022-8-29
05-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值