| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3144 人关注过本帖, 3 人收藏
标题:Date类
只看楼主 加入收藏
PcrazyC
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:5652
专家分:0
注 册:2006-10-20
结帖率:100%
收藏(3)
 问题点数:0 回复次数:13 
Date类
今天写了一个DATE类,发上来共享一下,由于时间比较紧,所以随便调试了一下,如果有错误,请指正,可能过两天再更新一下

//头文件(date.h)

程序代码:
class Date
{
public:
    Date();
    Date(int y,int m,int d);
    Date(const Date& dt);
    Date& add_year(int n);
    Date& add_month(int n);
    Date& add_day(int n);
    bool leapyear(int n);
    Date operator+(int n);
    Date operator-(int n);
    Date& operator++();
    Date& operator--();
    Date& operator+=(int n);
    Date& operator-=(int n);
    bool operator==(Date dt) const;
    bool operator!=(Date dt) const;
    bool operator<(Date dt) const;
    bool operator>(Date dt) const;
    int operator-(Date dt);
    int get_week();
    static void set_today(int y,int m,int d);
    void print();

private:
    int y,m,d;
    week w;
    static Date default_date;
};


//CPP文件:
程序代码:
#include"date.h"
#include<iostream>

using namespace std;

#define CASE break;case
typedef enum wk{sun=0,mon,tue,wed,thu,fri,sat}week;

const int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

Date Date::default_date=Date(2008,3,16);
void Date::set_today(int y,int m,int d)
{
    default_date.y=y;
    default_date.m=m;
    default_date.d=d;
}

Date::Date()
{
    *this=default_date;
}

Date::Date(int y,int m,int d)
{
    this->y=y;
    this->m=m;
    this->d=d;
}

Date::Date(const Date &dt)
{
    y=dt.y;
    m=dt.m;
    d=dt.d;
}

Date& Date::add_day(int n)
{
    d+=n;
    int day=m==2&&leapyear(y)?month[m]+1:month[m];
    while(d>day)
    {
        m++;
        d-=day;
        day=m==2&&leapyear(y)?month[m]+1:month[m];
    }
    if(m>12)
    {
        y+=m/12;
        m=m%12;
    }
    return *this;
}

Date& Date::add_month(int n)
{
    m+=n;
    if(m>12)
    {
        y+=m/12;
        m=m%12;
    }
    int day=m==2&&leapyear(y)?month[m]+1:month[m];
    if(d>day)
    {
        m++;
        d-=day;
    }
    return *this;
}

Date& Date::add_year(int n)
{
    if(leapyear(y)&&!leapyear(y+n)&&m==2&&d==29)
    {
        m=3;
        d=1;
    }
    y+=n;
    return *this;
}

bool Date::operator==(Date dt) const
{
    if(y==dt.y&&m==dt.y&&d==dt.d)
        return 1;
    return 0;
}
bool Date::operator !=(Date dt) const
{
    if(*this==dt)
        return 0;
    return 1;
}

bool Date::operator<(Date dt) const
{
    if(y!=dt.y)
        return y<dt.y;
    if(m!=dt.m)
        return m<dt.m;
    return d<dt.d;
}

bool Date::operator>(Date dt) const
{
    if(y!=dt.y)
        return y>dt.y;
    if(m!=dt.m)
        return m>dt.m;
    return d>dt.d;
}

Date Date::operator+(int n)
{
    this->add_day(n);
    return *this;
}

Date Date::operator-(int n)
{
    while(n)
    {
        int day=(m-1)==2&&leapyear(y)?month[m-1?m-1:12]+1:month[m-1?m-1:12];
        if(n>day)
        {
            d-=day;
            n-=day;
            m--;
        }
        else
        {
            if(n>=d)
            {
                d=day+d-n;
                m--;
            }
            else
                d-=n;

        }
        if(m<0)
        {
            m=12;
            y--;
        }
    }
    return *this;
}

