日期类的实现—Date

MyDate.h

#ifndef __MY_DATE_H__  
#define __MY_DATE_H__  

#include <iostream>  
#include <stdlib.h>  
using namespace std;

class my_date
{
    friend ostream& my_date::operator<<(ostream& os, const my_date& q);
public:
    my_date(int x = 1970, int y = 1, int z = 1)
        :year(x)
        , month(y)
        , day(z)
    {}

    my_date operator+(int m);
    my_date& operator+=(int m);
    // void add(int m);//加天数  

    my_date operator-(int m);
    my_date& operator-=(int m);
    // void del(int m);//减天数  

    int operator-(my_date& q);
    //int diff(my_date& q);//日期差  
    //void display();//显示  

private:
    int year, month, day;
};
my_date my_date::operator+(int m)
{
    my_date tmp_date(*this);
    int d[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, temp, f;//d为12个月每个月天数的数组  
    f = 0;
    while (m>0)
    {
        if (tmp_date.year % 100 == 0)
            d[1] = (tmp_date.year % 400 == 0 ? 29 : 28);//如果是闰年,2月天数为29  
        else
            d[1] = (tmp_date.year % 4 == 0 ? 29 : 28);//如果是闰年,2月天数为29  
        temp = d[tmp_date.month - 1] - tmp_date.day;
        if (temp >= m)//如果所加天数未超过当月剩余天数  
        {
            tmp_date.day = tmp_date.day + m;
            break;
        }
        m -= temp;
        tmp_date.day = 0;
        tmp_date.month++;
        for (; tmp_date.month <= 12; tmp_date.month++)//开始月遍历进行日期计算  
        {
            if (d[tmp_date.month - 1] >= m)
            {
                tmp_date.day += m;
                break;
            }
            else
            {
                m -= d[tmp_date.month - 1];
            }
        }
        if (tmp_date.month <= 12)//若所加天数在本年12月内  
            break;
        tmp_date.month = 1;//所加天数超过今天剩余天数,月初始化为1月,即从下年一月继续算  
        tmp_date.year++;//加一年  
    }

    return tmp_date;
}

my_date& my_date::operator+=(int m)
{
    int d[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, temp, f;//d为12个月每个月天数的数组  
    f = 0;
    while (m>0)
    {
        if (year % 100 == 0)
            d[1] = (year % 400 == 0 ? 29 : 28);//如果是闰年,2月天数为29  
        else
            d[1] = (year % 4 == 0 ? 29 : 28);//如果是闰年,2月天数为29  
        temp = d[month - 1] - day;
        if (temp >= m)//如果所加天数未超过当月剩余天数  
        {
            day = day + m;
            break;
        }
        m -= temp;
        day = 0;
        month++;
        for (; month <= 12; month++)//开始月遍历进行日期计算  
        {
            if (d[month - 1] >= m)
            {
                day += m;
                break;
            }
            else
            {
                m -= d[month - 1];
            }
        }
        if (month <= 12)//若所加天数在本年12月内  
            break;
        month = 1;//所加天数超过今天剩余天数,月初始化为1月,即从下年一月继续算  
        year++;//加一年  
    }

    return *this;
}

my_date my_date::operator-(int m)
//void my_date::del(int m)//本函数注释与add基本相同  
{
    my_date tmp_date(*this);
    int d[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, f;
    f = 0;
    while (m>0)
    {
        if (tmp_date.year % 100 == 0)
            d[1] = (tmp_date.year % 400 == 0 ? 29 : 28);
        else
            d[1] = (tmp_date.year % 4 == 0 ? 29 : 28);
        if (tmp_date.day >= m)
        {
            tmp_date.day = tmp_date.day - m;
            break;
        }
        m -= tmp_date.day;
        tmp_date.month--;
        for (; tmp_date.month >= 1; tmp_date.month--)
        {
            if (d[tmp_date.month - 1] >= m)
            {
                tmp_date.day = d[tmp_date.month - 1] - m;
                break;
            }
            else
            {
                m -= d[tmp_date.month - 1];
            }
        }
        if (tmp_date.month >= 1)
            break;
        tmp_date.month = 12;
        tmp_date.year--;
        tmp_date.day = 31;
    }
    return tmp_date;
}

my_date& my_date::operator-=(int m)
//void my_date::del(int m)//本函数注释与add基本相同  
{
    int d[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, f;
    f = 0;
    while (m>0)
    {
        if (year % 100 == 0)
            d[1] = (year % 400 == 0 ? 29 : 28);
        else
            d[1] = (year % 4 == 0 ? 29 : 28);
        if (day >= m)
        {
            day = day - m;
            break;
        }
        m -= day;
        month--;
        for (; month >= 1; month--)
        {
            if (d[month - 1] >= m)
            {
                day = d[month - 1] - m;
                break;
            }
            else
            {
                m -= d[month - 1];
            }
        }
        if (month >= 1)
            break;
        month = 12;
        year--;
        day = 31;
    }
    return *this;
}

int my_date::operator-(my_date& q)
//int my_date::diff(my_date& q)  
{
    int differece;
    int d[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, m, i;
    my_date temp;
    my_date *pSmall, *pBig;
    if (year == q.year)//如果年相同  
    {
        if (month == q.month)//若月相同  
        {
            differece = q.day - day;
        }
        else//月不同  
        {
            if (month>q.month)//让pSmall指针指向小日期,pBig指针指向大日期  
            {
                pSmall = &q;
                pBig = this;
            }
            else
            {
                pSmall = this;
                pBig = &q;
            }
            temp.month = pSmall->month;
            temp.day = pSmall->day;
            for (i = 1; i <= 366; i++)//每次加一天,看看加多少次,两个日期相同,加的次数即为相差的天数  
            {//由于年份相同,相差的天数必然不会大于366,实际循环次数不大于366  
                temp + 1;//使用 运算符重载 +  
                if (temp.month == pBig->month && temp.day == pBig->day)
                {
                    differece = i;
                    break;
                }
            }
            differece = (month>q.month ? -differece : differece);
        }
    }
    else//年份不同  
    {
        if (year>q.year)//让pSmall指针指向小日期,pBig指针指向大日期  
        {
            pSmall = &q;
            pBig = this;
        }
        else
        {
            pSmall = this;
            pBig = &q;
        }
        temp.year = pSmall->year;
        temp.month = pSmall->month;
        temp.day = pSmall->day;
        m = pBig->year - pSmall->year;
        for (i = (m - 1) * 365; i <= (m + 1) * 366; i++)//每次加一天,看看加多少次,两个日期相同,加的次数即为相差的天数  
        {//计算出年份差m后,两个日期的天数差必定在[(m-1)*365,(m+1)*366]范围内,实际循环的次数不大于2*366  
            //temp.add(1);  
            temp += 1;
            if (temp.month == pBig->month && temp.day == pBig->day)
            {
                differece = i - (m - 1) * 365 + 1;
                break;
            }
        }
        differece = (year>q.year ? -differece : differece);
    }
    return differece;
}

ostream& operator<<(ostream& os, const my_date& q)
//void my_date::display()  
{
    os << q.year << "年" << q.month << "月" << q.day << "日";
    return os;
}

#endif  

My_Date.cpp

#include "My_Date.h"  

class date
{
    friend ostream& operator<<(ostream& os, const date &d);
    friend istream& operator>>(istream& in, date &d);
public:
    date(int year = 1900, int month = 1, int day = 1)
        :_year(year)
        , _month(month)
        , _day(day)
    {
        if (is_invalid_date())//无效的  
        {
            _year = 1900;
            _month = 1;
            _day = 1;
        }
    }
public:
    bool is_invalid_date()//判断日期是否无效  
    {
        if ((_year < 1900)
            || ((_month < 1) || (_month > 12))
            || ((_day < 1) || (_day > day_in_month())))
        {
            return true;
        }
        return false;
    }
public:
    int day_in_month()
    {
        int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        if (is_leap_year())
        {
            days[2] += 1;
        }
        return days[_month];
    }
public:
    bool is_leap_year()
    {
        if ((_year % 400 == 0) || ((_year % 100) != 0 && _year % 4 == 0))
        {
            return true;
        }
        return false;
    }
    date(const date& d)
        :_year(d._year)
        , _month(d._month)
        , _day(d._day)
    {
    }
    date& operator=(const date& d)
    {
        if (this != &d)
        {
            _year = d._year;
            _month = d._month;
            _day = d._day;
        }
        return *this;
    }
public:
    void to_correct_date()
    {
        //2000 11 -3  
        while (_day <= 0)
        {
            if (_month == 1)
            {
                _month = 12;
                _year -= 1;
            }
            else
            {
                _month -= 1;
            }
            _day += day_in_month();
        }
        while (_day > day_in_month())
        {
            _day -= day_in_month();
            if (_month == 12)
            {
                _month = 1;
                _year += 1;
            }
            else
            {
                _month += 1;
            }
        }
    }
    date operator+(int day)
    {
        date d(*this);
        d._day += day;
        //  
        d.to_correct_date();
        return d;
    }
    date& operator+=(int day)
    {
        _day += day;
        //  
        this->to_correct_date();
        return *this;
    }
    date operator-(int day)
    {
        date d(*this);
        d._day -= day;
        //  
        d.to_correct_date();
        return d;
    }
    date& operator-=(int day)
    {
        date d(*this);
        _day -= day;
        //  
        this->to_correct_date();
        return *this;
    }
    int operator-(const date &d)
    {
        int days = 0;
        date d1(d);
        date d2(*this);
        if (d1 > d2)//d2 记录大的日期  
        {
            d1 = (*this);
            d2 = d;
        }
        while (d1 != d2)
        {
            days++;
            d1 += 1;
        }
        return days;
    }
    bool operator>(const date& d)
    {
        if (_year > d._year)
            return true;
        if (_year == d._year)
        {
            if (_month > d._month)
                return true;
            if (_month == d._month)
            {
                if (_day > d._day)
                {
                    return true;
                }
            }
        }
        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 (_year == d._year && _month == d._month && _day == d._day);
    }
    bool operator<(const date& d)
    {
        return !(*this >= d);
    }
    bool operator<=(const date& d)
    {
        return (*this > d);
    }
private:
    int _year;
    int _month;
    int _day;
};
ostream& operator<<(ostream& os, const date& d)
{
    os << d._year << "-" << d._month << "-" << d._day;
    return os;
}
istream& operator>>(istream& in, date& d)
{
    in >> d._year >> d._month >> d._day;
    return in;
}
int main()
{
    date d(2016, 3, 5);
    cout << d << endl;
    d += 2;
    cout << "d+2:  " << d << endl;
    d += 31;
    cout << "d+31:  " << d << endl;
    d -= 61;
    cout << "d-61:  " << d << endl;
    cout << "==============" << endl;
    date d1(2016, 3, 5), d2(2016, 2, 1);
    cout << (d2 - d1) << endl;
    getchar();
    return 0;
}

Test.cpp

#include "My_Date.h"  

int main()
{
    my_date d(2016, 2, 24);
    cout << "原日期date1:" << endl;
    //d.display();  
    cout << d << endl;
    // d.add(60);  
    d += 60;
    cout << "date2=date1+60:";
    cout << d << endl;
    d -= 60;
    cout << "date3=date2-60:";
    cout << d << endl;
    // printf("2016-2-24与2017-2-24日相差%d天\n",d.diff(my_date(2017,2,24)));  
    cout << "2016-2-24与2017-2-24日相差" << (d - my_date(2017, 2, 24)) << "天" << endl;

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.编写类MyDate.java 2.该类有如下构造方法 2.1 无参数构造方法public MyDate(),以当前的系统时间构造MyDate对象 2.2 public MyDate(int year, int month, int day), 以指定的年月日构造MyDate对象 3.该类有如下属性 3.1 private int year ;//年 3.2 private int month; //月 3.3 private int day; //日 4.该类有如下方法 4.1 public String after(int day); //返回当前对象代表的日期之后day天的日期,比如当前对象是2008-8-8,调用after(5)以后,应该返回2008-8-13,格式可自定义 4.2 public String before(int day); //返回当前对象代表的日期之前day天的日期,比如当前对象是2008-8-8,调用before(5)以后,应该返回2008-8-3,格式可自定义 4.3 public void setYear(int year); //设置年为指定值year 4.4 public void setMonth(int month); //设置月为指定值month 4.5 public void setDay(int day); //设置日为指定值day 4.6 public int getYear(); //返回当前对象的年 4.7 public int getMonth(); //返回当前对象的月 4.8 public int getDay(); //返回当前对象的日 4.9 public void set (int year, int month, int day); //设置年、月、日为指定的值year、month、day 4.10 public String toString();//以字符串形式返回当前对象的年月日,例如2008年08月08日,格式可自定义 4.11 public boolean equals(MyDate mydate);//当前对象与另一个对象比较,如果两个对象的年月日均相等,则返回true,反之返回false 5.编写TestMyDate.java,在main方法对MyDate类的各个方法进行测试 6.按照编程规范为MyDate.java编写规范的代码 7.按照java doc API的要求,对代码编写规范的注释,然后利用javadoc.exe命令生成MyDate.java的API doc 8.撰写上机报告

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值