C++实现Date日期类

时间:2023-03-09 02:32:48
C++实现Date日期类

定义一个Date类,包含三个属性年、月、日

实现了如下功能:

  1. 年月日的增加、减少:2017年10月1日加上100个月30天是2025年5月31日
  2. 输出某天是星期几:2017年10月1日是星期日
  3. 判断某一年是否是闰年:2020年是闰年
  4. 下一个工作日(周末)日期:2010年10月2日下一个周末是10月8日
 class Date
{
public:
Date();
Date(int yy, Month mm, int dd);
int day() const { return m_day; }
int year() const { return m_year; }
Month month() const { return m_month; } void add_day(int dd);//增加或减少天数
void add_month(int mm);//增加或减少月份
void add_year(int yy);//增加或减少年份 private:
int m_year;
Month m_month;
int m_day;
};
//判断日期是否合法
bool is_date(int y, Month m, int d);
//判断是否为闰年
bool leapyear(int y);
//两个Date是否相等
bool operator==(const Date &a, const Date &b);
bool operator!=(const Date &a, const Date &b);
//Date输入输出
ostream &operator<<(ostream &os, const Date &d);
istream &operator>>(istream &is, Date &dd);
//今天是星期几
Day day_of_week(const Date &date);
ostream &operator<<(ostream &os, const Day &d);
//下一个周末
Date next_Sunday(const Date &d);
//下一个工作日
Date next_weekday(const Date &d);

具体的实现代码如下:

 #include <iostream>
