题解 | #计算日期到天数转换#
计算日期到天数转换
https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded
#include <iostream>
using namespace std;
class Date {
public:
friend istream& operator>>(istream& in, Date& d);
Date(int year = 1, int month = 1, int day = 1) {
_year = year;
_month = month;
_day = day;
}
bool isyear(int year);
static int GetMonthDay(int year, int month);
static int DateCount(Date d);
private:
int _year;
int _month;
int _day;
};
int Date::GetMonthDay(int year, int month) {
static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day = days[month];
if (month == 2 && ((year % 4 == 0 && year % 100 != 0)
|| (year % 400 == 0))) {
day += 1;
}
return day;
}
bool Date::isyear(int year) {
if ((year % 4 == 0 && year % 100 != 0)
|| (year % 400 == 0)) {
return true;
}
return false;
}
int Date::DateCount(Date d) {
int n = 0;
for (int i = 1; i < d._month; i++) {
n += GetMonthDay(d._year, i);
}
n += d._day;
return n;
}
inline istream& operator>>(istream& in, Date& d) {
in >> d._year >> d._month >> d._day;
return in;
}
int main() {
Date d1;
cin >> d1;
int n = Date::DateCount(d1);
cout << n << endl;
return 0;
}