int Date::operator -(Date dt)
{
    int sum=0,day;
    while(y>dt.y+1)
    {
        sum+=leapyear(dt.y)?366:365;
        dt.y++;
    }
    if(y>dt.y&&(m>dt.m||m==dt.m&&d>=dt.d))
    {
        sum+=leapyear(dt.y)?366:365;
        dt.y++;
    }
    while(m>dt.m)
    {
        day=dt.m==2&&leapyear(dt.y)?month[dt.m]+1:month[dt.m];
        sum+=day;
        dt.m++;
    }
    sum+=d-dt.d;
    return sum;

}

Date& Date::operator+=(int n)
{
    *this=*this+n;
    return *this;
}

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

Date& Date::operator++()
{
    *this+=1;
    return *this;
}

Date& Date::operator--()
{
    *this-=1;
    return *this;
}

bool Date::leapyear(int n)
{
    if(n%4)
        return 0;
    if(n%100==0)
        if(n%400)
            return 0;
    return 1;
}

void Date::print()
{
    cout<<y<<"."<<m<<"."<<d<<"\t";
    w=(week)(get_week()%7);
    switch(w)
    {
    case 0:
        cout<<"sunday";
    CASE(1):
        cout<<"monday";
    CASE(2):
        cout<<"tuesday";
    CASE(3):
        cout<<"wednesday";
    CASE(4):
        cout<<"thursday";
    CASE(5):
        cout<<"friday";
    CASE(6):
        cout<<"saturday";
        break;
    default:
        cout<<"the week is wrong";
    }
    cout<<endl;
}

int Date::get_week()
{
    return *this>default_date?(*this-default_date)%7:7-(default_date-*this)%7;
}
搜索更多相关主题的帖子: Date 
2008-03-16 22:50
小烟
Rank: 1
等 级:新手上路
帖 子:29
专家分:0
注 册:2008-3-18
收藏
得分:0 
牛人,看不懂也来坐哈
2008-03-18 01:19
zabbey
Rank: 1
等 级:新手上路
帖 子:55
专家分:0
注 册:2007-10-10
收藏
得分:0 
LZ可以试着把农历加上,那就有点难度了。

2008-03-18 10:21
PcrazyC
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:5652
专家分:0
注 册:2006-10-20
收藏
得分:0 
[bo]以下是引用 [un]zabbey[/un] 在 2008-3-18 10:21 的发言:[/bo]

LZ可以试着把农历加上,那就有点难度了。

这个我考虑过,可能过两天再加,还可能加一些新的东西

雁无留踪之意,水无取影之心
2008-03-18 10:27
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
呵呵..不错..帮你顶了..我过几天也弄个原创..要不现在精华还是0呢..愚昧...

学习需要安静。。海盗要重新来过。。
2008-03-18 15:41
PcrazyC
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:5652
专家分:0
注 册:2006-10-20
收藏
得分:0 
我精华帖本来就有四个,这个好像不算在内吧

雁无留踪之意,水无取影之心
2008-03-18 19:46
zdyiam
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2008-1-26
收藏
得分:0 
ddddddd
2008-03-19 08:45
VanHorn
Rank: 1
等 级:新手上路
帖 子:152
专家分:0
注 册:2008-1-8
收藏
得分:0 
日期类啊。挺好的。楼主,我觉得应该再加一个获得系统日期的方法,用来获得当前时间,这样更有意义。
2008-03-19 10:22
PcrazyC
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:5652
专家分:0
注 册:2006-10-20
收藏
得分:0 
这个只是我自己刚学C++随便写的,后面的继承之类的我都还没看,如果真的要设计一个日期类的话,我不可能设计得这么简单,这只是看了前两章的一个练习而已,等我看了后面的再完整的设计一个日期类出来

雁无留踪之意,水无取影之心
2008-03-19 11:45
荒野的雄狮
Rank: 1
来 自:地球
等 级:新手上路
威 望:1
帖 子:300
专家分:0
注 册:2008-9-2
收藏
得分:0 
挺佩服你的,我c++看了不少,可很少动手自己写东西。顶顶……
2008-10-24 21:46
快速回复:Date类
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016142 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved