C++日期类的实现

来源:互联网 发布:php判断水仙花数 编辑:程序博客网 时间:2024/06/05 06:40
#include<iostream>  using namespace std;class Date{public:Date(int year = 2017, int month = 9, int day = 24)//构造函数: _year(year), _month(month), _day(day){}bool IsLeapyear(int year);//判断是否为闰年int one_month_day(int year, int month);//返回一个月的天数Date& operator=(const Date& d);//赋值运算符重载Date(const Date& d);//拷贝构造函数~Date();Date& operator++();// 前置++ Date operator++(int);// 后置++ Date& operator--();// 前置--Date operator--(int);// 后置--//days天之后的日期 Date operator+(int days);// days天之前的日期 Date operator-(int days);bool operator>(const Date& d);bool operator<(const Date& d);// 两个日期之间的距离 int operator-(const Date& d);bool operator==(const Date& d);bool operator!=(const Date& d);void display();friend ostream& operator<<(ostream& _cout, const Date& d);friend istream& operator>>(istream& _cin, Date& d);private:int _year;int _month;int _day;};//判断是否为闰年bool Date::IsLeapyear(int year){if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)){return true;}return false;}//返回一个月的天数int Date::one_month_day(int year,int month){int op[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };if (IsLeapyear(year)){if (month == 2)return 29;elsereturn op[month];}else{return op[month];}}//赋值运算符重载Date& Date::operator=(const Date& d){if (this != &d){this->_year = d._year;this->_month = d._month;this->_day = d._day;}return *this;}//拷贝构造函数Date::Date(const Date& d):_year(d._year), _month(d._month), _day(d._day){}Date::~Date(){}//days天之后的日期 Date Date::operator+(int days){Date tmp(*this);if (days < 0){days = -days;return tmp - days;}else{tmp._day += days;while (tmp._day>one_month_day(tmp._year, tmp._month)){tmp._day -= one_month_day(tmp._year, tmp._month);if (tmp._month == 12){tmp._year++;tmp._month = 1;}else{tmp._month++;}}return tmp;}}// days天之前的日期 Date Date::operator-(int days){Date tmp(*this);if (days < 0){days = -days;return tmp + days;}else{tmp._day -= days;while (tmp._day < 0){if (tmp._month == 1){tmp._year--;tmp._month = 12;}else{tmp._month--;}tmp._day += one_month_day(tmp._year, tmp._month);}return tmp;}}// 前置++Date& Date::operator++(){*this = *this + 1;return *this;}// 后置++Date Date::operator++(int){Date tmp(*this);*this = *this + 1;return tmp;}// 前置--Date& Date::operator--(){*this = *this - 1;return *this;}// 后置--Date Date::operator--(int){Date tmp(*this);*this = *this - 1;return tmp;}bool Date::operator==(const Date& d){if ((this->_year == d._year) && (this->_month == d._month) && (this->_day == d._day))return true;elsereturn false;}bool Date::operator!=(const Date& d){if ((this->_year != d._year) || (this->_month != d._month) || (this->_day != d._day))return true;elsereturn false;}bool Date::operator>(const Date& d){if ((this->_year > d._year) || ((this->_year == d._year) && (this->_month > d._month)) ||((this->_year == d._year) && (this->_month == d._month) && (this->_day > d._day)))return true;elsereturn false;}bool Date::operator<(const Date& d){if ((this->_year < d._year) || ((this->_year == d._year) && (this->_month < d._month)) ||((this->_year == d._year)&&(this->_month == d._month) && (this->_day < d._day)))return true;elsereturn false;}//两个日期之间的距离 int Date::operator-(const Date& d){int days = 0;Date bigdate(*this);Date smalldate(d);if (*this < d){smalldate = *this;bigdate = d;}while (bigdate > (smalldate + days)){days++;}return days;}void Date::display(){cout << _year << "年" << _month << "月" << _day << "日"<<endl;}void FunTest(){Date d1(2017, 9, 24);Date d2(2017, 8, 24);Date d3(2012, 6, 8);Date d4(2012, 6, 8);//d1.display();//d2.display();    //d3=--d1;//d3=d2++;//d3.display();//Date d4(d1);//d4.display();d3 = d1 + 39;d3.display();d4 = d2 - 45;d4.display();int days = d1 - d2;cout << days << endl;}int main(){FunTest();system("pause:");return 0;}

原创粉丝点击