简单日期类操作(c++)

简单的日期类操作,如一个日期加上指定天数后的日期,日期减去日期有多少天,求指定的一天是星期几,和一些有关类的函数。
头文件:

#ifndef _DATE_H__
#define _DATE_H__

#include<iostream>
using namespace std;

class Date
{
public:
    Date(int year = 1900, int month = 1, int  day = 1);
    Date(const Date& d);
    ~Date();
    Date& operator=(Date& d);
public:
    bool operator==(Date& d);
    bool operator!= (Date& d);
    bool operator>(Date& d);
    bool operator>=(Date& d);
    bool operator<(Date& d);
    bool operator<=(Date& d);
    ostream& operator<<(ostream&os, Date&d);
public:
    Date& operator++();
    Date operator++(int);
    Date& operator--();
    Date operator--(int);
    //日期+天数
    Date operator+(int day);
    //为加上指定天数的日期
    Date& operator+=(int day);
    //日期-天数
    Date operator-(int day);
    Date& operator-=(int day);
    //日期-日期
    int  operator-(const Date& d);
    //给定一个日期求这一天是星期几
    int IsWeekDay(Date&d);
public:
    bool IsLeapYear(int year);
    int GetMonthDay(int year, int month);
    void display();
private:
    int _year;
    int _month;
    int _day;
};

#endif//_DATE_H__

实现函数


//构造函数
Date::Date(int year, int month, int day)
{
    if (year > 0 && month > 0 && month<13 && day>0
        && day <= GetMonthDay(year,month))
    {
        _year = year;
        _month = month;
        _day = day;
    }
    else
    {
        cout << "输入错误" << endl;
        exit(EXIT_FAILURE);
    }
}

//拷贝构造函数
Date::Date(const Date& d)
{
    _year = d._year;
    _month = d._month;
    _day = d._day;
}

//析构函数
Date::~Date()
{
    //这里的析构函数不用做任何操作
}

//赋值操作运算符重载
Date& Date::operator=(Date& d)
{
    if (this != &d)
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
    }
    return* this;
}

//操作运算符的重载
bool Date::operator==(Date& d)
{
    if ((_year == d._year) && (_month == d._month) && (_day == d._day))
    {
        return true;
    }
    return false;
}

bool Date::operator!= (Date& d)
{

    return (!(operator==(d)));
}

bool Date:: operator>(Date& d)
{
    if (_year > d._year)
    {
        return true;
    }

    else if (_year == d._year)
    {
        if (_month > d._month)
        {
            return true;
        }
        else if (_month == d._month)
        {
            if (_day > d._day)
            {
                return true;
            }
        }
    }
    return false;
}

bool Date:: operator>=(Date& d)
{
    return(operator>(d) || operator==(d));
}

bool Date:: operator<(Date& d)
{
    return(!operator>=(d));
}

bool Date:: operator<=(Date& d)
{
    return(operator<(d) || operator==(d));
}

//判断是否为闰年
bool Date::IsLeapYear(int year)
{
    return(((0 == (year % 4)) && (0 != year % 100))||(0 == year%400));
}

