C++ 定义一个类

定 义 一 个 名 为 Date 的 类 ,用 于 输 入 并 验 证 日 期 ,类 中 的 数 据 成 员 和
成员函数应满足下面的规则;在主函数中编写相应代码验证这些规
则 是 否 正 确 地 实 现 了 ; 并 用 Debug 功 能 仔 细 体 会 程 序 的 运 行 过 程 。
1 ) 日 期 包 括 年 ( y e a r ) 、 月 ( mo n t h ) 、 日 ( d a y ) , 均 为 整 型 数 据 , 以 及 一
个 布 尔 数 据 成 员 pa ss 用 来 判 断 输 入 日 期 是 否 正 确 。
2 ) 布 尔 类 型 p r i v a t e 成 员 函 数 c h e c k F o r ma t ( ) , 有 一 个 字 符 串 引 用 形
参 , 验 证 读 入 的 日 期 格 式 是 否 正 确 ( yyyy: mm:dd 格 式 ) ?
3) void 类 型 private 成 员 函 数 validate(), 没 有 形 参 。 判 断 输 入 的 日
期是否合法。
4) 默 认 构 造 函 数 , 没 有 形 参 , 将 year , mont h, da y 分 别 初 始 化 为
2006, 1, 1; 在 构 造 函 数 中 要 调 用 va lida te( )。
5) 定 义 拷 贝 构 造 函 数
6) 定 义 类 型 转 换 构 造 函 数 , 能 从 字 符 串 转 换 成 Date 对 象 。
7 ) 使 用 带 默 认 参 数 的 构 造 函 数 , 三 个 整 形 形 参 i n t y , i n t m= 2 , i n t
d=29, 分 别 对 yea r, mont h, da y 赋 值 ; 注 意 要 调 用 va lida te( )
验证。
8) void 类 型 public 成 员 函 数 setDate(), 有 一 个 字 符 串 引 用 形 参 , 负
责 设 置 新 日 期 , 先 调 用 c h e c k F o r ma t ( ) 判 断 日 期 数 据 格 式 是 否 正首
确 , 如 正 确 , 继 续 调 用 va lida te( )验 证 输 入 日 期
9) void 类 型 public 成 员 函 数 pr intD a ta( ), 没 有 形 参 , 根 据 pa ss 值
决 定 是 否 打 印 设 置 的 日 期 , 如 果 打 印 , 要 按 照 ” yyyy 年 mm 月 dd
日 ”的 格 式 。
求代码,各位高手请帮忙,还要有注释....我可以加分

第1个回答  推荐于2017-09-24
亲手敲的代码,要加分啊,哪里看不懂问我
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

class Date
{
public:
Date();//构造函数
Date(const Date&);//拷贝构造函数
Date strToDate(string);//将string转化为Date
void setDate(string);//设置日期
void printDate();//打印日期
private:
int year;
int month;
int day;
bool pass;
bool checkFormat(string);//验证日期输入格式
void validate();//验证日期数值有效性
bool leapYear(int);//闰年判断
};

Date::Date()
{
year = 2006;
month = day = 1;
validate();
}

Date::Date(const Date& date)
{
year = date.year;
month = date.month;
day = date.day;
pass = date.pass;
}

Date Date::strToDate(string ds)
{
// yyyy:mm:dd
Date d;
d.setDate(ds);
return d;
}

void Date::setDate(string ds)
{
if( !checkFormat(ds) )
{
cout << "日期格式错误" << endl;
pass = false;
return;
}
validate();
if(pass)
{
year = atoi(ds.substr(0, 4).c_str());
month = atoi(ds.substr(5, 2).c_str());
day = atoi(ds.substr(8, 2).c_str());
}
}

void Date::printDate()
{
if (pass)
{
cout << year << "年"
<< month << "月"
<< day << "日" << endl;
}
else
{
cout << "日期错误" << endl;
}
}

void Date::validate()
{
pass = false;
if (year < 1900 || month < 1 || month > 12 || day < 1 || day > 31)
{
return;
}
if (month == 2)
{
if (leapYear(year))
{
if (day <= 29)
{
pass = true;
}
return;
}
else
{
if (day <= 28)
{
pass = true;
}
return;
}
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
if (month <= 30)
{
pass = true;
}
return;
}
else
pass = true;
}

bool Date::leapYear(int year)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
{
return true;
}
}
else
{
if ((year % 4) == 0)
{
return true;
}
}
return false;
}

bool Date::checkFormat(string ds)
{
//yyyy:mm:dd
if (ds.length() != 10
|| ds[4] != ':'
|| ds[7] != ':')
{
return false;
}

for (int i = 0; i < (int)ds.length(); i++ )
{
if( i != 4 && i != 7)
{
if (ds[i] < '0' || ds[i] > '9')
{
return false;
}
}
}
return true;
}

int main()
{
// 测试是否能正确设置日期
string str;
cout << "--TEST:请输入一个日期<yyyy:mm:dd>:" << endl;
cin >> str;
Date d;
d.setDate(str);
d.printDate();

//测试拷贝构造函数
cout << "--TEST:调用拷贝构造函数" << endl;
Date dc = d;
dc.printDate();

//测试字符串到日期转换的函数
cout << "--TEST:将字符串’2012:03:28‘转化为Date类型并输入" << endl;
Date dt = d.strToDate("2012:03:28");
dt.printDate();
return 0;
}本回答被提问者采纳
相似回答