C++实现日期类
2016-06-05 10:12
357 查看
VS2013#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 >= 2000 && month > 0 && month<13 && day>0 < 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)
{
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)
{
return (*this > d) || (*this == d);
}
bool operator<(const Date &d)
{
return !(*this >= d);
}
bool operator<=(const Date &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 % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
return true;
else
return false;
}
int GetMonthDay(int year, int month) //根据已知年月获取该月的天数
{
int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day = monthArray[month];
if (month == 2 && IsLeapYear(year))
{
day += 1;
}
return day;
}
public:
Date operator+(int day) //给一个日期加day天
{
if (day < 0)
{
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 > 12)
{
tmp._year++;
tmp._month %= 12;
}
else
{
tmp._day = sumDays;
}
}
return tmp;
}
Date & operator+=(int day) //加上相应的天数后还要进行赋值
{
*this = operator+(day);
return *this;
}
Date operator-(int day) //给一个日期减去day天
{
if (day < 0)
{
return operator+(-day);
}
Date tmp = *this;
while (day >= tmp._day)
{
day -= tmp._day;
if (tmp._month == 1)
{
tmp._year--;
tmp._month = 12;
}
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 > 12)
{
++_year;
_month = 1;
}
}
return *this;
}
Date & operator++(int) //后置++
{
Date tmp = *this;
*this = operator++();
return tmp;
}
Date & operator--()
{
if (_day > 1)
{
--_day;
}
else
{
if (_month == 1)
{
--_year;
_month = 12;
_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 = 0;
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(2016, 1, 15);
Date d2(2016, 1, 16);
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(2016, 2,1);
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(2016, 2, 29);
d2.Display();
int ret = d1 - d2;
cout << ret << endl;
}
int main()
{
//Test1();
Test2();
system("pause");
return 0;
}
#include<assert.h>
#include <iostream>
using namespace std;
//在实现日期之间的运算之前,要先进行日期是否非法的检查
//实现日期间大小的比较
//计算两个日期间相差的天数
//计算一个日期加或减上day天后的日期
class Date
{
public:
Date(int year,int month,int day) //构造函数
{
if (year >= 2000 && month > 0 && month<13 && day>0 < 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)
{
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)
{
return (*this > d) || (*this == d);
}
bool operator<(const Date &d)
{
return !(*this >= d);
}
bool operator<=(const Date &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 % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
return true;
else
return false;
}
int GetMonthDay(int year, int month) //根据已知年月获取该月的天数
{
int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day = monthArray[month];
if (month == 2 && IsLeapYear(year))
{
day += 1;
}
return day;
}
public:
Date operator+(int day) //给一个日期加day天
{
if (day < 0)
{
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 > 12)
{
tmp._year++;
tmp._month %= 12;
}
else
{
tmp._day = sumDays;
}
}
return tmp;
}
Date & operator+=(int day) //加上相应的天数后还要进行赋值
{
*this = operator+(day);
return *this;
}
Date operator-(int day) //给一个日期减去day天
{
if (day < 0)
{
return operator+(-day);
}
Date tmp = *this;
while (day >= tmp._day)
{
day -= tmp._day;
if (tmp._month == 1)
{
tmp._year--;
tmp._month = 12;
}
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 > 12)
{
++_year;
_month = 1;
}
}
return *this;
}
Date & operator++(int) //后置++
{
Date tmp = *this;
*this = operator++();
return tmp;
}
Date & operator--()
{
if (_day > 1)
{
--_day;
}
else
{
if (_month == 1)
{
--_year;
_month = 12;
_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 = 0;
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(2016, 1, 15);
Date d2(2016, 1, 16);
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(2016, 2,1);
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(2016, 2, 29);
d2.Display();
int ret = d1 - d2;
cout << ret << endl;
}
int main()
{
//Test1();
Test2();
system("pause");
return 0;
}
相关文章推荐
- C++ Assert()断言机制原理以及使用
- C和C++语言中基本的数据类型
- C语言 实现一个函数,判断一个数是不是素数
- C语言 实现一个函数判断year是不是润年
- C语言 使用函数实现两个数的交换
- C语言输出数组的三种方法
- C语言 有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?打印出来?
- C语言 模拟三次密码输入
- C语言 整人程序“我是猪”
- vs的一个调试的程序
- POJ2446 Chessboard(二分图)
- MFC如何清理和删除一些消息映射函数
- C++第7次实验-数组素数排序
- c++标准库—命名空间std和头文件的演变 (各个头文件作用解析解释,惊喜!!!)
- c++学习笔记(小技巧)
- C++运算符优先级
- c++访问权限之构造和析构函数说明
- 宏 —— C/C++ 黑魔法
- c++中limits头文件
- c++中cstdlib头文件