Date 日期类
下面实现一个简单的日期类
类的定义
因为在类外面定义函数会导致一些函数访问private成员变量导致代码出问题,所以直接就把函数定义到类里面,类里面的成员函数是可以访问私有的成员变量的
#pragma once
class Date
{
public:
int GetMonthDay(int year, int month);
void GetWeekday();
void Print();
Date(int year = 1, int month = 1, int day = 1);
Date(const Date& d);
Date& operator=(const Date& d);
~Date();
Date& operator+=(int day);
Date operator+(int day);
Date operator-(int day);
Date& operator-=(int day);
Date& operator++();
Date operator++(int);
Date& operator--();
Date operator--(int);
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 Date::GetMonthDay(int year, int month)
{
static int monthdayarray[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int day = monthdayarray[month];
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
{
day += 1;
}
return day;
}
打印年月日
void Date::Print()
{
if (_month <= 12)
{
int monthday = GetMonthDay(_year, _month);
if (_day <= monthday)
cout << _year << '-' << _month << '-' << _day << endl;
else
{
cout << "日期输入错误->";
cout << _year << '-' << _month << '-' << _day << endl;
}
}
else
{
cout << "日期输入错误->";
cout << _year << '-' << _month << '-' << _day << endl;
}
}
构造函数(全缺省)
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
析构函数
Date::~Date()
{
}
拷贝构造函数
Date::Date(const Date& d)
{
_year=d._year;
_month = d._month;
_day = d._day;
}
赋值运算符重载
Date& Date::operator= (const Date& d)
{
_year=d._year;
_month = d._month;
_day = d._day;
return *this;
}
日期+=天数
Date& Date::operator+=(int day)
{
if (day < 0)
{
return *this -= (-day);
}
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
++_month;
if (_month == 13)
{
_year++;
_month = 1;
}
}
return *this;
}
日期+天数
Date Date::operator+(int day)
{
Date ret(*this);
ret += day;
return ret;
}
日期-=天数
Date& Date::operator-=(int day)
{
if (day < 0)
{
return *this += (-day);
}
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
--_year;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
日期-天数
Date Date::operator-(int day)
{
Date ret(*this);
ret -= day;
return ret;
}
前置++
Date& Date::operator++()
{
*this += 1;
return *this;
}
后置++
Date Date::operator++(int)
{
Date ret = *this;
*this += 1;
return ret;
}
前置后置–
Date& Date::operator--()
{
*this -= 1;
return *this;
}
Date Date::operator--(int)
{
Date ret = *this;
*this -= 1;
return ret;
}
大于(>)和比较是否相等(==)的重载
bool Date::operator>(const Date& d)
{
if ((_year > d._year) ||
(_year == d._year && _month > d._month) ||
(_year == d._year && _month == d._month && _day > d._day))
return true;
else
return false;
}
bool Date::operator==(const Date& d)
{
return (_year == d._year) && (_month == d._month) && (_day == d._day);
}
其他的比较运算符的重载
bool Date::operator>=(const Date& d)
{
return (*this > d) || (*this == d);
}
bool Date::operator<(const Date& d)
{
return !(*this >= d);
}
bool Date::operator<=(const Date& d)
{
return !(*this > d);
}
bool Date::operator!=(const Date& d)
{
return !(*this == d);
}
日期-日期(返回天数)
int Date::operator-(const Date& d)
{
Date max = *this;
Date min = d;
int flag = 1;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
int count = 0;
while (min != max)
{
++min;
++count;
}
return count * flag;
}
查看输入的日期是星期几
这里设置了有星期的起始日期
void Date::GetWeekday()
{
Date start(1900,1,1);
int count = *this - start;
const char* dayarr[7] = { "星期一","星期二","星期三","星期四","星期五","星期六","星期日"};
cout << dayarr[count % 7] << endl;
}
|