//判断指定月有多少天
int Date::GetMonthDay(int year, int month)
{
    //一年每个月的天数
    int MonthArry[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    int MonthDays = MonthArry[month];
    //如果为闰年,且为2月,则为29天
    if (IsLeapYear(year) && (month == 2))
    {
        MonthDays = 29;
    }
    return MonthDays;
}

//日期前置++
Date& Date::operator++()
{
    //求这一年的这个月有多少天
    int ret = GetMonthDay(_year,_month);
    //判断这一天是不是这个月最后一天,如果不是直接++
    if (_day < ret)
    {
        _day++;
    }
    //如果是最后一天,需继续判断
    else if (_day == ret)
    {
        //判断是不是这一年的最后一个月
        if (12 == _month)
        {
            _day = 1;
            _month = 1;
            _year++;
        }
        else
        {
            _day = 1;
            _month++;
        }
    }
    return *this;
}

//日期后置++
Date Date::operator++(int)
{
    //求这一年的这个月有多少天
    int ret = GetMonthDay(_year, _month);
    Date tmp(*this);
    //判断这一天是不是这个月最后一天,如果不是直接++
    if (_day < ret)
    {
        _day++;
    }
    //如果是最后一天,需继续判断
    else if (_day == ret)
    {
        //判断是不是这一年的最后一个月
        if (12 == _month)
        {
            _day = 1;
            _month = 1;
            _year++;
        }
        else
        {
            _day = 1;
            _month++;
        }
    }
    return tmp;
}

//日期前置--
Date& Date:: operator--()
{
    //判断这一天是不是为一个月月初
    if (1 == _day)
    {
        //判断这个月是否为这一年的最后一个月
        if (1 == _month)
        {
            _day = 31;
            _month = 12;
            _year--;
        }
        else
        {
            _day = GetMonthDay(_year, _month - 1);
            _month--;
            _year;
        }
    }
    else
    {
        _day--;
        _month;
        _year;
    }
    return *this;
}

//日期后置--
Date Date::operator--(int)
{
    Date tmp(*this);
    //判断这一天是不是为一个月月初
    if (1 == _day)
    {
        //判断这个月是否为这一年的最后一个月
        if (1 == _month)
        {
            _day = 31;
            _month = 12;
            _year--;
        }
        else
        {
            _day = GetMonthDay(_year, _month - 1);
            _month--;
            _year;
        }
    }
    else
    {
        _day--;
        _month;
        _year;
    }
    return tmp;
}

//日期加天数
Date Date:: operator+(int day)
{
    Date tmp(*this);
    int SumDay = tmp._day + day;
    while (true)
    {
        if (SumDay <= GetMonthDay(tmp._year, tmp._month))
        {
            tmp._day = SumDay;
            return tmp;
        }
        else
        {
            SumDay -= GetMonthDay(tmp._year, tmp._month);
            tmp._month++;
            if (tmp._month > 12)
            {
                tmp._year++;
                tmp._month %= 12;
            }
        }
    }
}

//把该日期改为加上指定数目的天数后的日期
Date&Date:: operator+=(int day)
{
    *this = operator+(day);
    return* this;
}

//日期减去指定数目的天数
Date Date::operator-(int day)
{
    Date tmp(*this);
    int SumDays = tmp._day + day;
    while (true)
    {
        if (day < tmp._day)
        {
            tmp._day -= day;
            return tmp;
        }
        else
        {
            day -= tmp._day;
            if (1 == tmp._month)
            {
                tmp._month = 12;
                tmp._year--;
            }
            else
            {
                tmp._month--;
            }
            tmp._day = GetMonthDay(tmp._year, tmp._month);
        }
    }
}

//把该日期改为减去指定数目天数的日期
Date&Date::operator-=(int day)
{
    *this = operator-(day);
        return* this;
}

//日期-日期
int  Date::operator-(const Date& d)
{

    //异常情况
    if (_year < d._year)
    {
        return -1;
    }
    if ((_year == d._year) && (_month < d._month))
    {
        return -1;
    }
    if ((_year == d._year) && (_month == d._month) && (_day < d._day))
    {
        return -1;
    }
    //正常情况
    Date tmp_d(d);
    Date tmp_d_day(*this);
    int count = 0;  
    while ((tmp_d != tmp_d_day))
    {
        tmp_d.operator++();;
        count++;
    }
    return count;
}

//判断这一天是周几
int Date::IsWeekDay(Date&d)
{
    Date tmp(1900, 1, 1);
    int days = d.operator-(tmp);
    int weekday = days % 7+1;
    return weekday;
}

//打印函数
void Date::display()
{
    cout << _year << "-" << _month << "-" << _day << endl;
}

测试函数

#define _CRT_SECURE_NO_WARNINGS 1
#include"date.h"


void test1()
{
    Date d1(2017,7,17);
    /*int day=d1.IsWeekDay(d1);
    cout << day << endl;*/
    Date d2(2017,1,3);
    //Date d3(2016,2,9);
    //int ret = d1 - d3;
    //cout << ret << endl;
    //Date d4;
    //d4 = d3;
    //Date d4 = d3-d1;
    //Date d4 = d3++;
    //d1.display();
    //d2.display();
    //d4.display();
    //d4.display();
    if (d2 <= d1)
    {
        cout << "d4 == d3" << endl;
    }
    //if (d2 == d3)
    //{
    //  cout << "d2 == d3" << endl;
    //}
    //if (d2 != d3)
    //{
    //  cout << "d2 != d3" << endl;
    //}
    //if (d1 >= d3)
    //{
    //  cout << "d1>=d3" << endl;
    //}
    //if (d3 < d1)
    //{
    //  cout << "d3 < d1" << endl;
    //}
}

int main()
{
    test1();
    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值