C++ class Date

<pre name="code" class="cpp">#include<iostream>
#include<windows.h>
using namespace std;
static int data[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

class Date
{
    friend int count(const Date &);//有元函数,计算该日期是该年的第几天
    friend bool leap_year(int);//判断是否为闰年
    friend bool operator<(Date &,Date &);//重载<
    friend bool operator>(Date &,Date &);//重载>
    friend bool operator==(Date &,Date &);//重载==
public:
    Date();//默认构造函数
    void set(int,int,int);//设置日期
    Date& operator-(int);//一个日期减去天数
    Date& operator+(int);//一个日期加上天数
    int sum();//从0天开始算起,该日期一共多少天
    void display();//显示日期
private:
    int year,month,day;//定义私有变量
};

Date::Date()
{
    year=month=day=0;
}

bool leap_year(int y)
{
    if(y%400==0||(y%4==0&&y%100!=0))
    {
        data[2]=29;
        return true;
    }
    return false;
}
void Date::set(int y,int m,int d)
{
    year=y>0?y:0;
    month=(m>0&&m<=12)?m:0;
    leap_year(y);
    day=(d>0&&d<=data[m])?d:0;
}

int Date::sum()
{
    int sum;
    if(leap_year(year))
       sum=(year-1)*366;
    sum=(year-1)*365;
    for(int i=1;i<month;i++)
       sum+=data[i];
    sum+=day;
    return sum;
}

int count(const Date &a)
{
    int count=0;
    leap_year(a.year);
    for(int i=0;i<a.month;i++)
      count+=data[i];
    count+=a.day;
    return count;
}

bool operator<(Date &a,Date &b)
{
    return a.sum()<b.sum();
}

bool operator>(Date &a,Date &b)
{
    return a.sum()>b.sum();
}

bool operator==(Date &a,Date &b)
{
    return a.sum()==b.sum();
}

Date& Date::operator+(int x)
{
    int flag=0;
    if(x==0)
      return *this;
    leap_year(year);
    if((day+x)>data[month])
    {
        month++;
        flag++;
        if(month>12)
        {
            year++;
            month=1;
        }
    }
    if(flag==0)
       day+=x;
    else
       day=x;
    return *this;
}

Date& Date::operator-(int x)
{
    int flag=0;
    if(x==0)
      return *this;
    leap_year(year);
    if(day<=x)
    {
        month--;
        flag++;
        if(month<1)
        {
            year--;
            month=12;
        }
    }
    if(flag==0)
      day-=x;
    day=data[month]+1-x;
    return *this;
}

int operator-(Date &a,Date &b)
{
    int t;
    if(a==b)
      t=a.sum()-b.sum();
    else if(a<b)
      t=b.sum()-a.sum();
    else
      t=a.sum()-b.sum();
}

void Date::display()
{
    cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
}

int main()
{
    Date a,b;
    cout<<"set date"<<endl;
    a.set(2000,2,29);
    b.set(2000,2,1);
    cout<<"data a:"<<endl;
    a.display();
    cout<<"data b:"<<endl;
    b.display();
    cout<<"从0开始该日期共多少天"<<endl;
    cout<<count(a);
    cout<<endl;
    cout<<"date a>date b"<<endl;
    cout<<(a>b)<<endl;
    cout<<"date a<date b"<<endl;
    cout<<(a<b)<<endl;
    cout<<"date a==date b"<<endl;
    cout<<(a==b);
}


 
 

结果如下:


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值