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

C++实现简单的日期类

2016-12-24 22:58 330 查看
date.h
/*******************************************************************************************
*Author:MaJing
*Date:2016_12_24
*Word: if you want,you can do it
*******************************************************************************************/

#pragma once
#include <iostream>
#include <assert.h>
using namespace std;

class Date
{
public:
Date(int y = 1900, int m = 1, int d = 1);
Date(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=(const Date & d);   //返回该类型的引用是为了支持连等

Date & operator++();//前置++
Date operator++(int); //后置++
Date & operator--(); // 前置--
Date operator--(int); //后置--
int operator-(const Date &d);//计算两个日期之间差几天
Date operator-(int day); //计算几天之前的日期
Date operator+(int day); //计算几天之后的日期
Date operator+=(int day); //计算几天之后的日期
int operator-(const Date &d); //计算两个日期之间差几天
Date &operator-=(int day);//计算几天之前的日期

private:
//内部函数
bool IsVaild();  //检查传入参数的合法性
bool IsLeapYear();
int GetMonthDay(); //
private:
int _year;
int _month;
int _day;
};

date.cpp
#include "date.h"

Date::Date(int y = 1900, int m = 1, int d = 1)
{
_year = y;
_month = m;
_day = d;
assert(IsVaild());
}

Date::Date(const Date &d)  //复制构造函数看成构造函数的重载, 只有参数不同
{
_year = d._year;
_month = d._month;
_day = d._day;
}

//  比较运算符重载
bool Date::operator>(const Date& d)  //前面的日期在后面的日期之后  敲的不多吧,感觉像在默写
{
if (_year > d._year)
return true;
else if (_year < d._year)
return false;
else
{
if (_month > d._month)
return true;
else if (_month < d._month)
return false;
else
return _day > d._day;
}
}
bool Date::operator<(const Date& d)
{
return !(*this > d || *this == d);//比较的是*this
}
bool Date::operator>=(const Date& d)
{
return ((*this > d) || (*this == d)); //调用的时候是date对象 编译器处理成this指针  return 后面到底用不用加()
}
bool Date::operator<=(const Date& d)
{
return ((*this < d) || (*this == d));
}
bool Date::operator==(const Date& d) //类中被调用函数在调用函数之后可以找到这个函数吗?
{
return (_year == d._year && _month == d._month && _day == d._day);
}
bool Date::operator!=(const Date& d) //类中被调用函数在调用函数之后可以找到这个函数吗?
{
return !(_year == d._year && _month == d._month && _day == d._day);
}

//赋值运算符重载
Date &Date::operator=(const Date & d)   //返回该类型的引用是为了支持连等
{
if (this != &d)  // 此处为比较地址
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}

Date & Date::operator++()//前置++
{
_day += 1;
if (_day > GetMonthDay())
{
if (_month == 12)
{
_month = 1;
_year += 1;
}
else
{
_month++;
}
_day = 1;
}
return *this;
}

Date Date::operator++(int) //后置++
{
Date tmp(*this);  //Date 类型
operator++();
return tmp;
}

Date & Date::operator--() // 前置--
{
_day -= 1;
if (_day < 1)
{
--_month;
if (_month < 1)
{
--_year;
_month = 12;
}
_day = GetMonthDay();
}
return *this;   //为什么要返回 是要支持连加
}

Date Date::operator--(int) //后置--
{
Date tmp(*this);
operator--();
return tmp;
}

Date Date::operator-(int day) //计算几天之前的日期
{
Date tmp(*this);
tmp -= day;
return tmp;
}

Date Date::operator+(int day) //计算几天之后的日期
{
Date tmp(*this);
tmp += day;
return tmp;
}

Date &Date::operator-=(int day) //计算几天之前的日期
{
_day -= day;
while (_day < 1)
{
--_month;
if (_month < 1)
{
--_year;
_month = 12;
}
_day += GetMonthDay();
}
return *this;
}

Date Date::operator+=(int day) //计算几天之后的日期
{
_day += day;
while (_day > GetMonthDay())
{
_day -= GetMonthDay();
_month++;
if (_month > 12)
{
_month = 1;
_year += 1;
}
}
return *this;
}

int Date::operator-(const Date &d)//计算两个日期之间差几天
{
Date later = (*this) > d ? (*this) : d;
Date formar = (*this) < d ? (*this) : d;
int count = 0;
while (formar != later)
{
++formar;
++count;
}
return count;
}

//内部函数
bool Date::IsVaild()  //检查传入参数的合法性
{
if (_year > 0
&& _month > 0 && _month <13
&& _day > 0 && _day <= GetMonthDay())
return true;
else
return false;
}

bool Date::IsLeapYear()
{
if ((_year % 4 == 0 && _year % 100 != 0) || _year % 400 == 0)
return true;
else
return false;
}

int Date::GetMonthDay() //
{
static const int dayTable[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (_month == 2 && IsLeapYear())
{
return dayTable[2] + 1;
}
else
return dayTable[_month];
}

test.c

#include "date.h"

int main()
{
Date d1(2016, 12,1);
//d1-=10; //2015 12 27
Date d2(2016, 12, 23);
//cout <<(d1.operator>=(d2)) << endl; // d1.operator>(d2) d1>d2; operator>(d1,d2)由于隐式传参不行
//d2 += 10; // 2017 1 2 想好你要的结果,就能快一些
int distance = d2 - d1; // 22
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: