日期类的实现

来源:互联网 发布:淘宝加盟商 编辑:程序博客网 时间:2024/05/22 00:26

代码如下:

#pragma once#include<iostream>using namespace std;#include<ctime>#include<cassert>class Date{public:Date(int years = 1900, int month = 1, int day = 1);Date operator+(int day);Date& operator+=(int day);Date operator-(int day);Date& operator-=(int day);int operator-(const Date& d);Date& operator++(); //前置++Date& operator++(int);//后置++Date& operator--(); //前置--Date& operator--(int);//后置--void Display();int DayInYear(const Date& d);bool IsLeapYear(int year);friend void Swap(Date& d1, Date& d2);friend bool operator>(const Date& d1, const Date& d2);friend bool operator<(const Date& d1, const Date& d2);friend bool operator==(const Date& d1, const Date& d2);friend bool operator!=(const Date& d1, const Date& d2);protected:bool isValid(const Date& d){if ((d._year>0) && (d._month > 0 && d._month<13) && (d._day>0 && d._day <= GetMonthDays(d._year, d._month))){return true;}return false;}int GetMonthDays(int year, int month){static int MonthDays[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//?if (month != 2){return MonthDays[month];}else if (IsLeapYear(year)){return 29;}else{return 28;}}private:int _year;int _month;int _day;};
#include"Date.h"Date::Date(int year,int month,int day):_year(year), _month(month), _day(day){assert(isValid(*this) != 0);}Date Date::operator+(int day){Date tmp(*this);tmp._day += day;while(tmp._day > GetMonthDays(tmp._year, tmp._month)){tmp._day -= GetMonthDays(tmp._year, tmp._month);if (12 == tmp._month){tmp._year ++;tmp._month = 1;}else{tmp._month++;}}return tmp;}Date& Date::operator+=(int day){*this = *this + day;return *this;}Date Date::operator-(int day){Date tmp(*this);tmp._day -= day;while (!isValid(tmp)){if (tmp._month == 1){tmp._year--;tmp._month = 12;}else{tmp._month--;}tmp._day += GetMonthDays(tmp._year, tmp._month);  //?}return tmp;}Date& Date::operator-=(int day){    *this=*this - day;return *this;}void Swap(Date& d1, Date& d2){int tmp = d1._year;d2._year = tmp;d1._year = d2._year;tmp = d1._month;d2._month = tmp;d1._month = d2._month;tmp = d1._day;d2._day = tmp;d1._day = d2._day;}//重载一个自增运算符,然后使用计数器来计算两个日期之间的差值int Date::operator-(const Date& d1)//两个天数相减(形如:d1-d2){int flag = 1;if (d1 > *this){flag = -flag;}int count = 0;Date minDate = *this;Date maxDate = d1;if (minDate > maxDate){Swap(minDate, maxDate);}while (minDate != maxDate){minDate++;count++;}return flag * count;}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;}void Date::Display(){cout << _year << "-" << _month << "-" << _day << endl;}bool Date::IsLeapYear(int year){if ((year % 4 == 0) && (year % 100 != 0) || (year % 100 == 0)){return true;}return false;}bool operator>(const Date& d1, const Date& d2){if ((d1._year > d2._year) || (d1._year == d2._year)\&& ((d1._month > d2._month) || ((d1._month == d2._month) && (d1._day > d2._day)))){return true;}return false;}bool operator<(const Date& d1, const Date& d2){return (!(d1>d2) || (d1 == d2));}bool operator==(const Date& d1, const Date& d2){if ((d1._year == d2._year) && (d1._month == d1._month) && (d1._day == d2._day)){return true;}return false;}bool operator!=(const Date& d1, const Date& d2){return (!(d1 == d2));}
         

0 0
原创粉丝点击