您的位置:首页 > 编程语言 > C语言/C++

【C++基础】实现一个日期类

2018-03-21 17:09 501 查看
又是一个昏昏欲睡的下午~好了不多说,开始总结自己写的代码吧
我写这个日期类,一个是判断输入的时期是否合法,也就是在构造函数那块,还有一个是运算符重载的时候
先讲一下如何判断日期是否合法,写一个函数返回当前月的天数,代码如下:

//判断闰年
bool Date::isleap(int year) {
if ((_year % 4 == 0 && _year % 100 != 0) || (_year % 400 == 0)) {
return true;
}
return false;
}
//求出当前月的天数
int Date::_GetMonthDay(int year, int month) {
//对year和month的合法性进行判断
assert(month > 0 && month < 13);
assert(year >= 0);
//不同月份的天数
int arr_month[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (isleap(year) && month == 2) {
//是闰年的二月,就把二月的天数+1
return arr_month[month] + 1;
}
return arr_month[month];
}因为闰年的二月是29天,所以需要判断当前年月是否是闰年的二月
运算符重载的一些操作就不说了,都是一些细节性的问题,在判断两个日期类大于小于大于等于之类的时候,只需要实现两个操作符,一个等于,和一个大于或者小于的函数,其他的都可以调用这两个函数来实现了
这里主要讲一讲求出两个日期之间的时间//两个日期相隔天数
int Date::operator-(const Date& d) {
int day = 0;
Date Max, Min;
if (*this > d) {
Max = *this;
Min = d;
}
else {
Max = d;
Min = *this;
}
while (Min != Max) {
--Max;
++day;
}
return day;
}用一个day变量来存储天数,然后找出两个日期之间较大的,让它进行自减操作再让day变量进行自增操作直到两个日期类相等,这样就计算出了两个日期类之间的变量
其实一个日期类是个比较简单的类,下面是全部的代码:
Date.h#pragma once

#include<iostream>

using namespace std;

/*日期类*/
class Date {

public:
//构造函数
Date(int year = 1900, int month = 1, int day = 1)
:_year(year)
, _month(month)
, _day(day) {
//对合法性进行判断
//判断月
if ((year >= 0)
&& (month >= 1 && month <= 12)
&& (day >= 1 && day <= _GetMonthDay(year, month))) {
//当前年月日合法,可以进行初始化
this->_year = year;
this->_month = month;
this->_day = day;
}
else {
//当前月不合法,进行初始化
cout << "传入的参数不合法" << endl;
_year = 1900;
_month = 1;
_day = 1;
}
}
//拷贝构造函数
Date(const Date& d) {
_year = d._year;
_month = d._month;
_day = d._day;
}
//析构函数
~Date() {
cout << "~Date" << endl;
}

//操作符重载
//赋值
Date& operator=(const Date& d);
//判断相等
bool operator==(const Date& d);
//判断不等
bool operator!=(const Date& d);
//大于
bool operator>(const Date& d);
//大于等于
bool operator>=(const Date& d);
//小于
bool operator<(const Date& d);
//小于等于
bool operator<=(const Date& d);
//加天数
Date operator+(int day);
//+=
Date& operator+=(int day);
//减天数
Date operator-(int day);
//-=
Date& operator-=(int day);
//两个日期相隔天数
int operator-(const Date& d);
//自加天数 前置++
Date& operator++();
//自加天数 后置++
Date operator++(int);
//自减天数 前置--
Date& operator--();
//自减天数 后置--
Date operator--(int);
//输出
//输入
void Show() {
cout << "year = " << _year << endl;
cout << "month = " << _month << endl;
cout << "day = " << _day << endl;
}
private:
//判断闰年
bool isleap(int year);
//得到当前月的日期
int _GetMonthDay(int year, int month);
private:
int _year;
int _month;
int _day;
};
Date.c#include"Date.h"
#include<iostream>
#include<stdlib.h>
#include<assert.h>

using namespace std;

//判断闰年
bool Date::isleap(int year) {
if ((_year % 4 == 0 && _year % 100 != 0) || (_year % 400 == 0)) {
return true;
}
return false;
}
//求出当前月的天数
int Date::_GetMonthDay(int year, int month) {
//对year和month的合法性进行判断
assert(month > 0 && month < 13);
assert(year >= 0);
//不同月份的天数
int arr_month[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (isleap(year) && month == 2) {
//是闰年的二月,就把二月的天数+1
return arr_month[month] + 1;
}
return arr_month[month];
}

//赋值
Date& Date::operator=(const Date& d) {
//这里传入的是this指针,如果两个要赋值的是自身,就不用去赋值了
if (this != &d) {
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}

//判断相等
bool Date::operator==(const Date& d) {
if (this == &d) return true;
return (_year == d._year && _month == d._month && _day == d._day);
}

//判断不等
bool Date::operator!=(const Date& d) {
return (!(*this == d));
}

//大于
bool Date::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 Date::operator>=(const Date& d) {
if (operator==(d) || operator>(d)) {
//大于和等于都为真则大于等于
return true;
}
return false;
}

//小于
bool Date::operator<(const Date& d) {
if (!operator>=(d)) return true;
return false;
}

//小于等于
bool Date::operator<=(const Date& d) {
if (operator<(d) || operator==(d)) return true;
return false;
}

//加天数
Date Date::operator+(int day) {
Date tmp(*this);
if (day < 0) {
return *this = operator-(-day);
}
else {
tmp._day += day;
while (tmp._day > _GetMonthDay(tmp._year, tmp._month)) {
tmp._day -= _GetMonthDay(tmp._year, tmp._month);
if (tmp._month == 12) {
++tmp._year;
tmp._month = 1;
}
else {
++tmp._month;
}
}//_day < _GetMonthDay(_year, _month),天数如果合法那么日期也就合法了
}//day < 0
return tmp;
}

//+=
Date& Date::operator+=(int day) {
//这里是的*this是如何加的?
*this = *this + day;
return *this;
}

//减天数
Date Date::operator-(int day) {
Date tmp(*this);
if (day < 0) {
return *this + (-day);
}
else {
tmp._day -= day;
while (tmp._day <= 0) {
if (tmp._month = 1) {
--tmp._year;
tmp._month = 12;
}
else {
--tmp._month;
}
//因为是借位借的是上个月的天数,所以要先变化年数和天数
tmp._day += _GetMonthDay(tmp._year, tmp._month);
}
}
return tmp;
}

//-=
Date& Date::operator-=(int day) {
*this = *this - day;
return *this;
}

//自加天数 前置++
Date& Date::operator++() {
++_day;
if (_day > _GetMonthDay(_year, _month)) {
_day = 1;
if (_month == 12) {
++_year;
_month = 1;
}
else {
++_month;
}
}
return *this;
}

//自加天数 后置++
Date Date::operator++(int) {
Date tmp(*this);
++_day;
if (_day > _GetMonthDay(_year, _month)) {
_day = 1;
if (_month == 12) {
++_year;
_month = 1;
}
else {
++_month;
}
}
return tmp;
}

//自减天数 前置--
Date& Date::operator--() {
--_day;
if (_day < 0) {
if (_month == 1) {
--_year;
_month = 12;
}
else {
--_month;
}
_day = _GetMonthDay(_year, _month);
}
return *this;
}

//自减天数 后置--
Date Date::operator--(int) {
Date tmp(*this);
--_day;
if (_day < 0) {
if (_month == 1) {
--_year;
_month = 12;
}
else {
--_month;
}
_day = _GetMonthDay(_year, _month);
}
return tmp;
}

//两个日期相隔天数
int Date::operator-(const Date& d) {
int day = 0;
Date Max, Min;
if (*this > d) {
Max = *this;
Min = d;
}
else {
Max = d;
Min = *this;
}
while (Min != Max) {
--Max;
++day;
}
return day;
}测试代码就不贴了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: