这篇文章主要介绍了C++如何实现日期类,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
具体内容如下
#include<iostream>
#include<stdlib.h>
using namespace std;
class Date
{
public:
//构造函数
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;
}
}
//拷贝函数
Date(const Date& d)
: _year(d._year)
, _month(d._month)
, _day(d._day)
{}
//析构函数
~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;
}
//判断当前月份多少天
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;
}
// 当前日期days天后是什么日期?
Date operator+(int days)
{
Date tmp(*this);
tmp._day += days;
ToCorrect(tmp);
return tmp;
}
// 当前日期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);
}
// 重载取地址符号
Date* operator&()
{
}
// 前置++
Date& operator++()
{
(*this)++;
return *this;
}
// 后置++
Date operator++(int)//通过返回值和传参区别前置和后置++
{
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;
}
感谢你能够认真阅读完这篇文章,希望小编分享的“C++如何实现日期类”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。