1. 函数的声明(Date.h)
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
public:
int GetMonthDay(int year, int month);
Date(int year = 1900, int month = 1, int day = 1);
Date(const Date& d);
Date& operator=(const Date& d);
void print()const;
bool operator>(const Date& d)const;
bool operator==(const Date& d)const;
bool operator >= (const Date& d)const;
bool operator < (const Date& d)const;
bool operator <= (const Date& d)const;
bool operator != (const Date& d)const;
Date& operator+=(int day);
Date operator+(int day)const;
Date operator-(int day)const;
Date& operator-=(int day);
Date& operator++();
Date operator++(int);
Date operator--(int);
Date& operator--();
int operator-(const Date& d)const;
private:
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream& out,const Date& d);
istream& operator>>(istream& in, Date& d);
2. 函数的定义(Date.c)
#define _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"
int Date::GetMonthDay(int year, int month)
{
static int DaysArr[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;
}
else
return DaysArr[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;
assert(false);
}
}
Date::Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
Date& Date::operator=(const Date& d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
void Date::print() const
{
cout << this->_year << "年" << this->_month << "月" << this->_day << "日" << endl;
}
bool Date::operator<(const Date& d)const
{
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)const
{
if (_year == d._year && _month == d._month && _day == d._day)
{
return true;
}
else
{
return false;
}
}
bool Date::operator<=(const Date& d)const
{
return *this < d || *this == d;
}
bool Date::operator>(const Date& d)const
{
return !(*this <= d);
}
bool Date::operator>=(const Date& d)const
{
return !(*this < d);
}
bool Date::operator!=(const Date& d)const
{
return !(*this == d);
}
Date& Date::operator+=(int day)
{
if (day < 0)
{
*this -= -day;
return *this;
}
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month += 1;
if (_month > 12)
{
_year += 1;
_month = 1;
}
}
return *this;
}
Date Date::operator+(int day)const
{
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 -= 1;
if (_month < 1)
{
_year -= 1;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
Date Date::operator-(int day)const
{
Date tmp = *this;
tmp -= day;
return tmp;
}
Date& Date::operator++()
{
*this += 1;
return *this;
}
Date Date::operator++(int)
{
Date tmp = *this;
*this += 1;
return tmp;
}
Date& Date::operator--()
{
*this -= 1;
return *this;
}
Date Date::operator--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}
int Date::operator-(const Date & d)const
{
int count = 0, flag = 1;
Date big = *this;
Date small = d;
if (small > big)
{
big = d;
small = *this;
flag = -1;
}
while (big != small)
{
big -= 1;
count++;
if (big == small)
{
break;
}
}
return flag * count;
}
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}
istream& operator>>(istream& in, Date& d)
{
int year, month, day;
in >> year >> month >> day;
if (month > 0 && month < 13
&& day>0 && day <= d.GetMonthDay(year, month))
{
d._year = year;
d._month = month;
d._day = day;
}
else
{
cout << "非法日期" << endl;
assert(false);
}
return in;
}
3. 测试(Test.c)
#define _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"
void TestDate1()
{
Date d1(2023, 5, 7);
Date d2(2022, 3, 3);
cout << (d1 < d2) << endl;
d1.print();
}
void TestDate2()
{
Date d1(2023, 5, 7);
d1.print();
Date tmp = d1 + 88;
d1.print();
tmp.print();
}
void TestDate3()
{
Date d1(2023, 5, 7);
d1.print();
Date tmp = d1 - 88;
tmp.print();
d1.print();
}
void TestDate4()
{
Date d1(2023, 5, 7);
d1.print();
++d1;
d1.print();
d1++;
d1.print();
}
void TestDate5()
{
Date d1(2023, 5, 7);
Date d2(2021, 5, 7);
int ret = d1 - d2;
cout << ret << endl;
}
void TestDate6()
{
Date d1(2023, 5, 7);
Date d2(2021, 5, 17);
cout << d1 << d2;
cin >> d1 >> d2;
cout << d1 << d2;
}
void TestDate7()
{
Date d1(2023, 1, 7);
const Date d2(2021, 1, 12);
d1.print();
d2.print();
d1 < d2;
d2 > d1;
d1 + 100;
d2 + 100;
}
int main()
{
TestDate7();
return 0;
}