完美版nextday程序。可以根据平年闰年智能算出明天日期

 

#include <iostream.h>
class Date
{
private:
 int year,month,day;
public:
 Date(){}
 Date(int y,int m,int d);
 void display()
 {
  cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
 }
 int isLeapyear(int y);
 void nextday();
};
void Date::nextday()
{

   switch(month)
   {
   case 1:
   case 3:
   case 5:
   case 7:
   case 8:
   case 10:
   case 12:
     if((month==12)&&(day==31))
     {
    cout<<"当前日期为"<<day<<"年"<<month<<"月"<<day<<"日"<<endl;
    cout<<endl;
    cout<<"明天日期为:"<<year+1<<"年"<<1<<"月"<<1<<"日"<<endl;
    cout<<endl;
     }

 
      else if(day==31)
   {
   cout<<"当前日期为"<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
   cout<<endl;
   cout<<"明天日期为:"<<year<<"年"<<month+1<<"月"<<1<<"日"<<endl;
   cout<<endl;
   }
  
      else
   {
   cout<<"当前日期为"<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
   cout<<endl;
   cout<<"明天日期为:"<<year<<"年"<<month<<"月"<<day+1<<"日"<<endl;
   cout<<endl;
   }
   break;
 
 case 2:
   if(isLeapyear(year))
  {
    
   if((month==12)&&(day==29))
  {
    cout<<"当前日期为"<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
    cout<<endl;
    cout<<"明天日期为:"<<year+1<<"年"<<1<<"月"<<1<<"日"<<endl;
    cout<<endl;
  }
  else if(day==29)
  {
   cout<<"当前日期为"<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
   cout<<endl;
   cout<<"明天日期为:"<<year<<"年"<<month<<"月"<<1<<"日"<<endl;
   cout<<endl;
  }
  
  else
  {
   cout<<"当前日期为"<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
   cout<<endl;
   cout<<"明天日期为:"<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
   cout<<endl;
  }
  }
  else
  {
   if((month==12)&&(day==28))
  {
    cout<<"当前日期为"<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
    cout<<endl;
    cout<<"明天日期为:"<<year+1<<"年"<<1<<"月"<<1<<"日"<<endl;
    cout<<endl;
  }
  else if(day==28)
  {
   cout<<"当前日期为"<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
   cout<<endl;
   cout<<"明天日期为:"<<year<<"年"<<month+1<<"月"<<1<<"日"<<endl;
   cout<<endl;
  }
  
  else
  {
   cout<<"当前日期为"<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
   cout<<endl;
   cout<<"明天日期为:"<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
   cout<<endl;
  }
  }
  break;
 case4:
 case6:
 case9:
 case11:

   if((month==12)&&(day==30))
  {
    cout<<"当前日期为"<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
    cout<<endl;
    cout<<"明天日期为:"<<year+1<<"年"<<1<<"月"<<1<<"日"<<endl;
    cout<<endl;
  }
  else if(day==30)
  {
   cout<<"当前日期为"<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
   cout<<endl;
   cout<<"明天日期为:"<<year<<"年"<<month+1<<"月"<<1<<"日"<<endl;
   cout<<endl;
  }
  
  else
  {
   cout<<"当前日期为"<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
   cout<<endl;
   cout<<"明天日期为:"<<year<<"年"<<month<<"月"<<day+1<<"日"<<endl;
   cout<<endl;
  }
  break;


   }
}


int Date::isLeapyear(int y)
{
 if(((y%4==0)&&(y%100!=100))||(y%400==0)) return 1;
 else return 0;
}
Date::Date(int y,int m,int d)
{
 if((y>=1900)&&(y<=2050))
 {
  year=y;
  if((m>=1)&&(m<=12))
  {
   month=m;
   switch(month)
   {
   case 1:
   case 3:
   case 5:
   case 7:
   case 8:
   case 10:
   case 12:
    if((d>=1)&&(d<=31))
    {
     day=d;
    }
    else
    {
     cout<<"day不合理"<<endl;
    }
    break;
   case 4:
   case 6:
   case 9:
   case 11:
    if((d>=1)&&(d<=30))
    {
     day=d;
    }
    else
    {
     cout<<"day不合理"<<endl;
    }
    break;
   case 2:
    if(isLeapyear(year))
    {
     if((d>=1)&&(d<=29))
     {
      day=d;
     }
     else
     {
      cout<<"day不合理"<<endl;
     }
    }
    else
    {
     if((d>=1)&&(d<=28))
     {
      day=d;
     }
     else
     {
      cout<<"day不合理"<<endl;
     }
    }

   }
  }
  else
  {
   cout<<"month不合理"<<endl;
  }
 }
 else
 {
  cout<<"year不合理"<<endl;
 }
}
void main()
{
 Date d1(2010,2,28);
 d1.nextday();
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值