分享一道腾讯的笔试题,日期类。
主要还是在实现操作符重载。
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
class Date
{
friend ostream& operator<<(ostream& _cout, const Date& d);
public:
Date(int year = 1900, int month = 1, int day = 1)
: _year(year)
, _month(month)
, _day(day)
{}
Date operator+(int days);
Date operator-(int days);
int operator-(const Date& d);//一个日期减去另一日期
Date& operator++();//前置++
Date operator++(int);//后置++
Date& operator--();//前置--
Date operator--(int);//后置--
friend bool operator>(const Date& d1, const Date& d2);
friend bool operator<(const Date& d1, const Date& d2);
friend bool operator==(const Date& d1, const Date& d2);
friend bool operator!=(const Date& d1, const Date& d2);
friend bool IsLeapYear(int);
friend bool IsValid(const Date& d);
friend int GetMonthDays(int, int);
friend int DayInYear(const Date& d);
friend void Swap(Date& d1, Date& d2);
private:
int _year;
int _month;
int _day;
};
bool operator>(const Date& d1, const Date& d2)
{
if ((d1._year > d2._year) || ((d1._year<d2._year) && (d1._month>d2._month)) || ((d1._year < d2._year) && (d1._month<d2._month) && (d1._day>d2._day)))
{
return true;
}
else
return false;
}
bool operator<(const Date& d1, const Date& d2)
{
if ((d2>d1) || (d1 == d2))
{
return false;
}
else
return true;
}
bool operator==(const Date& d1, const Date& d2)
{
if ((d1._year == d2._year) && (d1._month == d2._month) && (d1._day == d2._day))
{
return true;
}
else
return false;
}
bool operator!=(const Date& d1, const Date& d2)
{
return !(d1 == d2);
}
Date& Date::operator++()
{
*this = *this + 1;
return *this;
}
Date Date::operator++(int)//后置++
{
Date tmp(*this);
*this = *this + 1;
return tmp;
}
Date& Date::operator--()//前置--
{
*this = *this - 1;
return *this;
}
Date Date::operator--(int)//后置--
{
Date tmp(*this);
*this = *this + 1;
return tmp;
}
Date Date::operator+(int days)
{
Date tmp(*this);
tmp._day = tmp._day + days;
while (tmp._day > GetMonthDays(tmp._year, tmp._month))
{
tmp._day = tmp._day - GetMonthDays(tmp._year, tmp._month);
if (tmp._month == 12)
{
tmp._year++;
tmp._month = 1;
}
else
{
tmp._month++;
}
}
return tmp;
}
Date Date::operator-(int days)
{
Date tmp(*this);
tmp._day = tmp._day - days;
while (!IsValid(tmp))
{
if (tmp._month == 1)
{
tmp._year--;
tmp._month = 12;
}
else
{
tmp._month--;
}
tmp._day = tmp._day + GetMonthDays(tmp._year, tmp._month);
}
return tmp;
}
int Date::operator-(const Date& d1) //日期减日期
{
Date minDays = *this;
Date maxDays = d1;
int count = 0;
if (minDays > maxDays)
{
Swap(minDays, maxDays);
}
while (minDays != maxDays)
{
count++;
minDays = minDays + 1;
}
return count;
}
int DayInYear(const Date& d)
{
int days = d._day;
int i = 0;
for (i = 1; i < d._month; i++)
{
days = days + GetMonthDays(d._year, i);
}
return days;
}
void Swap(Date& d1, Date& d2)
{
int tmp = d1._year;
d1._year = d2._year;
d2._year = tmp;
tmp = d1._month;
d1._month = d2._month;
d2._month = tmp;
tmp = d1._day;
d1._day = d2._day;
d2._day = tmp;
}
int GetMonthDays(int year, int month)//不同月份的天数
{
static int MonthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (month != 2)
{
return MonthDays[month];
}
else if (IsLeapYear(year))
{
return 29;
}
else
{
return 28;
}
}
bool IsValid(const Date& d)
{
if (d._year > 0 && (d._month > 0 && d._month<13) && (d._day>0 && d._year <= GetMonthDays(d._year, d._month)))
{
return true;
}
return false;
}
bool IsLeapYear(int year)//判断闰年
{
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
{
return true;
}
return false;
}
void test1()
{
Date d1(2017, 3, 20);
Date d2;
d2 = d1 + 31;
}
void test2()
{
Date d1(2017, 3, 21);
int d2;
Date d(2018, 10, 21);
d2 = d1 - d;
cout << d2 << endl;
}
void test3()
{
}
int main()
{
//test1();
test2();
system("pause");
return 0;
}