date.h
#pragma once
#include<iostream>
using namespace std;
class Date{
public:
Date(int year=1, int month=1, int day=1);
void Print() const;
int GetMonthDay(int year,int month) const;
void PrintWeekDay() const {
const char* arr[] = { "周一","周二","周三","周四","周五","周六","周日" };
int count = *this - Date (1900, 1, 1);
cout << arr[count % 7] << endl;
}
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);
Date operator-(int day) const;
int operator-(const Date& d) const;
Date& operator++();
Date operator++(int);
Date& operator--();
Date operator--(int);
private:
int _year;
int _month;
int _day;
};
date.cpp
#include"Date.h"
int Date::GetMonthDay(int year, int month) const {
static int MonthDayArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int day = MonthDayArray[month];
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))) {
day += 1;
}
return day;
}
Date::Date(int year, int month, int day) {
_year = year;
_month = month;
_day = day;
if (!(_year >= 0
&&(_month>0 && _month<13)
&&(_day>0&&_day<=GetMonthDay(_year,_month))))
{
cout << "非法日期->";
Print();
}
}
void Date::Print() const {
cout << _year << "/" << _month << "/" << _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 {
return _year == d._year
&& _month == d._month
&& _day == d._day;
}
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) {
return *this -= -day;
}
_day += day;
while (_day>GetMonthDay(_year,_month))
{
_day -= GetMonthDay(_year, _month);
++_month;
if (_month==13) {
_month = 1;
_year++;
}
}
return *this;
}
Date Date::operator+(int day) const {
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 (_month == 0) {
_month = 12;
--_year;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
Date Date::operator-(int day) const {
Date ret(*this);
ret -= day;
return ret;
}
int Date::operator-(const Date& d) const {
Date max = *this;
Date min = d;
int flag = 1;
if (*this < d) {
max = d;
min = *this;
flag = -1;
}
int count = 0;
while (min!=max){
++min;
++count;
}
return count*flag;
}
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;
}
Test.cpp
#include"Date.h"
void TestDate1() {
Date d1(2023, 3, 7);
Date d2(2023,2, 29);
Date d3(2000, 2, 29);
Date d4(2022, 2, 29);
d1.Print();
d2.Print();
d3.Print();
d4.Print();
cout <<"\n"<< endl;
}
void TestDate2() {
Date d1(2023, 3, 9);
Date ret=d1 + 100;
ret.Print();
d1 += 100;
d1.Print();
Date ret2 = ++d1;
Date ret1 = d1++;
cout << "\n" << endl;
}
void TestDate3() {
Date d1(2023, 3, 9);
Date ret1 = d1 - 3;
ret1.Print();
Date ret2 = d1 - 9;
ret2.Print();
Date ret3 = d1 - 180;
ret3.Print();
Date ret4 = d1 - 1800;
ret4.Print();
Date ret5 = d1 - -100;
ret5.Print();
cout << "\n" << endl;
}
void TestDate4() {
Date d1(2023, 3, 30);
Date d2(2023, 6, 20);
cout << (d2 - d1) << endl;
cout << (d1 - d2) << endl;
d1.PrintWeekDay();
cout << "\n" << endl;
}
int main() {
TestDate1();
TestDate2();
TestDate3();
TestDate4();
Date d1(2023, 6, 28);
return 0;
}