题解 | #计算日期到天数转换#

计算日期到天数转换

https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded

#include<iostream>
using namespace std;
class Date {
  public:
    // 获取某年某月的天数
    int GetMonthDay(int year, int month);
    int Getday(Date& t);//获取当前的所有天数
    void prin() {
        cout << _year << "-";
        if (_month < 10) {
            cout << "0" << _month << "-";
        } else {
            cout << _month << "-";
        }
        if (_day < 10) {
            cout << "0" << _day << endl;
        } else {
            cout << _day << endl;
        }
    }
    // 全缺省的构造函数
    Date(int year = 1900, int month = 1, int day = 1)
        : _year(year),
          _month(month),
          _day(day) {
        ;
    }
    // 拷贝构造函数
    // d2(d1)
    Date(const Date& d) {
        _year = d._year;
        _month = d._month;
        _day = d._day;
    }
    // 赋值运算符重载
    // d2 = d3 -> d2.operator=(&d2, d3)
    Date& operator=(const Date& d);
    // 析构函数

    // 日期+=天数
    Date& operator+=(int day);
    // 日期+天数
    Date operator+(int day);
    // 日期-天数
    Date operator-(int day);
    // 日期-=天数
    Date& operator-=(int day);
    // 前置++
    Date& operator++();
    // 后置++
    Date operator++(int);
    // 后置--
    Date operator--(int);
    // 前置--
    Date& operator--();
    // >运算符重载
    bool operator>(const Date& d);
    // ==运算符重载
    bool operator==(const Date& d);
    // >=运算符重载
    bool operator >= (const Date& d);
    // <运算符重载
    bool operator < (const Date& d);
    // <=运算符重载
    bool operator <= (const Date& d);
    // !=运算符重载
    bool operator != (const Date& d);
    // 日期-日期 返回天数
    int operator-(const Date& d);
  private:
    int _year;
    int _month;
    int _day;
};
int month[13] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
int day[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day2[13] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
//日期+=天数
Date& Date::operator+=(int day) {
    _day += day;
    while (_day > GetMonthDay(_year, _month)) {
        _day -= GetMonthDay(_year, _month);
        _month++;
        if (_month == 13) {
            _month = 1;
            _year++;
        }
    }
    return *this;
}
// 日期+天数
Date Date::operator+(int day) {
    Date tmp(*this);
    tmp += day;
    return tmp;
}
// 日期-天数
Date Date::operator-(int day) {
    Date tmp(*this);
    tmp -= day;
    return tmp;
}
// 日期-=天数
Date& Date:: operator-=(int day) {
    _day -= day;
    if (_day < 0) {
        _day += GetMonthDay(_year, _month);
        _month - 1;
        if (_month == 0) {
            _month = 12;
            _year = _year - 1;
        }
    }
    return *this;
}
// 前置++
Date& Date:: operator++() {
    *this += 1;
    return *this;
}
// 后置++
Date Date::operator++(int) {
    Date tmp = *this;
    *this += 1;
    return tmp;
}
// 后置--
Date Date::operator--(int) {
    Date tmp = *this;
    *this -= 1;
    return tmp;
}
// 前置--
Date& Date::operator--() {
    *this -= 1;
    return *this;
}
Date& Date::operator=(const Date& d) {
    _year = d._year;
    _month = d._month;
    _day = d._day;
    return *this;
}
int Date::GetMonthDay(int year, int month) {
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
        return day2[month];
    } else {
        return day[month];
    }
}
bool Date::operator<(const Date& x) {
    if (_year < x._year) {
        return true;
    } else if (_year == x._year && _month < x._month) {
        return true;
    } else if (_year == x._year && _month == x._month && _day < x._day) {
        return true;
    }

    return false;
}

bool Date::operator==(const Date& x) {
    return _year == x._year
           && _month == x._month
           && _day == x._day;
}

// 复用
// d1 <= d2
bool Date::operator<=(const Date& x) {
    return *this < x || *this == x;
}

bool Date::operator>(const Date& x) {
    return !(*this <= x);
}

bool Date::operator>=(const Date& x) {
    return !(*this < x);
}

bool Date::operator!=(const Date& x) {
    return !(*this == x);
}
int Date::Getday(Date& t) {
    int p = 0;
    int s = t._month;
    while (--s) {
        p += t.GetMonthDay(t._year, s);
    }
    p += t._day;
    int year__ = t._year - 1;
    while (year__--) {
        if ((year__ % 4 == 0 && year__ % 100 != 0) || year__ % 400 == 0) {
            p = p + 366;
        } else {
            p = p + 365;
        }
    }
    return p;
}
int Date::operator-(const Date& d) {
    int year = _year;
    int month = _month;
    int day1 = _day;
    int t = 0;
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
        while (month) {
            t += day2[month];
            month--;
        }
    } else {
        while (month) {
            t += day[month];
            month;
        }
    }
    while (year--) {
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            t += 366;
        } else {
            t += 365;
        }
    }
    t += day1;
    int t2 = 0;
    year = d._year;
    day1 = d._day;
    month = d._month;
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
        while (month) {
            t2 += day2[month];
            month--;
        }
    } else {
        while (month) {
            t2 += day[month];
            month--;
        }
    }
    while (year) {
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            t2 += 366;
        } else {
            t2 += 365;
        }
        year--;
    }
    t2 += d._day;
    return abs(t - t2);
}
int main() {
    int a, b, c;
    cin >> a >> b >> c;
    Date A(a, 1, 1);
    Date B(a, b, c);
    int p1 = A.Getday(A);
    int p2 = B.Getday(B);
    cout << p2 - p1 + 1 << endl;
    return 0;
}

全部评论
这个是用类实现的
点赞 回复 分享
发布于 2023-06-28 00:26 河南

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务