C++日期类的简单实现

日期类主要功能

        给定任意日期可以对该日期进行加或减上响相应的天数后得到新的日期
     或者给定你两个日期算出两个日期之间相差的天数

代码实现:


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


class Date 
{ 
public: 
  Date(int year = 1900, int month = 1, int day = 1) {//构造
    _year=year;
    _month=month;
    _day=day;
    int days=GetMonthDays(year,month);
    if(day==-1||day<1||day>days){
      cout<<"日期不合法"<<endl;
      assert(false);
    }
  }
// 如何检查一个日期是否合法
Date(const Date &d)//拷贝构造函数当作是构造函数的重载
{
  _year=d._year;
  _month=d._month;
  _day=d._month;
}

~Date(){//析构

}
//打印日期
void display(){
  cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
}
//d1 > d2 
bool operator>(const Date& d){

  if(_year>d._year){
    return true;
  }else if(_year==d._year&&_month>d._month){
    return true;
  }else if(_year>d._year&&_month>d._month&&_day>d._day){
    return true;
  }else{
    return false;
  }
}
//operator>=
bool operator>=(const Date& d){
  return (*this>d)||(*this==d);
}
//operator< 
bool operator<(const Date & d){
  return !(*this>=d);
}
//operator<= 
bool operator<=(const Date& d){
  return !(*this>d);
}
//operator==
bool operator==(const Date& d){
  return (_year=d._year&&_month==d._month&&_day==d._day);
}
//operator!= 
bool operator!=(const Date& d){
  return (_year!=d._year||_month!=d._month||_day!=d._day);
}
//d1 + 100 
Date operator=(const Date& d){ //复制运算符的重载

  if(this!= &d){
     this->_year=d._year;
     this->_month=d._month;
     this->_day=d._day;
  }
  return *this;
}
Date operator+(int day){//计算加上几天后的日期

Date tmp(*this);  
    tmp += day;  
    return tmp; 
} 
Date operator+=(int day){
  //*this=operator+(day);
  //return *this 

 _day += day;  
    while (_day > GetMonthDays(_year,_month))  
    {  
        _day -= GetMonthDays(_year,_month);  
        _month++;  
        if (_month > 12)  
        {  
            _month = 1;  
            _year += 1;  
        }  
    }  
    return *this;  
}
Date operator-(int day){//减上几天后的日期

Date tmp(*this);  
    tmp -= day;  
    return tmp;
} 
Date operator-=(int day){
  //*this=operator-(day);
  //return *this;
_day -= day;  
    while (_day < 1)  
    {  
        --_month;  
        if (_month < 1)  
        {  
            --_year;  
            _month = 12;  
        }  
        _day += GetMonthDays(_year,_month);  
    }  
    return *this;  
} 
Date operator++(){//前置++
  _day+=1;
  if(_day>GetMonthDays(_year,_month)){
    if(_month==12){
      ++_year;
      _month=1;
    }else{
      ++_month;
    }
     _day=1;
  }
  return *this;
} 
Date operator++(int){

  Date tmp;
  *this=operator++();
  return tmp;
}//后置++ 
Date operator--(){

  _day-=1;
  if(_day<1){
    --_month;
    if(_month<1){
      --_year;
      _month=12;
    }
    _day=GetMonthDays(_year,_month);
  }
  return *this;
} 
Date operator--(int){
  Date tmp;
  *this=operator--();
  return tmp;
} 
int operator-( Date& d){
  //计算两个日期间相差的天数
  if(_year<d._year){
    Date tmp=*this;
    *this=d;
    d=tmp;
  }else if(_year==d._year&&_month<d._month){
    Date tmp=*this;
    *this=d;
    d=tmp;
  }else if(_year==d._year&&_month==d._month&&_day<d._day){

    Date tmp=*this;
    *this=d;
    d=tmp;
  }
  Date tmp1(*this);
  Date tmp2(d);
  int ret=0;
    while(!(tmp1==tmp2)){
      tmp2.operator++();
      ret++;
    }

    return ret;
}

bool leapyear(int year){ //判断是否为闰年

  if(((year%4==0) && (year%100!=0))|| (year%400==0)){
    return true;
  }
    return false;

}
// if /switch/array 
int GetMonthDays(int year, int month){     //根据已知年月获得该日期的天数

  int monthdays[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
  int days=monthdays[month];

  if(month==2&&leapyear(year)){
    days+=1;
  }
  return days;

}
private: 
int _year; 
int _month; 
int _day; 
};


int main(){

  Date d1(2018,6,5);
  Date d2(2018,6,6);
  d1.display();
  d2.display();
  Date ret=d1-3;
  ret.display();
 //Date ret=d1+30;
 //ret.display();
  //Date rey=d2+60;
 // rey.display();
 // Date reu=d1-30;
 // reu.display();
 //Date rei=d2-60;
 // rei.display();

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C 语言中,由于没有对象的概念,因此无法像面向对象语言一样使用内嵌子对象。不过,我们可以通过结构体来模拟对象的概念,从而实现似内嵌子对象的效果。 以日期类为例,我们可以定义一个结构体来表示日期,然后在日期类中嵌入这个结构体作为其成员变量,从而实现日期类的功能。 以下是一个简单的 C 日期类实现: ``` typedef struct { int year; int month; int day; } Date; typedef struct { Date date; int (*getYear)(const Date *); int (*getMonth)(const Date *); int (*getDay)(const Date *); } DateClass; int getYear(const Date *date) { return date->year; } int getMonth(const Date *date) { return date->month; } int getDay(const Date *date) { return date->day; } DateClass Date_new(int year, int month, int day) { DateClass dateClass = { { year, month, day }, getYear, getMonth, getDay }; return dateClass; } ``` 在上面的代码中,我们首先定义了一个日期结构体 `Date`,表示一个具体的日期。然后,我们定义了一个日期类 `DateClass`,其中包含一个 `Date` 型的成员变量 `date`,以及三个函数指针,用于获取日期的年、月、日信息。 接着,我们实现了三个函数 `getYear`、`getMonth`、`getDay`,分别用于获取日期的年、月、日信息。最后,我们定义了一个 `Date_new` 函数,用于创建一个新的日期对象,并返回一个 `DateClass` 型的结构体,该结构体包含了日期对象的所有信息和操作。 使用这个日期类时,我们可以先通过 `Date_new` 函数创建一个日期对象,然后使用该对象的方法获取日期的信息,如下所示: ``` DateClass date = Date_new(2021, 10, 1); printf("Year: %d\n", date.getYear(&date.date)); printf("Month: %d\n", date.getMonth(&date.date)); printf("Day: %d\n", date.getDay(&date.date)); ``` 输出结果为: ``` Year: 2021 Month: 10 Day: 1 ``` 可以看到,我们通过 `Date_new` 函数创建了一个日期对象 `date`,然后使用该对象的方法获取了日期的年、月、日信息,并将其输出到控制台上。这就是一个简单的 C 日期类实现,其中使用了结构体来模拟对象,并实现似内嵌子对象的功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值