文章目录
- Date.h
- Date.cpp
- DateTest.cpp
Date.h
#pragma once
#include
#include
using namespace std;
#include
class Date
{
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
public:
Date(int year = 1900, int month = 1, int day = 1);
void print() const;
int GetMonthDay(int year, int month) 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);
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;
private:
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);
Date.cpp
#include "Date.h"
Date::Date(int year, int month, int day)
{
if (year >= 0
&& (month > 0 && month <= 12)
&& (day > 0 && day <= Date::GetMonthDay(year, month)))
{
_year = year;
_month = month;
_day = day;
}
else
{
cout << "日期非法..." << endl;
}
}
void Date::print() const
{
cout << _year << " 年 " << _month << " 月 " << _day << " 日" << endl;
}
ostream& operator<<(ostream& out, const Date& d)
{
cout << d._year << " 年 " << d._month << " 月 " << d._day << " 日" << endl;
return out;
}
istream& operator>>(istream& in, Date& d)
{
cin >> d._year;
if (d._year < 0)
{
perror("输入年份非法");
exit(-1);
}
cin >> d._month;
if (d._month > 12 || d._month <= 0)
{
perror("输入月份非法");
exit(-1);
}
cin >> d._day;
if (d._day > d.GetMonthDay(d._year, d._month) || d._day <= 0)
{
perror("输入日期非法");
exit(-1);
}
return in;
}
int Date::GetMonthDay(int year, int month) const
{
assert(month > 0 && month < 13);
int monthArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
{
monthArray[2] = 29;
}
return monthArray[month];
}
Date& Date::operator+=(int day)
{
if (day < 0)
{
*this -= -day;
return *this;
}
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
++_month;
if (_month > 12)
{
_month -= 12;
++_year;
}
}
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;
}
while (day > _day)
{
if (_month == 1)
{
_month = 12;
--_year;
}
else
{
--_month;
}
day -= _day;
_day = GetMonthDay(_year, _month);
}
_day -= day;
return *this;
}
Date Date::operator-(int day) const
{
Date tmp(*this);
tmp -= day;
return tmp;
}
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 (max != min)
{
++min;
++count;
}
return count * flag;
}
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;
}
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 _year < d._year
|| ((_year == d._year) && (_month < d._month))
|| ((_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);
}
DateTest.cpp
#include "Date.h"
void TestDate1()
{
Date d1(2023, 3, 17);
d1.print();
Date d2 = d1 + 168;
d2.print();
d1.print();
d1 += 168;
d1.print();
}
void TestDate2()
{
Date d1(2023, 3, 17);
d1.print();
++d1;
d1.print();
d1++;
d1.print();
}
void TestDate3()
{
Date d1(2023, 3, 17);
d1.print();
Date d2 = d1 - (-5000);
d1.print();
d2.print();
d1 -= 5000;
d1.print();
}
void TestDate4()
{
Date d1(2023, 3, 17);
d1.print();
--d1;
d1.print();
d1--;
d1.print();
}
void TestDate5()
{
Date d1(2023, 3, 17);
Date d2(2017, 10, 23);
Date d3(2023, 9, 1);
cout << d1 - d2 << endl;
cout << d1 - d3 << endl;
}
void TestDate6()
{
int i = 8888;
double d = 1.11;
cout << i;
cout << d << endl;
Date d1(2023, 3, 17);
operator<<(cout, d1);
cout << d1;
Date d2(2035, 6, 6);
cout << d1 << d2 << endl;
}
void TestDate7()
{
Date d1;
cout << d1;
cin >> d1;
cout << d1;
}
int main()
{
TestDate1();
TestDate2();
TestDate3();
TestDate4();
TestDate5();
TestDate6();
TestDate7();
return 0;
}