1. 日期类的具体实现
1.date.h
#pragma once
#include<iostream>
using namespace std;
class date
{
public:
int getmonthday(int year, int month);
date(int year = 1, int month = 1, int day = 1);
void print();
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);
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);
int operator-(date& d);
private:
int _year;
int _month;
int _day;
};
2.date.cpp
#include"date.h"
int date::getmonthday(int year, int month)
{
int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (month == 2 && (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)))
{
return 29;
}
return arr[month];
}
date::date(int year, int month, int day)
{
if (month > 0 && month < 13 && (day > 0 && day <= getmonthday(year, month)))
{
_year = year;
_month = month;
_day = day;
}
else
{
cout << "日期不合法" << endl;
}
}
void date::print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
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);
}
bool date::operator<(const date& d)
{
if (_year < d._year)
{
return true;
}
else if (_year == d._year && _month < d._month)
{
return true;
}
else if (_year == d._year && _month == d._month && _day < d._day)
{
return true;
}
else
{
return false;
}
}
bool date::operator<=(const date& d)
{
return (*this < d) || (*this == d);
}
bool date::operator>(const date& d)
{
return !(*this < d) && (*this != d);
}
bool date::operator>=(const date& d)
{
return !(*this < d);
}
date& date::operator+=(int day)
{
if (day < 0)
{
*this -= -day;
return *this;
}
int getday = _day + day;
while (getday > getmonthday(_year, _month))
{
getday -= getmonthday(_year, _month);
_month++;
if (_month == 13)
{
_year++;
_month = 1;
}
}
_day = getday;
return *this;
}
date date::operator+(int day)
{
date tmp = *this;
tmp += day;
return tmp;
}
date& date::operator-=(int day)
{
if (day < 0)
{
*this += -day;
return *this;
}
_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;
}
int date::operator-(date& d)
{
date max = *this;
date min = d;
if (*this < d)
{
max = d;
min = *this;
}
int n = 0;
while (min != max)
{
n++;
if (min._day < getmonthday(min._year, min._month))
{
min._day++;
}
else
{
min._month++;
min._day = 1;
}
if (min._month == 13)
{
min._year++;
min._month = 1;
}
}
return n;
}
3.dateTest.cpp
#include"date.h"
int main()
{
date d1(2023, 2, 9);
date d2(2023, 2, 1);
cout << (d1 == d2) << endl;
cout << (d1 < d2) << endl;
cout << (d1 - d2) << endl;
return 0;
}