您的位置:首页 > 其它

日期类(全)

2016-10-20 23:36 225 查看
日期类一般面试的时候都是只给15到25分钟左右的时间来实现,所以要完成必要的几个功能函数

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<windows.h>
using namespace::std;
class Date
{
public:
Date(int year = 1990, int month = 1, int day = 1)
:_year(year)
, _month(month)
, _day(day)
{
if (!(year >= 1990 && (month > 0 && month < 13) && (day>0 && day<GetDaysInMonth(year,month))))
{
_year=1990;
_month = 1;
_day = 1;
}
}
~Date()
{}
//获取本月天数
int GetDaysInMonth(int year,int month)
{
int days[] = { 0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (IsLeapYear(year)&&(2==month))
{
days[month] = 29;
}
return days[month];
}
//判断闰年
bool IsLeapYear(int year)const
{
if ((0 == year % 4) && (0 != year % 100) || (0 == year % 400))
{
return true;
}
return false;
}
//拷贝构造函数
Date(const Date& d)
:_year(d._year)
, _month(d._month)
, _day(d._day)
{}
//赋值运算符重载
Date& operator=(const Date& d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
/*
必要的判断函数
*/
bool 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;
}
return false;
}
bool 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;
}
return false;
}
bool operator==(const Date& d)
{
if (_year == d._year&&_month == d._month&&_day == d._day)
return true;
return false;
}
Date operator+(int days)
{
if (days < 0)
{
days = 0 - days;
return *this - days;
}
Date temp(*this);
temp._day += days;
int dayInMonth = 0;
while (temp._day > (dayInMonth=GetDaysInMonth(temp._year, temp._month)))
{
temp._day -= dayInMonth;
if (12 == temp._month)
{
++temp._year;
temp._month = 1;
}
else
{
temp._month++;
}
}
return temp;
}
Date operator-(int days)
{
if (days < 0)
{
days = 0 - days;
return *this + days;
}
Date temp(*this);
temp._day -= days;
while (temp._day <= 0)
{
if (temp._month == 1)
{
--temp._year;
temp._month = 12;
}
else
{
--temp._month;
}
temp._day += GetDaysInMonth(temp._year, temp._month);
}
return temp;
}
/*前置++重载*/
Date& operator++()
{
if (_day == GetDaysInMonth(_year, _month))
{
if (_month == 12)
{
++_year;
_month = 1;
_day = 1;
}
else
{
++_month;
_day = 1;
}
}
else
{
++_day;
}
return *this;
}
//后置++重载
//这里相当于先创建一个临时对象给临时变量*this的内容,然后让this天数++
Date operator++(int)
{
Date temp(*this);
if (_day == GetDaysInMonth(_year, _month))
{
if (_month == 12)
{
++_year;
_month = 1;
_day = 1;
}
else
{
++_month;
_day = 1;
}
}
else
{
++_day;
}
return temp;
}
friend ostream& operator<<(ostream& _cout, const Date& d)
{
_cout << d._year << "-" << d._month << "-" << d._day;
return _cout;
}
int operator-(const Date& d)
{
Date minDate(*this);
Date maxDate(d);
if (minDate > maxDate)
{
std::swap(minDate, maxDate);
}
int count = 0;
while (minDate < maxDate)
{
count++;
++minDate;
}
return count;
}
private:
int _year;
int _month;
int _day;
};
FunTest()
{
Date d1(2016,10,20);
Date d2;
d2=d1++;
}
int main()
{
FunTest()
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  日期类