跨年倒计时
请修改编辑器中的代码,完成以下功能:程序中定义了一个 Date 类,该类用于表示日期。你需要修正程序中的错误,使得后缀++运算符能够正确地增加日期。具体来说,当日期为2020年12月31日时,使用后缀++运算符后,日期应变为2021年1月1日。程序不需要读取输入,直接在代码中初始化日期对象并输出结果。 注意:只能修改每个//ERROR **********found**********下的那一行,不要改动程序中的其他内容。 输出示例: 今天是:2020年12月31日 明天是:2021年1月1日
#include <iostream>
using namespace std;
class Date //日期类
{
public:
Date(int y=2021,int m=1,int d=1):year(y),month(m),day(d){ } //构造函数
int isLeap() const; //判断是否是闰年
//ERROR**********found**********
Date operator++(); //重载后缀++运算符
friend ostream& operator<<(ostream& os,const Date& d); //重载<<运算符
private:
int year,month,day;
};
//ERROR**********found**********
int Date::isLeap()
{
if((year%4==0&&year%100!=0)||(year%400==0))
return 1; //闰年
return 0; //非闰年
}
Date Date::operator++(int)
{
//利用数组存储一年12月的天数,若为闰年则将2月天数加1
int month_days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeap())
month_days[1]++;
//ERROR**********found**********
if(day==month_days[month]) //是该月最后一天
{
if(month==12)
{
year++;
month=1;
day=1;
}
else
{
month++;
day=1;
}
}
else
day++; //不是该月最后一天
return *this;
}
ostream& operator<<(ostream& os, const Date& d)
{
os<<d.year<<"年"<<d.month<<"月"<<d.day<<"日"<<endl;
return os;
}
int main()
{
Date d(2020,12,31);
cout<<"今天是:"<<d;
d++;
cout<<"明天是:"<<d;
return 0;
}控制台
调试终端
