题解 | #计算日期到天数转换#
计算日期到天数转换
https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded
#include <iostream> using namespace std; int Count = 0; class Date { public: Date(int year = 1, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } int Days(int year, int month) { int a[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (month == 2) if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) return 29; return a[month]; } bool operator==(Date d) { if (d._year == this->_year&&d._month == this->_month&&d._day == this->_day) return true; return false; } Date& operator++() { this->_day += 1; if (this->_day>Days(this->_year, this->_month)) { this->_month++; this->_day = 1; } if (this->_month>12) { this->_year++; this->_month = 1; } return *this; } int operator-(Date d) { int res = 1; while (!(*this == d)) { ++res; ++d; } return res; } private: int _year; int _month; int _day; }; int main() { int year, month, day; cin >> year >> month >> day; Date d1(year, month, day); Date d2(year); cout << d1 - d2 << endl; return 0; }