//--------------------------------------------------------------------------
/*
**功能:实现日期的简单操作
**
**
**基本的成员函数:
** 构造函数,拷贝构造函数,析构函数,赋值运算符重载,操作符重载(两个日期间比较大小)
**
**日期类功能函数:
** 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 ;
}

最新文章

  1. 再战江湖。vuforia 初试
  2. 【python】lxml处理命名空间
  3. Nbimer族助手 部分控件不能用的解决方法(转)
  4. 二维数组去除重复值和array_unique函数
  5. 桥牌笔记L4D17:小心阻塞
  6. Linkclump – 批量打开多个链接[Chrome]
  7. UVa 814邮件传输代理的交互
  8. 【PHP代码审计】 那些年我们一起挖掘SQL注入 - 8.全局防护盲点的总结下篇
  9. MySQL安装配置,命令,异常纪要
  10. js 介绍
  11. K&amp;amp;R练习题6-1统计关键词出现的次数
  12. Vimperator技巧
  13. Spring+SpringMVC+MyBatis深入学习及搭建(六)——MyBatis关联查询
  14. [SDOI2010]所驼门王的宝藏
  15. 在MFC中通过访问IP地址下载文件到本地
  16. 关于python的GIL全局解释器锁的简单理解
  17. 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
  18. (转)Docker磁盘垃圾清理
  19. C#中当程序的访问权限不足时,Directory.Exists和File.Exists方法不会抛出异常报错
  20. 【LeetCode191】Number of 1 Bits★

热门文章

  1. ios 不通过import 调用其他控制器的方法
  2. 如何进入到Docker容器内部
  3. 通过小书匠编辑器让印象笔记和evernote支持markdown编辑
  4. Memcached的LRU和缓存命中率
  5. secureCrt Linux 文件传输
  6. 条件数(condition number)
  7. LVS+Keepalived(DR模式)学习笔记
  8. rebound是facebook的开源动画库
  9. 传统数据仓库架构与Hadoop的区别
  10. java中的多线程高并发与负载均衡的用途