#include <vector>
#include <string> using std::istream;
using std::ostream;
using std::vector;
using std::ios_base;
using std::string; namespace Chrono
{
class InvalidDate
{
public:
std::string what() { return "InValid Date Occured"; };
}; enum class Month
{
jan = , //January
feb, //February
mar, //March
apr, //April
may, //May
jun, //June
jul, //July
aug, //August
sep, //September
oct, //October
nov, //November
dec //December
}; enum class Day
{
sun, //sunday
mon, //monday
tue, //tuesday
wed, //wednesday
thu, // thursday
fri, //friday
sat //saturday
}; class Date
{
public:
Date();
Date(int yy, Month mm, int dd);
int day() const { return m_day; }
int year() const { return m_year; }
Month month() const { return m_month; } void add_day(int dd);
void add_month(int mm);
void add_year(int yy); private:
int m_year;
Month m_month;
int m_day;
}; bool is_date(int y, Month m, int d);
bool leapyear(int y);
bool operator==(const Date &a, const Date &b);
bool operator!=(const Date &a, const Date &b);
ostream &operator<<(ostream &os, const Date &d);
istream &operator>>(istream &is, Date &dd);
Day day_of_week(const Date &date);
ostream &operator<<(ostream &os, const Day &d); Date next_Sunday(const Date &d);
Date next_weekday(const Date &d); ////////////////////////////////////////////////////////////////////////////////
//Implements /////
////////////////////////////////////////////////////////////////////////////////
Date::Date(int yy, Month mm, int dd) : m_year(yy), m_month(mm), m_day(dd)
{
if (!is_date(yy, mm, dd))
throw InvalidDate{};
} const Date &default_date()
{
static const Date d{, Month::jan, };
return d;
} Date::Date() : m_year(default_date().year()),
m_month(default_date().month()),
m_day(default_date().day()) {} void Date::add_day(int dd)
{
if (dd > )
{
for (; dd > ; --dd)
{
int temp_d = m_day + ;
switch (month())
{
case Month::feb:
if ((leapyear(m_year) && temp_d > ) || (!leapyear(m_year) && temp_d > ))
{
temp_d = ;
m_month = Month::mar;
}
break;
case Month::apr:
case Month::jun:
case Month::sep:
case Month::nov:
if (temp_d > )
{
temp_d = ;
m_month = Month((int)m_month + );
}
break;
case Month::dec:
if (temp_d > )
{
temp_d = ;
m_month = Month::jan;
m_year += ;
}
break;
default:
if (temp_d > )
{
temp_d = ;
m_month = Month((int)m_month + );
}
break;
}
m_day = temp_d;
}
}
else if (dd < )
{
for (; dd < ; ++dd)
{
int temp_d = day() - ;
if (temp_d <= )
{
switch (month())
{
case Month::jan:
m_month = Month::dec;
temp_d = ;
m_year -= ;
break;
case Month::mar:
m_month = Month::feb;
if (leapyear(m_year))
temp_d = ;
else
temp_d = ;
break;
case Month::feb:
case Month::apr:
case Month::jun:
case Month::oct:
case Month::sep:
case Month::nov:
temp_d = ;
m_month = Month((int)m_month - );
break;
default:
temp_d = ;
m_month = Month((int)m_month - );
break;
}
}
m_day = temp_d;
}
}
} void Date::add_month(int month)
{
int temp_y = month / + m_year;
int temp_m = month % + (int)m_day; if (temp_y <= )
{
temp_y--;
temp_m = temp_m + ;
}
else if (temp_m > )
{
temp_y++;
temp_m = temp_m - ;
}
m_year = temp_y;
m_month = Month(temp_m);
switch (m_month)
{
case Month::feb:
if (leapyear(m_year) && m_day > )
m_day = ;
else if (!leapyear(m_year) && m_day > )
m_day = ;
break;
case Month::apr:
case Month::jun:
case Month::sep:
case Month::nov:
if (m_day > )
m_day = ;
default:
break;
}
} void Date::add_year(int n)
{
if (month() == Month::feb && day() == && !leapyear(m_year + n))
m_day = ;
m_year += n;
} bool is_date(int y, Month m, int d)
{
if (d <= )
return false;
if (m < Month::jan || m > Month::dec)
return false;
int days_in_month;
switch (m)
{
case Month::feb:
days_in_month = leapyear(y) ? : ;
break;
case Month::apr:
case Month::jun:
case Month::sep:
case Month::nov:
days_in_month = ;
break;
default:
days_in_month = ;
break;
}
if (days_in_month < d)
return false;
return true;
} // https://en.wikipedia.org/wiki/Leap_year#Algorithm
bool leapyear(int y)
{
if (y % != )
return false;
else if (y % != )
return true;
else if (y % != )
return false;
return true;
}
bool operator==(const Date &a, const Date &b)
{
return a.year() == b.year() && a.month() == b.month() && a.day() == b.day();
} bool operator!=(const Date &a, const Date &b)
{
return !(a == b);
} ostream &operator<<(ostream &os, const Date &d)
{
return os << '(' << d.year()
<< ',' << (int)d.month()
<< ',' << d.day() << ')';
} ostream &operator<<(ostream &os, const Day &d)
{
vector<string> weekdays{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
os << weekdays[(int)d];
return os;
} // format (2017,5,23)
istream &operator>>(istream &is, Date &d)
{
int yy, mm, dd;
char ch1, ch2, ch3, ch4;
is >> ch1 >> yy >> ch2 >> mm >> ch3 >> dd >> ch4;
if (!is)
return is;
if (ch1 != '(' || ch2 != ',' || ch3 != ',' || ch4 != ')')
is.clear(ios_base::failbit);
d = Date{yy, Month(mm), dd};
return is;
} // https://cs.uwaterloo.ca/~alopez-o/math-faq/node73.html
Day day_of_week(const Date &date)
{
int y = date.year();
int m = (int)date.month();
int d = date.day();
y -= m < ;
int day_of_week = (y + y / - y / + y / + "-bed=pen+mad."[m] + d) % ;
return Day(day_of_week);
}
Date next_Sunday(const Date &d)
{
Date d1 = d;
while (day_of_week(d1) != Day::sun)
{
d1.add_day();
}
return d1;
} Date next_weekday(const Date &d)
{
Date d1 = d;
if (day_of_week(d1) == Day::sat)
d1.add_day();
else if (day_of_week(d1) == Day::fri)
d1.add_day();
else
d1.add_day();
return d1;
}
}

代码实现