C++-题目:设计一个CDate类,要求满足如下要求:a)有带参构造函数b)可设置日期c)可运行日期加一天的操作d)有输出操作(用日/月/年格式输出日期)

 

#include<iostream>

 

using namespace std;

 

class CDate

 

{

 

public:

 

CDate(int nYear = 0, int nMon = 0, int nDay = 0); //构造函数

 

bool SetData(int nYear, int nMon = 0, int nDay = 0);// 设置日期函数

 

void PrintData();// 打印日期函数

 

bool AddDay(); // 将日期加1函数

 

protected:

 

bool IsTrueDate(); // 判断日期是否有效函数

 

protected:

 

int m_nYear;

 

int m_nMonth;

 

int m_nDay;

 

};

 

CDate::CDate(int nYear, int nMon, int nDay)

 

: m_nYear(nYear),

 

m_nMonth(nMon),

 

m_nDay(nDay)

 

{}

 

bool CDate::IsTrueDate()

 

{

 

// 将每一个月的天数保存在数组

 

static unsigned char days[12]

 

= { 31,0,31,30,31,30,31,31,30,31,30,31 };

 

// 判断月份是否有效(小于1,大于12都是无效月份)

 

if (m_nMonth >= 13 || m_nMonth <= 0)

 

return false;

 

// 判断年份是否是闰年

 

if (m_nYear > 0

 

&& m_nYear % 400 == 0

 

|| m_nYear % 4 == 0

 

&& m_nYear % 100 != 0)

 

{

 

days[1] = 29;//如果是闰年2月有29天

 

}

 

else

 

days[1] = 28;//不是闰年则2月只有28天

 

// 判断天数是否在对应月份的天数的范围之内

 

if (m nDay > days[m nMonth - 1] || m nDay <= 0)

 

return false;

 

    return true;

 

}

 

bool CDate::SetData(int nYear, int nMon, int nDay)

 

{

 

m_nYear = nYear;

 

m_nMonth = nMon;

 

m_nDay = nDay;

 

return IsTrueDate();

 

}

 

void CDate::PrintData()

 

{

 

if (IsTrueDate())

 

{

 

cout << "[" ;

 

}

 

else

 

{

 

cout << "无效日期";

 

}

 

}

 

bool CDate::AddDay()

 

{

 

if (IsTrueDate())

 

{

 

return false;

 

}

 

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值