日期加N天的实现

#include <iostream>
using namespace std;
class CDate
{
private:
    int year;
    int month;
    int day;
public:
     int IsLeapYear(int y)
     {
         if((year%400==0)||(year%100!=0&&year%4==0))
            return 1;
         else
            return 0;
     }
     int DaysOfMonth(int y)
     {
         if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
            return 31;
         else if(month==4||month==6||month==9||month==11)
            return 30;
         else if(month==2)
         {
             if(IsLeapYear(y))
                return 29;
             else
                return 28;
         }
     }
     CDate()
     {
         year=2015;
         month=5;
         day=7;
         cout<<"Default constructor is called!"<<endl;
     }
     CDate(int y,int m,int d)
     {
         year=y;
         month=m;
         day=d;
         cout<<"Constructor is called!"<<endl;
     }
      void SetDate(int y,int m,int d)
      {
          year=y;
          month=m;
          day=d;
          cout<<"Date is reset!"<<endl;
      }
      int GetYear(){return year;}
      int GetMonth(){return month;}
      int GetDay(){return day;}
      void Show()
      {
           cout<<year<<'-'<<month<<'-'<<day<<endl;
      }
      CDate IncNDay(int n)
      {
          CDate d=*this;
          int days=365;
          if(d.IsLeapYear(d.year))
            days=366;
          while(n/days)
          {
              n-=days;
              d.year++;
              days=365;
              if(d.IsLeapYear(d.year))
                days=366;
          }
          while(n/(d.DaysOfMonth(d.year)))
          {
              n-=d.DaysOfMonth(d.year);
              d.month++;
              if(d.GetMonth()>12)
              {
                  d.year++;
              }
              d.month%=12;
          }
          if((n+d.day)<=d.DaysOfMonth(d.year))
            d.day+=n;
          else
          {
              d.day=n+d.day-d.DaysOfMonth(d.year);
              d.month++;
          }
          if(d.GetMonth()>12)
          {
              d.year++;
              d.month%=12;
          }
          return d;
      }
};
int main()
{
    CDate d1;
    d1.Show();
    d1.SetDate(2015,2,28);
    d1.IncNDay(200).Show();
    d1.Show();
    CDate d2(2016,12,31);
    d2.Show();
    d2=d1.IncNDay(1000);
    d1.Show();
    d2.Show();
    d1.SetDate(d2.GetYear(),d2.GetMonth(),d2.GetDay());
    d1.Show();
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 C 语言实现日期减的示例代码: ```c #include <stdio.h> #include <stdlib.h> struct Date { int year; int month; int day; }; // 计算一个月有多少天 int days_in_month(int year, int month) { switch (month) { case 2: if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { return 29; } else { return 28; } case 4: case 6: case 9: case 11: return 30; default: return 31; } } // 计算日期上指定天数后的日期 struct Date add_days(struct Date date, int days) { while (days > 0) { int days_in_month = days_in_month(date.year, date.month); if (date.day + days > days_in_month) { days -= days_in_month - date.day + 1; date.day = 1; if (date.month == 12) { date.year++; date.month = 1; } else { date.month++; } } else { date.day += days; break; } } return date; } // 计算日期减去指定天数后的日期 struct Date sub_days(struct Date date, int days) { while (days > 0) { if (date.day - days < 1) { days -= date.day - 1; if (date.month == 1) { date.year--; date.month = 12; } else { date.month--; } date.day = days_in_month(date.year, date.month); } else { date.day -= days; break; } } return date; } int main() { struct Date date = {2021, 3, 15}; printf("原始日期:%d年%d月%d日\n", date.year, date.month, date.day); struct Date new_date = add_days(date, 10); printf("上10天后的日期:%d年%d月%d日\n", new_date.year, new_date.month, new_date.day); new_date = sub_days(date, 10); printf("减去10天后的日期:%d年%d月%d日\n", new_date.year, new_date.month, new_date.day); return 0; } ``` 在这个示例代码中,我们定义了一个 `struct Date` 结构体来表示日期,其中包含年、月、日三个字段。 我们使用 `days_in_month` 函数来计算一个月有多少天。这个函数会根据年份和月份来判断每个月有多少天,考虑闰年和平年的情况。 我们使用 `add_days` 函数来计算日期上指定天数后的日期。这个函数会循环遍历每一天,直到指定天数减为 0,每次循环都会判断当前月份是否已经超过了这个月的最大天数,如果超过了,就将月份 1,将日期重置为 1。如果月份已经是 12 月了,就将年份 1,月份重置为 1。 我们使用 `sub_days` 函数来计算日期减去指定天数后的日期。这个函数的实现和 `add_days` 函数类似,只是每次循环都会判断当前日期是否已经小于 1,如果小于 1,就将月份减 1,将日期重置为上个月的最大天数。如果月份已经是 1 月了,就将年份减 1,月份重置为 12。 在 `main` 函数中,我们定义了一个初始日期,然后分别调用 `add_days` 和 `sub_days` 函数来计算上和减去指定天数后的日期,并输出结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值