自己实现的日期类

data.h 开始

#ifndef DATE_H
#define DATE_H

// No description
class Date
{
 public:
  // class constructor
  Date(); //默认日期是 2001年1月1日
  Date(int, int, int); //定义日期 年 月 日
  
  Date& operator=(const Date&);// 日期的赋值操作
  Date operator+(int);// 返回数天之后的日期
  Date operator-(int);// 返回数天之前的日期
  
  bool operator>(const Date&) const;// 判断当天是否在给定日期之后
  bool operator<(const Date&) const;// 判断当天是否在给定日期之前
  bool operator<=(const Date&) const;// 判断当天是否在给定日期之后或等于
  bool operator>=(const Date&) const;// 判断当天是否在给定日期之前或等于
  bool operator==(const Date&) const;// 判断当天是否同给定日期相等
  bool operator!=(const Date&) const;// 判断当天是否在给定日期不相等
  
  bool isLeap() const;// 判断当年是否是闰年
  int weekDay() const;// 返回今天是星期几
  int monthDays() const;// 返回本月的天数
  int weekNum() const;// 返回本周是本年的周数
  int getSpace(const Date&) const;// 返回当天与给定日期之间相隔的天数
  
  int getYear() const;// 返回 年
  int getMonth() const;// 返回 月
  int getDay() const;// 返回 日
  void print() const;// 打印当日相关信息
  
 private:
        int _iYear;
        int _iMonth;
        int _iDay;
       
        void init(int, int, int);// 初始化操作
        int dTon() const;// 将日期转换为常量,定义1901年1月1日为 常量 1
        Date nTod(int);// 将日期常量转换为日期类型
       
        bool isLeapYear(int) const;// 判断给定年份是闰年
       
        static const int DAY_TABLE[2][12];
};
#endif // DATE_H

 data.cpp 开始

#include "date.h" // class's header file
#include <iostream>
#include <math.h>

using namespace std;

const int Date::DAY_TABLE[2][12]={
                      {31,28,31,30,31,30,31,31,30,31,30,31},
                      {31,29,31,30,31,30,31,31,30,31,30,31}
                      };

// class constructor
Date::Date()
{
 init(2001,1,1);
}

Date::Date(int iYear, int iMonth, int iDay)
{
    init(iYear, iMonth, iDay);
}

Date& Date::operator=(const Date& date)
{
    _iYear=date.getYear();
    _iMonth=date.getMonth();
    _iDay=date.getDay();
     
    return *this;
}

Date Date::operator+(int iNumber)
{
      int d=dTon();
      d+=iNumber;
     
      return nTod(d);
}

Date Date::operator-(int iNumber)
{
      int d=dTon();
      d-=iNumber;
     
      return nTod(d);
}

bool Date::operator>(const Date& date) const
{
     return dTon()>date.dTon();
}

bool Date::operator>=(const Date& date) const
{
     return dTon()>=date.dTon();
}

bool Date::operator==(const Date& date) const
{
     return dTon()==date.dTon();
}

bool Date::operator!=(const Date& date) const
{
     return dTon()!=date.dTon();
}

bool Date::operator<=(const Date& date) const
{
     return dTon()<=date.dTon();
}

bool Date::operator<(const Date& date) const
{
     return dTon()<date.dTon();
}

bool Date::isLeap() const
{
     return isLeapYear(getYear());
}

bool Date::isLeapYear(int year) const
{
       return year%4==0 ? (year%100 ==0? (year%400==0? true: false) : true) : false;
}

int Date::weekDay() const
{
    int iSpace= getSpace(Date(2001,1,1));
   
    int iResult=1;

    if(iSpace >=0)
    {
        iResult=iSpace%7+1;
    }
    else
    {
        iResult=7-(((-iSpace)-1)%7);
    }
   
    return iResult;
}

int Date::monthDays() const
{
    return isLeap()?DAY_TABLE[1][getMonth()-1]:DAY_TABLE[0][getMonth()-1];
}

int Date::weekNum() const
{
    Date first(_iYear,1,1);
    int iResult=0;
    int iSpaceDay=0;
    if(first.weekDay()==1)
    {
        iSpaceDay=getSpace(first);
        iResult=floor(iSpaceDay/7)+1;
    }
    else
    {
        int temp=9-first.weekDay();
        iSpaceDay=getSpace(Date(_iYear,1,temp));
        iResult=floor(iSpaceDay/7)+2;
        if(iSpaceDay<0) iResult=1;
    }

    return iResult;
}

int Date::getSpace(const Date& date) const
{
    return dTon()-date.dTon();
}

int Date::getDay() const
{
    return _iDay;       
}
              
int Date::getMonth() const
{
    return _iMonth;   
}

int Date::getYear() const
{
    return _iYear;       
}

void Date::print() const
{
     cout<<"[Year="<<_iYear<<", Month="<<_iMonth<<", Day="<<_iDay<<", weekDay()="<<weekDay()<<"]"<<endl;
}

void Date::init(int iYear, int iMonth, int iDay)
{
    _iYear=iYear;
    _iMonth=iMonth;
    _iDay=iDay;
}

int Date::dTon() const
{
    int iLeapYearTimes=0;
     for(int i=1901;i<getYear();i++)
     {
         if(isLeapYear(i)) iLeapYearTimes++;
     }
    
     int iResult=(getYear()-1901)*365+iLeapYearTimes;
    
     int iLeap=0;
     if(isLeap()) iLeap=1;
    
     for(int i=0;i<getMonth()-1;i++) iResult+=DAY_TABLE[iLeap][i];
    
     iResult+=getDay();
    
     return iResult;
               
}

Date Date::nTod(int num)
{
      int iYear=1901,
          iMonth=1,
          iDay=1;
     
      int days=num;
     
      while(days!=0)
      {
          int flag=0;
         
          if(isLeapYear(iYear))
          {
              if(days-366<=0) flag=1;
              else
              {
                  days-=366; 
                  iYear++;
                  continue;
              }
          }
          else
          {
              if(days-365<=0) flag=0;
              else
              {
                  days-=365;
                  iYear++;
                  continue;
              }             
          }
         
          while(days>0)
          {
              days-=DAY_TABLE[flag][iMonth-1];
              iMonth++;
          }
          iMonth--;
          iDay=days+DAY_TABLE[flag][iMonth-1];
          break;
      }
     
      return Date(iYear, iMonth, iDay);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值