日期类(C++实现)
2024-09-07 09:16:07
//--------------------------------------------------------------------------
/*
**功能:实现日期的简单操作
**
**
**基本的成员函数:
** 构造函数,拷贝构造函数,析构函数,赋值运算符重载,操作符重载(两个日期间比较大小)
**
**日期类功能函数:
** 1:计算一个日期加上多少天数后的日期
** 2:把该日期改为加上指定数目的天数后的日期
** 3:一个日期减上多少天数后的日期
** 4:把该日期改为减去指定数目的天数后的日期
** 5:该日期加1(前置++)(后置++)
** 6:该日期减1(前置--)(后置--)
** 7:计算某日期到未来某日期间隔的天数
**
**
** By :Lynn-Zhang
**
*/
//--------------------------------------------------------------------------- #define _CRT_SECURE_NO_WARNINGS 1 #include<assert.h>
#include <iostream>
using namespace std; //在实现日期之间的运算之前,要先进行日期是否非法的检查
//实现日期间大小的比较
//计算两个日期间相差的天数
//计算一个日期加或减上day天后的日期 class Date
{
public:
Date(int year,int month,int day) //构造函数
{
if (year >= && month > && month< && day> < GetMonthDay(year, month)) //判断日期是否非法
{
_year = year;
_month = month;
_day = day;
}
else
{
cout << "日期非法" << endl;
assert(false);
}
} Date(const Date& d) //拷贝构造
{
_year = d._year;
_month = d._month;
_day = d._day;
} void Display() // 打印日期
{
cout << _year << "-" << _month << "-" << _day << endl;
} ~Date() //析构函数
{
//cout << "~Date()" << endl;
} //运算符重载(两个日期间比较大小)
bool operator==(const Date &d) //判断两个日期是否相等
{
return (_year == d._year) && (_month == d._month) && (_day == d._day);
} bool operator>(const Date &d) //判断本日期是否大于日期d
{
if (_year > d._year)
{
return true;
}
else if (_year == d._year&&_month > d._month)
{
return true;
}
else if (_year == d._year&&_month == d._month&&_day > d._day)
{
return true;
}
else
return false;
} bool operator>=(const Date &d) //判断本日期是否大于或等于日期d
{
return (*this > d) || (*this == d);
} bool operator<(const Date &d) //判断本日期是否小于日期d
{
return !(*this >= d);
}
bool operator<=(const Date &d) //判断本日期是否小于等于日期d
{
return !(*this > d);
} //赋值运算符重载
Date operator=(const Date &d)
{
if (this != &d)
{
this->_year = d._year;
this->_month = d._month;
this->_day = d._day;
}
return *this;
} private:
bool IsLeapYear(int year) //判断是否闰年
{
if (((year % == ) && (year % != )) || (year % == ))
return true;
else
return false;
} int GetMonthDay(int year, int month) //根据已知年月获取该月的天数
{
int monthArray[] = { , , , , , , , , , , , , }; int day = monthArray[month]; if (month == && IsLeapYear(year))
{
day += ;
} return day;
} public:
Date operator+(int day) //给一个日期加day天
{
if (day < )
{
return operator-(-day);
}
Date tmp = *this;
int sumDays = tmp._day + day;
while (sumDays > GetMonthDay(tmp._year, tmp._month))
{
sumDays -= GetMonthDay(tmp._year, tmp._month);
tmp._month++;
if (tmp._month > )
{
tmp._year++;
tmp._month %= ;
}
else
{
tmp._day = sumDays;
}
}
return tmp;
} Date & operator+=(int day) //加上相应的天数后还要进行赋值
{
*this = operator+(day);
return *this;
} Date operator-(int day) //给一个日期减去day天
{
if (day < )
{
return operator+(-day);
}
Date tmp = *this;
while (day >= tmp._day)
{
day -= tmp._day;
if (tmp._month == )
{
tmp._year--;
tmp._month = ;
}
else
{
tmp._month--;
}
tmp._day = GetMonthDay(tmp._year, tmp._month);
}
tmp._day -= day;
return tmp;
} Date & operator-=(int day) //减去相应的天数后赋值
{
*this = operator-(day);
return *this;
}
Date & operator++() //日期加1(前置++)
{
++_day;
if (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
++_month;
if (_month > )
{
++_year;
_month = ;
}
}
return *this;
} Date & operator++(int) //后置++
{
Date tmp = *this;
*this = operator++();
return tmp;
} Date & operator--() // 日期减1 (前置--)
{
if (_day > )
{
--_day;
}
else
{
if (_month == )
{
--_year;
_month = ;
_day = GetMonthDay(_year, _month);
}
else
{
--_month;
_day = GetMonthDay(_year, _month);
}
}
return *this;
} Date & operator--(int) //后置--
{
Date tmp = *this;
*this = operator--();
return tmp;
} //计算两个日期相差的天数
int operator-(Date & d)
{
if (_year <d. _year)
{
Date tmp =* this;
*this = d;
d = tmp;
}
else if (_year == d._year&&_month < d._month)
{
Date tmp = *this;
*this = d;
d = tmp;
}
else if (_year ==d. _year&&_month == d._month&&_day < d._day)
{
Date tmp = *this;
*this = d;
d = tmp;
}
Date tmp1(*this);
Date tmp2(d);
int ret = ;
while (!(tmp2 == tmp1))
{
tmp2.operator++();
ret++;
}
return ret;
} private:
int _year;
int _month;
int _day;
}; void Test1() //测试用例
{
/*Date d1(2016, 1, 15);
d1.Display();
Date d2 = d1;
d2.Display();
Date d3;
d3 = d1;
d2.Display();*/ Date d1(, , );
Date d2(, , );
d1.Display();
d2.Display();
// == 0
//bool ret = d1 == d2;
//cout << ret << endl;
// > 1
//bool ret = d1 >d2;
//cout << ret << endl;
// < 0
//bool ret = d1 < d2;
//cout << ret << endl;
// >= 1
//bool ret = d1 >= d2;
//cout << ret << endl;
// <= 0
//bool ret = d1 <= d2;
//cout << ret << endl;
// =
//d1 = d2;
//d1.Display();
//d2.Display();
}
void Test2()
{
Date d1(, ,);
d1.Display();
//Date ret = d1+1;
//ret.Display();
//Date ret=d1+70;
//ret.Display();
//Date ret=d1-25;
//ret.Display();
////Date ret = d1 += 70;
////ret.Display();
////d1.Display();
//Date ret = d1 -= 70;
//ret.Display();
//d1.Display();
//d1++;
//d1.Display();
//++d1;
//d1.Display();
//--d1;
//d1.Display();
//d1--;
//d1.Display();
Date d2(, , );
d2.Display();
int ret = d1 - d2;
cout << ret << endl;
}
int main()
{
//Test1();
Test2();
system("pause");
return ;
}
最新文章
- 再战江湖。vuforia 初试
- 【python】lxml处理命名空间
- Nbimer族助手 部分控件不能用的解决方法(转)
- 二维数组去除重复值和array_unique函数
- 桥牌笔记L4D17:小心阻塞
- Linkclump – 批量打开多个链接[Chrome]
- UVa 814邮件传输代理的交互
- 【PHP代码审计】 那些年我们一起挖掘SQL注入 - 8.全局防护盲点的总结下篇
- MySQL安装配置,命令,异常纪要
- js 介绍
- K&;amp;R练习题6-1统计关键词出现的次数
- Vimperator技巧
- Spring+SpringMVC+MyBatis深入学习及搭建(六)——MyBatis关联查询
- [SDOI2010]所驼门王的宝藏
- 在MFC中通过访问IP地址下载文件到本地
- 关于python的GIL全局解释器锁的简单理解
- The Tomcat connector configured to listen on port 8080 failed to start. The port may already be in use or the connector may be misconfigured
- (转)Docker磁盘垃圾清理
- C#中当程序的访问权限不足时,Directory.Exists和File.Exists方法不会抛出异常报错
- 【LeetCode191】Number of 1 Bits★