日期类(Date)

来源:互联网 发布:c语言实现冒泡排序 编辑:程序博客网 时间:2024/04/29 00:15

日期类的实现在此分三个步骤实现:

一、构建类

class Date{friend ostream& operator<<(ostream& _cout, const Date& d);//友元函数声明public:Date(int year = 2017, int month = 9, int day = 23);~Date();Date& operator=(const Date& d);Date& operator++();//前置++Date operator++(int);//后置++Date& operator--();//前置--Date operator--(int);//后置--Date operator+(int days);//days天之后的日期Date operator-(int days);//days天之前的日期int operator-(const Date& d);//计算量日期之间的距离bool operator==(const Date& d);bool operator!=(const Date& d);bool operator>(const Date& d);bool operator<(const Date& d);int Is_runnian(int year);//判断闰年int Days(int year, int month);//求月的天数bool Is_hefadate(int year, int month, int day);//日期是否合法private:int _year;int _month;int _day;};

二、在类外定义构造函数,析构函数,友元函数

ostream& operator<<(ostream& _cout, const Date& d){_cout << d._year << "/" << d._month << "/" << d._day << endl;return _cout;}//构造函数Date::Date(int year, int month, int day):_year(year), _month(month), _day(day){}//析构函数Date::~Date(){}

三、运算符的重载及相关操作

//赋值运算符重载Date& Date::operator=(const Date& d){if (this == &d){return *this;}else{_year = d._year;_month = d._month;_day = d._day;}return *this;}//前置++Date& Date::operator++(){_day += 1;return *this;}//后置++Date Date::operator++(int){Date temp = *this;_day += 1;return temp;}//前置--Date& Date::operator--(){_day -= 1;return *this;}//后置--Date Date::operator--(int){Date temp = *this;_day -= 1;return temp;}bool Date::operator==(const Date& d){if ((_year == d._year) && (_month == d._month) && (_day == d._day))return true;elsereturn false;}bool Date::operator!=(const Date& d){if ((_year != d._year) || (_month != d._month) || (_day != d._day))return true;elsereturn false;}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;elsereturn false;}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;elsereturn false;}int Date::Is_runnian(int year){if ((year % 4 && year % 100) || (year % 400)){return 1;}return -1;}int Date::Days(int year, int month){int day;int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };day = days[month - 1];if (month == 2){day += Is_runnian(year);}return day;}bool Date::Is_hefadate(int year,int month,int day){if ((year < 1) || (month < 1) || (month > 13) || (day < 1) || (day>Days(year, month)))return true;elsereturn false;}//days天之后的日期Date Date::operator+(int days){Date temp = *this;if (days < 0){int day = -days;return temp - day;}else{temp._day += days;while (temp._day>Days(temp._day, temp._month)){temp._day -= Days(temp._day, temp._month);if (temp._month == 12){temp._year++;temp._month = 1;}else{temp._month++;}}return temp;}}//days天之前的日期Date Date::operator-(int days){Date temp = *this;if (days < 0){int day = -days;return temp + day;}else{temp._day -= days;while (temp._day <= 0){temp._day = temp._day + Days(temp._day, temp._month);if (temp._month== 1){temp._year--;temp._month = 12;}else{temp._month--;}}return temp;}}//计算量日期之间的距离int Date::operator-(const Date& d){int count = 0;Date max=*this;Date min=d; if (max < min){     max = d;     min = *this;}     while ((min + count) < max)    {   count++;    }   return count;}

到这里,日期类的构建就结束了,不妨,我们来测试一下吧。

void test(){Date d1(2017, 8, 25),d2(2017,7,5),d3;int count = 0;d1++;cout << d1;++d1;cout << d1;d1--;cout << d1;--d1;cout << d1;d3=d1 + 5;cout << d3;d3=d2 - 5;cout << d3;count=d1 - d2;cout << count<<endl;}int main(){test();system("pause");return 0;}