日期类——日期计算器

来源:互联网 发布:淘宝买轮毂 编辑:程序博客网 时间:2024/06/05 11:04

"date.h"

<strong><span style="font-size:18px;">#pragma onceclass Date{public:Date(int year = 1990,int month = 1,int day = 1):_year(year),_month(month),_day(day){if (IsInvalidData() == false){_year = 1990;_month = 1;_day = 1;}}Date(const Date& d){this->_year = d._year;this->_month = d._month;this->_day = d._day;}Date& operator=(const Date& d){if (this != &d){this->_year = d._year;this->_month = d._month;this->_day = d._day;}return *this;}Date& operator+(const int day){_day += day;while (_day > DayOfMonth()){_month++;if (_month > 12){_year++;_month = 1;}_day = _day - DayOfMonth();}return *this;}Date& operator-(const int day){_day -= day;while(_day < 1){_month--;if (_month < 1){_year--;_month = 12;}_day = DayOfMonth() - (-_day);}return *this;}Date& operator++()//前置++{_day++;if (_day > DayOfMonth()){_month++;if (_month > 12){_year++;_month = 1;}_day = 1;}return *this;}Date operator++(int)//后置++{Date tmp = *this;_day++;if (_day > DayOfMonth()){_month++;if (_month > 12){_year++;_month = 1;}_day = 1;}return tmp;}Date& operator--()//前置--{_day--;if (_day < 1){_month--;if (_month < 1){_year--;_month = 12;}_day = DayOfMonth();}return *this;}Date operator--(int)//后置--{Date tmp = *this;_day--;if (_day < 1){_month--;if (_month < 1){_year--;_month = 12;}_day = DayOfMonth();}return tmp;}int operator-(const Date& d){if (this > &d){Date tmp = d;int count = 0;while (tmp != *this){++tmp;++count;}return count;}else{Date tmp = *this;int count = 0;while(tmp != d){++tmp;count++;}return count;}}bool operator==(const Date& d){return (_year==d._year) && (_month == d._month) && (_day == d._day);}bool operator!=(const Date& d){return !(operator==(d));}bool operator>(const Date& d){return (_year > d._year)|| (_year == d._year && _month > d._month)|| (_year == d._year && _month > d._month && _day > d._day);}bool operator<(const Date& d){return !(operator>(d));}void Display(){cout<<this->_year<<"_"<<this->_month<<"_"<<this->_day<<endl;}protected:bool IsInvalidData(){if (_year < 1990 || _month < 1 || _month > 12 || _day < 1 || _day > DayOfMonth()){return false;} else{return true;}}int DayOfMonth(){int arr[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};if (IsLeepYear(_year) == true){arr[2] = 29;}return arr[_month];}bool IsLeepYear(int year){if (((year % 4 == 0)&&(year % 100 == 0)) || (year % 400 == 0)){return true;}else{return false;}}private:int _year;int _month;int _day;};</span></strong>


 

"test.cpp"

<strong><span style="font-size:18px;">#define _CRT_SECURE_NO_WARNINGS 1#include <iostream>using namespace std;#include "date.h"void Test1()//operator=  operator+{Date d(2016,7,8);d.Display();Date d1;d1.Display();d1 = d;//operator=d1.Display();//d = d + 10;  //operator+//d.Display();d = d + 100;  //operator+d.Display();}void Test2()//operator-  operator++{Date d(2016,7,8);d.Display();//d = d-10; // operator-//d.Display();d = d-100; // operator-d.Display();++d;//operator++d.Display();++d;//operator++d.Display();Date d1(2012,12,31);d1.Display();++d1;//operator++d1.Display();}void Test3()//operator--{Date d(2016,7,11);d.Display();--d;//operator--d.Display();Date d1(2000,1,1);d1.Display();--d1;d1.Display();}void Test4()//operator==  operator!=  operator>   operator<{Date d(2001,1,1);Date d1(2001,2,1);Date d2(2001,1,1);cout<<(d==d2)<<endl;//operator==cout<<(d==d1)<<endl;//operator==cout<<(d!=d2)<<endl;//operator!=cout<<(d!=d1)<<endl;//operator!=cout<<(d>d1)<<endl;//operator>cout<<(d1>d2)<<endl;//operator>cout<<(d<d1)<<endl;//operator<cout<<(d1<d2)<<endl;//operator<}void Test5()//operator-{Date d(2016,7,11);Date d1(2014,8,18);int ret = d-d1;cout<<ret<<endl;int tmp = d1-d;cout<<tmp<<endl;}int main(){//Test1();//Test2();//Test3();//Test4();Test5();system("pause");return 0;}</span></strong>


 

0 0