日期类
来源:互联网 发布:中国人才流失严重知乎 编辑:程序博客网 时间:2024/05/02 01:14
Date类的编写要求如下,编写构造函数以及运算符的重载,以及测试基础代码
class Date
{ public: Date(int year = 1900, int month = 1, int day = 1) :_year(year) ,_month(month) ,_day(day) { // 如何检查一个日期是否合法 int days = GetMonthDays(year, month); if (days == -1 || day < 1 || day > days) { cout<<"日期不合法"<<endl; Display(); exit(-1); } } //d1 > d2 bool operator>(const Date& d); //operator>= //operator< //operator<= //operator== //operator!= //d1 + 100 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-(const Date& d); bool IsLeapYear(int year); // if /switch/array int GetMonthDays(int year, int month); private: int _year; int _month; int _day; };
代码如下:
- #define _CRT_SECURE_NO_WARNINGS 1
-
- #include<iostream>
- using namespace std;
- class Date
- {
- public:
- Date(int year = 1900, int month = 1, int day = 1)
- :_year(year)
- , _month(month)
- , _day(day)
- {
- // 如何检查一个日期是否合法
- int days = GetMonthDays(year, month);
- if (days == -1 || day < 1 || day > days)
- {
- cout << "日期不合法" << endl;
- this->Display();
- //exit(-1);
- }
- }
- void Display()const
- {
- cout << _year << "-" << _month << "-" << _day << endl;
- }
- //d1 > d2
- bool operator>(const Date& d) //>
- {
- if (this->_year > d._year)
- {
- return true;
- }
- else if (this->_year == d._year)
- {
- if (this->_month > d._month)
- {
- return true;
- }
- else if (this->_month == d._month)
- {
- if (this->_day > d._day)
- {
- return true;
- }
- }
- }
- return false;
- }
-
- static bool IsLeapYear(int year)
- {
- if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
- {
- return true;
- }
- return false;
- }
- inline static int GetMonthDays(int year, int month)
- {
- if (year < 1900 || month<1 || month>12)
- {
- return -1;
- }
- static int arr[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
- int days = arr[month];
- if ((month == 2) && (IsLeapYear(year)))
- {
- days += 1;
- }
- return days;
- }
- //operator>=
- bool operator >=(const Date& d) //>=
- {
- return (*this>d)||(*this==d);
- }
- //operator<
- bool operator<(const Date& d) //<
- {
- return !(*this>= d);
- }
- //operator<=
- bool operator<=(const Date& d) //<=
- {
- return (*this < d || *this == d);
- }
- //operator==
- bool operator==(const Date& d) //==
- {
- return _year == d._year
- && _month == d._month
- && _day == d._day;
- }
- //operator!=
- bool operator!=(const Date& d) //!=
- {
- return !(*this == d);
- }
-
- bool IsInvalid()
- {
- if (_year < 1900 || _month<1 || _month>12 || _day<1 || _day>GetMonthDays(_year, _month))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- //d1 + 100
- Date operator+(int day) //+
- {
- if (day < 0)
- {
- return *this - (-day);
- }
- Date tmp(*this);
- tmp._day += day;
- while (tmp.IsInvalid())
- {
- tmp._day -= GetMonthDays(tmp._year, tmp._month);
- tmp._month++;
- if (tmp._month == 13)
- {
- tmp._year++;
- tmp._month = 1;
- }
- }
- return tmp;
- }
- Date operator+=(int day) //+=
- {
- if (day < 0)
- {
- return *this -= (-day);
- }
- _day += day;
- while (IsInvalid())
- {
- _day -= GetMonthDays(_year, _month);
- _month++;
- if (_month == 13)
- {
- _year++;
- _month = 1;
- }
- }
- return *this;
- }
- Date operator-(int day) //-
- {
- if (day < 0)
- {
- return *this + (-day);
- }
- Date tmp(*this);
- tmp._day -= day;
- while (tmp.IsInvalid()==true)
- {
- if (tmp._month == 1)
- {
- tmp._year--;
- tmp._month = 12;
- }
- else
- {
- tmp._month--;
- }
- tmp._day += tmp.GetMonthDays(tmp._year, tmp._month);
-
- }
- return tmp;
- }
- Date operator-=(int day) //-=
- {
- if (day < 0)
- {
- return *this += (-day);
- }
- _day -= day;
- while (IsInvalid() == true)
- {
- if (_month == 1)
- {
- _year--;
- _month = 12;
- }
- else
- {
- _month--;
- }
- _day += GetMonthDays(_year, _month);
-
- }
- return *this;
- }
-
- Date operator++() //前置++
- {
- *this+=1;
- return *this;
- }
- Date operator++(int) //后置++
- {
- Date tmp(*this);
- *this+=1;
- return tmp;
- }
- Date operator--() //前置--
- {
- *this -= 1;
- return *this;
- }
- Date operator--(int) //后置--
- {
- Date tmp(*this);
- *this -= 1;
- return tmp;
- }
- int operator-(const Date& d) //一个日期减去一个日期,相差多少天
- { //将两个日期分别与初始定义的1900-1-1相减,得到两个天数再相减
- int Leapyearday = 366;
- int Noleapyearday = 365;
- Date chushi;
- Date tmp1(*this);
- int day1 = tmp1._day ;
- while (tmp1._month >chushi._month)
- {
- tmp1._month--;
- day1 += tmp1.GetMonthDays(tmp1._year, tmp1._month);
- }
- while (tmp1._year >chushi._year)
- {
- tmp1._year--;
- if (tmp1.IsLeapYear(tmp1._year))
- {
- day1 += Leapyearday;
- }
- else
- {
- day1 += Noleapyearday;
- }
- }
- Date tmp2(d);
- int day2 = tmp2._day;
- while (tmp2._month >chushi._month)
- {
- tmp2._month--;
- day2 += tmp2.GetMonthDays(tmp2._year, tmp2._month);
- }
- while (tmp2._year > chushi._year)
- {
- tmp2._year--;
- if (tmp2.IsLeapYear(tmp2._year))
- {
- day2 += Leapyearday;
- }
- else
- {
- day2 += Noleapyearday;
- }
- }
-
- int day = day1 - day2;
- return day;
- }
- private:
- int _year;
- int _month;
- int _day;
- };
测试用例:
- void test()
- {
- Date d1;
- Date d2(1990, 1, 1);
- d1.Display();
- d2.Display();
- cout << d1.operator==(d2)<< endl;
- cout << d1.operator<=(d2) << endl;
- cout << d1.operator>=(d2) << endl;
- cout << d1.operator!=(d2) << endl;
- cout << d1.operator<(d2) << endl;
- cout << d1.operator>(d2) << endl;
-
- }
- void test1()
- {
- Date d1(1991, 7, 1);
- Date d3 = ++d1;
- d3.Display();
- d3 = d1++;
- d3.Display();
- d1.Display();
- d3 = d1 + 1000;
- d3.Display();
- d1 += 1000;
- d1.Display();
-
- }
-
- void test2()
- {
- Date d1(1991, 7, 1);
- Date d3 = d1 - 1000;
- d3.Display();
- d1.Display();
- d1 -= 1000;
- d1.Display();
- }
-
- void test3()
- {
- Date d1(1991,7,1);
- d1.Display();
- Date d3 = d1--;
- d3.Display();
- d1.Display();
- d3 = --d1;
- d3.Display();
- d1.Display();
- }
- void test4()
- {
- Date d1(1991, 1, 1);
- Date d2(1994, 7, 1);
- int days = d2 - d1;
- cout << days << endl;
- }