C++實(shí)現(xiàn)日期類(Date)
本文實(shí)例為大家分享了C++實(shí)現(xiàn)日期類的具體代碼,供大家參考,具體內(nèi)容如下
#include<iostream>
#include<stdlib.h>
using namespace std;
class Date
{
public:
//構(gòu)造函數(shù)
Date(int year = 1900, int month = 1, int day = 1)
:_year(year)
, _month(month)
, _day(day)
{
if (!IsInvalidDate(_year, _month, _day))
{
_year = 1900;
_month = 1;
_day = 1;
}
}
//拷貝函數(shù)
Date(const Date& d)
: _year(d._year)
, _month(d._month)
, _day(d._day)
{}
//析構(gòu)函數(shù)
~Date()
{}
//判斷是不是閏年
bool IsLeapYear(int year)
{
if ((year % 400 == 0) ||
((year % 4 == 0) && (year % 100 != 0)) )
{
return true;
}
return false;
}
//判斷是不是合法日期
bool IsInvalidDate(int year, int month, int day)
{
if ((year < 1) ||
(month < 0 || month >12) ||
(day < 0 || day > YearsOfMonth(year, month)))
{
return false;
}
return true;
}
//判斷當(dāng)前月份多少天
int YearsOfMonth(int year, int month)
{
int day;
int days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
day = days[month];
if (month == 2 && IsLeapYear(year))
{
day += 1;
}
return day;
}
//修正日期
Date ToCorrect(Date &d)
{
while (d._day > YearsOfMonth(d._year, d._month) || d._day <= 0)
{
if(d._day <= 0)
{
d._day += YearsOfMonth(d._year,( d._month - 1));
if (d._month == 1)
{
d._month = 12;
d._year--;
}
else
{
d._month--;
}
}
else
{
d._day -= YearsOfMonth(d._year, d._month);
if (d._month == 12)
{
d._year++;
d._month = 1;
}
else
{
d._month++;
}
}
}
return d;
}
// 當(dāng)前日期days天后是什么日期?
Date operator+(int days)
{
Date tmp(*this);
tmp._day += days;
ToCorrect(tmp);
return tmp;
}
// 當(dāng)前日期days天前是什么日期?
Date operator-(int days)
{
Date tmp(*this);
tmp._day -= days;
ToCorrect(tmp);
return tmp;
}
// 日期比大小
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 (_year < d._year ||
(_year == d._year && _month < d._month) ||
(_year == d._year && _month == d._month && _day < d._day));
}
bool operator==(const Date& d)
{
return ((_year == d._year) && (_month == d._month) && (_day == d._day));
}
bool operator!=(const Date& d)
{
return !(*this == d);
}
bool operator>=(const Date &d)
{
return !(*this<d);
}
bool operator<=(const Date &d)
{
return !(*this>d);
}
// 重載取地址符號(hào)
Date* operator&()
{
}
// 前置++
Date& operator++()
{
(*this)++;
return *this;
}
// 后置++
Date operator++(int)//通過(guò)返回值和傳參區(qū)別前置和后置++
{
Date tmp(*this);
(*this) = (*this) + 1;
return tmp;
}
// 前置--
Date& operator--()
{
(*this)--;
return *this;
}
// 后置--
Date operator--(int)
{
Date tmp(*this);
(*this)--;
return tmp;
}
void Display()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d(2018, 9, 9);
d.Display();
Date d1 = d + 50;
d1.Display();
d1 =d1 - 50;
d1.Display();
cout << "------"<<endl;
cout << "前置++" << endl;
d1.Display();
(++d1).Display();
d1.Display();
cout << "后置++" << endl;
d1.Display();
(d1++).Display();
d1.Display();
cout << "------" << endl;
system("pause");
return 0;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Linux下C語(yǔ)言的幾道經(jīng)典面試題小結(jié)(分享)
下面小編就為大家?guī)?lái)一篇Linux下C語(yǔ)言的幾道經(jīng)典面試題小結(jié)(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
C++實(shí)現(xiàn)LeetCode(201.數(shù)字范圍位相與)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(201.數(shù)字范圍位相與),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
C數(shù)據(jù)結(jié)構(gòu)之單鏈表詳細(xì)示例分析
以下是對(duì)C語(yǔ)言中的單鏈表進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下2013-08-08
Cocos2d-x UI開(kāi)發(fā)之CCControlColourPicker控件類使用實(shí)例
這篇文章主要介紹了Cocos2d-x UI開(kāi)發(fā)之CCControlColourPicker控件類使用實(shí)例,本文代碼中包含大量注釋來(lái)講解CCControlColourPicker控件類的使用,需要的朋友可以參考下2014-09-09
如何使用visual studio2019創(chuàng)建簡(jiǎn)單的MFC窗口(使用C++)
這篇文章主要介紹了如何使用visual studio2019創(chuàng)建簡(jiǎn)單的MFC窗口(使用C++),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
VC++實(shí)現(xiàn)程序開(kāi)機(jī)啟動(dòng)運(yùn)行的方法
這篇文章主要介紹了VC++實(shí)現(xiàn)程序開(kāi)機(jī)啟動(dòng)運(yùn)行的方法,很實(shí)用的功能,需要的朋友可以參考下2014-08-08

