要求
1.定义日期类型myDate。要求如下:
(1)无参构造函数时,设置的日期为1900-1-1。
(2)要求自定义构造函数,实现日期的设置;
设置日期时,应该进行合法性检查。如2月29日,则发生在闰年,否则报错;每一个月,也不能超过31天,否则也报错;如果年份小于1900或超过3000,则报错。
(3)可以重新设置日期;合法性检查同上。
(4)可以单独修改年或月或日(同样进行合法性检查),要求实现这些功能的成员函数;
(5)要求对当前日期对象有增加N天或减少N天的操作;
减法时,如果当月的天数不够时,则向上一个月借(或年);加法时,超过当月的天数,则修改上个月份。
(6)所有的年月日修改,都要进行合法性检查,如数据无效时要求保留原数据,且给出不成功的提示;如果数据合法性正确,则修改数据;
(7)日期的输出格式为“XXXX年XX月XX日”。
2.类的声明,定义在头文件myDate.h中
class myDate
{
private:
int year;
int month;
int day;
public:
myDate(); // 初始化为1900,1,1
myDate(int newyear,int newmonth,int newday);
bool validity(int newyear,int newmonth,int newday); // 检查日期格式合法性
bool setDate(int newyear,int newmonth,int newday); // 月:1-12,日: 根据月份决定
bool setyear(int newyear); // 1900----3000
int getyear();
bool setmonth(int newmonth); // 1--31
int getmonth();
bool setday(int newday);
int getday();
bool increaseday(int n); // 31+1==
bool reduceday(int n);
void display();
};
主函数内容
int main()
{
int n;
//1、定义对象
myDate today(2010,4,22);
//2、显示当前日期
today.display();
//3、修改日期,使用setDate()
today.setDate(2010,2,28);
//4、显示当前日期
today.display();
//5、修改日期,使用setXXX()
today.setday(26);
//6、显示当前日期
today.display();
//7、修改日期,使用increaseday()
today.increaseday(5);
today.increaseday(365);
//8、显示当前日期
today.display();
//9、修改日期,使用reduceday()
today.setDate(2014,1,1);
today.reduceday(3);
today.reduceday(365);
//10、显示当前日期
today.display();
//11、使用cout输出,getXXX()获得的数据
cout<<today.getyear()<<"年"<<today.getmonth()<<"月"<<today.getday()<<"日"<<endl;
return 0;
}
参考答案
main.cpp
#include <bits/stdc++.h>
#include "myDate.h"
using namespace std;
int main()
{
int n;
//1、定义对象
myDate today(2010, 4, 22);
//2、显示当前日期
today.display();
//3、修改日期,使用setDate()
today.setDate(2010, 2, 28);
//4、显示当前日期
today.display();
//5、修改日期,使用setXXX()
today.setday(26);
//6、显示当前日期
today.display();
//7、修改日期,使用increaseday()
today.increaseday(365);
today.increaseday(5);
//8、显示当前日期
today.display();
//9、修改日期,使用reduceday()
today.setDate(2014, 1, 1);
today.reduceday(365);
today.reduceday(3);
//10、显示当前日期
today.display();
//11、使用cout输出,getXXX()获得的数据
cout << today.getyear() << "年" << today.getmonth() << "月" << today.getday() << "日" << endl;
return 0;
}
myDate.h
#pragma once
#include <bits/stdc++.h>
using namespace std;
class myDate
{
private:
int year;
int month;
int day;
public:
myDate();
myDate(int newyear, int newmonth, int newday);
bool validity(int newyear, int newmonth, int newday);
bool setDate(int newyear, int newmonth, int newday);
bool setyear(int newyear);
int getyear();
bool setmonth(int newmonth);
int getmonth();
bool setday(int newday);
int getday();
bool increaseday(int n);
bool reduceday(int n);
void display();
};
myDate.cpp
#include "myDate.h"
myDate::myDate()
: year(1900), month(1), day(1)
{
}
myDate::myDate(int newyear, int newmonth, int newday)
: year(newyear), month(newmonth), day(newday)
{
}
bool myDate::validity(int newyear, int newmonth, int newday)
{
// 判断月和天是否合法
// Lambda
auto checkDayAndMonth = [&](bool isLeap)->bool
{
// 判断2月天数
int Feb = isLeap ? Feb = 29 : Feb = 28;
// 判断月的天数是否合法
switch (newmonth)
{
case 1: return newday >= 1 && newday <= 31 ? true : false; break;
case 2: return newday >= 1 && newday <= Feb ? true : false; break;
case 3: return newday >= 1 && newday <= 31 ? true : false; break;
case 4: return newday >= 1 && newday <= 30 ? true : false; break;
case 5: return newday >= 1 && newday <= 31 ? true : false; break;
case 6: return newday >= 1 && newday <= 30 ? true : false; break;
case 7: return newday >= 1 && newday <= 31 ? true : false; break;
case 8: return newday >= 1 && newday <= 31 ? true : false; break;
case 9: return newday >= 1 && newday <= 30 ? true : false; break;
case 10: return newday >= 1 && newday <= 31 ? true : false; break;
case 11: return newday >= 1 && newday <= 30 ? true : false; break;
case 12: return newday >= 1 && newday <= 31 ? true : false; break;
default: return false;
}
return false;
};
if (year % 4 == 0 && year % 400 != 0)
{
if(!checkDayAndMonth(true))
{
return false;
}
}
else
{
if (!checkDayAndMonth(false))
{
return false;
}
}
// 判断年是否合法
if (newyear > 3000 && newyear < 1900)
{
return false;
}
return true;
}
bool myDate::setDate(int newyear, int newmonth, int newday)
{
if (this->validity(newyear, newmonth, newday))
{
this->year = newyear;
this->month = newmonth;
this->day = newday;
return true;
}
else
{
return false;
}
}
bool myDate::setyear(int newyear)
{
if (this->validity(newyear, this->year, this->day))
{
this->year = newyear;
return true;
}
else
{
return false;
}
}
int myDate::getyear()
{
return this->year;
}
bool myDate::setmonth(int newmonth)
{
if (this->validity(this->year, newmonth, this->month))
{
this->month = newmonth;
return true;
}
else
{
return false;
}
}
int myDate::getmonth()
{
return this->month;
}
bool myDate::setday(int newday)
{
if (this->validity(this->year, this->month, newday))
{
this->day = newday;
return true;
}
else
{
return false;
}
}
int myDate::getday()
{
return this->day;
}
// 递归增加天数
bool myDate::increaseday(int n)
{
// 0代表处理完毕
if (n == 0)
{
return true;
}
// -1代表未处理完毕
if (n == -1)
{
n = 0;
}
int y = this->year;
int m = this->month;
int d = this->day + n;
if (this->validity(y, m, d))
{
this->day = d;
return true;
}
else
{
if (this->validity(y, m, 31))
{
d -= 31;
}
else if (m == 2)
{
if (m % 4 == 0 && m % 400 != 0)
{
d -= 29;
}
else
{
d -= 28;
}
}
else
{
d -= 30;
}
this->month++;
this->day = d;
if (this->month > 12)
{
this->year++;
this->month = 1;
}
if (this->year > 3000)
{
this->setDate(3000, 12, 31);
return false;
}
// 判断此次处理后的日期是否合法
if (this->validity(this->year,this->month,this->day))
{
// 合法则在下个递归中停止
return this->increaseday(0);
}
else
{
// 不合法继续处理
return this->increaseday(-1);
}
}
}
bool myDate::reduceday(int n)
{
if (n == 0)
{
return true;
}
if (n == -1)
{
n = 0;
}
int y = this->year;
int m = this->month;
int d = this->day - n;
if (this->validity(y, m, d))
{
this->day = d;
return true;
}
else
{
if (m == 1)
{
m = 12;
}
else
{
m -= 1;
}
if (this->validity(y, m, 31))
{
d += 31;
}
else if (m == 2)
{
if (m % 4 == 0 && m % 400 != 0)
{
d += 29;
}
else
{
d += 28;
}
}
else
{
d += 30;
}
this->month--;
this->day = d;
if (this->month < 1)
{
this->year--;
this->month = 12;
}
if (this->year < 1900)
{
this->setDate(1900, 1, 1);
return false;
}
if (this->validity(this->year, this->month, this->day))
{
return this->reduceday(0);
}
else
{
return this->reduceday(-1);
}
}
}
void myDate::display()
{
cout << this->year << "年" << this->month << "月" << this->day << "日" << endl;
}