没事可以自己在C++上模拟实现一个日期类。
Date.h文件
#ifndef __DATE_H__
#define __DATE_H__
#include <iostream>
using namespace std;
class Date{
friend ostream& operator<<(ostream& _cout, const Date& date);
friend istream& operator>>(istream& _cin, Date& date);
public:
Date(int year,int month,int day)
:_year(year)
,_month(month)
,_day(day)
{
}
Date(const Date& d)
:_year(d._year)
,_month(d._month)
,_day(d._day)
{
}
//日期的赋值
Date& operator=(const Date& d);
//指定年份是否为闰年
static bool IsLeapYear(const Date& date);
//当前年份是否为闰年
bool IsLeapYear();
//计算当前月份所在天数
int DayOfMonth();
//计算指定月份天数
static int DayOfMonth(const Date& date);
//计算当前日期day天之后日期
Date operator+(int day);
//计算当前日期day天之前日期
Date& operator-(int day);
//判断两个日期类是否相等
bool operator==(const Date& date);
//判断两个日期的大小D1>D2
bool operator>(const Date& date);
//判断两个日期的大小D1<D2
bool operator<(const Date& date);
//两个日期类D1 != D2
bool operator!=(const Date&date);
//计算两个日期之间差距
int operator-(const Date& date);
//显示
void Display();
//前置++
Date& operator++();
//后置++
Date operator++(int);
//前置--
Date& operator--();
//后置--
Date operator--(int);
private:
int _year;
int _month;
int _day;
};
#endif //__DATE_H__
Date.cpp文件
#include<iostream>
#include<Windows.h>
#include"Date.h"
using namespace std;
//日期的赋值
Date& Date::operator=(const Date& d){
if(this != &d){
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
//指定年份是否为闰年
bool Date::IsLeapYear(const Date& date){
int year = date._year;
return (year % 4 ==0 || year % 400 ==0) && (year % 100 !=0);
}
//当前年份是否为闰年
bool Date::IsLeapYear(){
int year = this->_year;
return (year % 4 ==0 || year % 400 ==0) && (year % 100 !=0);
}
//计算当前月份所在天数
int Date::DayOfMonth(){
int d = 0;
switch(this->_month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
d = 31;
break;
case 4:
case 6:
case 9:
case 11:
d = 30;
break;
case 2:
d = 28 + IsLeapYear();
break;
}
return d;
}
//计算指定月份天数
int Date::DayOfMonth(const Date& date){
int d = 0;
switch(date._month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
d = 31;
break;
case 4:
case 6:
case 9:
case 11:
d = 30;
break;
case 2:
d = 28 + IsLeapYear(date);
break;
}
return d;
}
//计算当前日期day天之后日期
Date Date::operator+(int day){
Date temp(*this);
if(day == 0)
return temp;
if(day > 0){
temp._day += day;
while(temp._day > DayOfMonth()){
temp._day -= DayOfMonth();
temp._month++;
if(temp._month > 12){
temp._month -= 12;
temp._year++;
}
}
return temp;
}
else
return temp -(-day);
}
//计算当前日期day天之前日期
Date& Date::operator-(int day){
Date& temp = (*this);
if(day == 0)
return temp;
if(day > 0){
temp._day -= day;
while(temp._day <= 0){
--temp._month;
if(temp._month == 0){
temp._month = 12;
--temp._year;
}
temp._day += DayOfMonth();
}
return temp;
}
else
return temp +(-day);
}
//判断两个日期类是否相等
bool Date::operator==(const Date& date){
return ((_year == date._year)
&&(_month == date._month)
&&(_day == date._day));
}
//判断两个日期的大小D1>D2
bool Date::operator>(const Date& date){
return ((_year > date._year)
||((_year == date._year)&&(_month > date._month))
||((_year == date._year)&&(_month == date._month)&&(_day > date._day)));
}
//判断两个日期的大小D1<D2
bool Date::operator<(const Date& date){
return!((*this > date)||(*this == date));
}
//两个日期类D1 != D2
bool Date::operator!=(const Date&date){
return !((*this) == date);
}
//计算两个日期之间差距
int Date::operator-(const Date& date){
Date datemax(*this);
Date datemin(date);
if((*this) < date){
Date temp(datemax);
datemax = datemin;
datemin = temp;
}
int day = 0;
while(1){
day++;
if(datemin + day == datemax)
break;
}
return day;
}
void Date::Display()
{
cout<<this->_year<<"-"<<this->_month<<"-"<<this->_day<<endl;
}
//前置++
Date& Date::operator++(){
this->_day += 1;
if((this->_day)>DayOfMonth()){
this->_day = 1;
this->_month++;
if((this->_month)>12){
this->_year++;
this->_month = 1;
}
}
return *this;
}
//后置++
Date Date::operator++(int){
Date temp(*this);
(*this) = ++(*this);
return temp;
}
//前置--
Date& Date::operator--(){
this->_day -= 1;
if((this->_day)< 1){
Date temp((*this)-1);
this->_day = DayOfMonth(temp);
this->_month--;
if((this->_month)< 1){
this->_year--;
this->_month = 12;
}
}
return *this;
}
//后置--
Date Date::operator--(int){
Date temp(*this);
(*this) = --(*this);
return temp;
}
ostream& operator<<(ostream& _cout, const Date& date)
{
_cout << date._year << "/" << date._month << "/" << date._day;
return _cout;
}
istream& operator>>(istream& _cin, Date& date)
{
_cin >> date._year >> date._month >> date._day;
return _cin;
}
Test.cpp测试文件
#include<stdio.h>
#include<Windows.h>
#include<iostream>
#include"Date.h"
using namespace std;
void test(){
Date D1(2017,10,30);
D1.Display();
//测试日期的赋值
/*Date D2(2017,10,20);
D2.Display();
(D1 = D2).Display();*/
//测试当前日期Day天之后日期
/*(D1+4).Display();*/
//测试当前日期Day天之前日期
/*(D1-4).Display();*/
//判断两个日期是否相等
/*Date D2(2017,10,20);
int i = 5;
i = (D1 == D2);
cout<<i<<endl;*/
//判断两个日期是否不等
/*Date D2(2017,10,20);
int i = 5;
i = (D1 != D2);
cout<<i<<endl;*/
//判断两个日期的大小
/*Date D2(2017,10,20);
int i = 5;
i = (D1 > D2);
cout<<i<<endl;*/
//计算两个日期之间相差的天数
/*Date D2(2017,10,20);
D2.Display();
int days = 0;
days = D1 - D2;
cout<<"两个日期之间相差"<<days<<"天"<<endl;*/
//前置加加 后置加加
/*Date D1(2016,12,31);
D1.Display();
(++D1).Display();
(D1++).Display();*/
//前置减减 后置减减 后置减减
/*Date D1(2017,1,1);
(--D1).Display();
(D1--).Display();
(D1--).Display();*/
}
int main(){
test();
system("pause");
return 0;
}
程序运行结果:
测试日期的赋值:
测试当前日期4天之后日期:
测试当前日期4天之前日期:
判断两个日期是否相等:
判断两个日期是否不等:
判断两个日期的大小(D1>D2):
计算两个日期之间相差的天数:
前置++ 后置++:
前置-- 后置-- 后置--: