Test.cpp
#include "Date.h"
void TestDate1()
{
Date d1(2023, 5, 1);
Date d2(2023, 9, 1);
cout << (d1 == d2) << endl;
cout << (d1 != d2) << endl;
cout << (d1 > d2) << endl;
cout << (d1 < d2) << endl;
cout << (d1 >= d2) << endl;
cout << (d1 <= d2) << endl;
}
void TestDate2()
{
Date d1(2023, 5, 1);
Date d2(2023, 9, 1);
d1 += 50;
(d1 + 50).Print();
d2 -= 10;
(d2 - 10).Print();
(++d1).Print();
(d1++).Print();
}
void TestDate3()
{
Date d1(2023, 5, 1);
Date d2(2023, 9, 1);
d1 - d2;
}
void TestDate5()
{
const char* WeeDayToStr[] = { "周一","周二","周三","周四","周五","周六","周七" };
Date d1, d2;
int day = 0;
int option = 0;
do {
cout << "**********************************" << endl;
cout << " 1、日期加减天数 2、日期减日期" << endl;
cout << " 3、日期->周几 -1、退出" << endl;
cout << "请选择:>" << endl;
cout << "*********************************" << endl;
cin >> option;
if (1 == option)
{
cout << "请依次输入日期及天数(减天数就输入负数):";
cin >> d1 >> day;
cout << "日期加减天数后的日期:" << d1 + day << endl;
}
else if (2 == option)
{
cout << "请依次输入两个日期:";
cin >> d1 >> d2;
cout << "相差的天数:" << d1 - d2 << endl;
}
else if (option == 3)
{
cout << "请输入日期:";
cin >> d1;
Date start(1, 1, 1);
int n = d1 - start;
int weekDay = 0;
weekDay += n;
cout << WeeDayToStr[weekDay % 7] << endl;
}
else
{
cout << "无此选项,请重新输入" << endl;
}
} while (option != -1);
}
void TestDate6()
{
Date d1(2023, 8, 8);
(d1 + 100).Print();
(d1 + -100).Print();
(d1 - -100).Print();
}
void TestDate7()
{
int i = 0;
double d = 1.1;
Date d1(2022, 7, 25);
Date d2(2022, 7, 26);
cout << d1 << d2;
int day = 1;
cin >> d1 >> day;
cin >> d1 >> d2;
cout << d1 << d2;
}
int main()
{
TestDate1();
return 0;
}
Date.h
#pragma once
#include <iostream>
#include <stdlib.h>
#include <assert.h>
using namespace std;
class Date
{
friend inline ostream& operator<<(ostream& out, const Date& d);
friend inline istream& operator>>(istream& in, Date& d);
public:
Date(int year = 1, int month = 1, int day = 1)
{
this->_year = year;
_month = month;
_day = day;
assert(this->CheckDate());
}
void Print()
{
cout << _year << "/" << _month << "/" << _day << endl;
}
int GetMonthDay(int year, int month)
{
static int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int day = days[month];
if (2 == month
&& ((0 == year % 4 && year % 100 != 0) || (0 == year % 400)))
{
day += 1;
}
return day;
}
bool CheckDate()
{
if (_year >= 1
&& _month > 0 && _month < 13
&& _day>0 && _day <= GetMonthDay(_year, _month))
return true;
else
return false;
}
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);
int operator-(const Date& d);
void operator<<(ostream& out);
private:
int _year;
int _month;
int _day;
};
inline ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "/" << d._month << "/" << d._day << endl;
return out;
}
inline istream& operator>>(istream& in, Date& d)
{
in >> d._year >> d._month >> d._day;
assert(d.CheckDate());
return in;
}
Date.cpp
#include "Date.h"
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)
|| (_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 !(*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)
{
Date ret(*this);
ret += day;
return ret;
}
Date& Date::operator+=(int day)
{
if (day < 0)
{
return *this -= -day;
}
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
++_month;
if (13 == _month)
{
++_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 (0 == _month)
{
--_year;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
Date& Date::operator++()
{
*this += 1;
return *this;
}
Date Date::operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}
int Date::operator-(const Date& d)
{
int flag = 1;
Date max = *this;
Date min = d;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (min != max)
{
++min;
++n;
}
return n * flag;
}
void Date::operator<<(ostream& out)
{
out << _year << "/" << _month << "/" << _day << endl;
}