实验6:Problem D: 时间和日期类(II)

Description

设计一个日期时间类,用于读取输入的数据,按格式输出日期和时间。

 

设计日期时间类DateTime由2个成员组成,分别是一个Date类对象和一个Time类对象;

设计DateTime类需支持以下操作:

DateTime::DateTime()无参构造方法:初始化为1年1月1日、0时0分0秒;

DateTime::DateTime(const Date&,const Time&)构造方法:依照参数传入的日期和时间初始化对象;

DateTime::DateTime(int,int,int,int,int,int)构造方法:依照参数(顺序为年月日、时分秒)初始化对象;

DateTime::showDateTime()方法:按格式输出DateTime对象;

DateTime::setDateTime(int,int,int,int,int,int)方法:依照参数(顺序为年月日、时分秒)修改对象的属性值;

 

DateTime类包含了两个类:Date类和Time类

 

设计日期类Date需支持以下操作:

Date::Date()无参构造方法:初始化为1年1月1日

Date::Date(int,int,int)构造方法:传入的参数依次为年月日,用参数将日期初始化。

Date::showDate()方法:按格式输出Date对象。

Date::setDate(int,int,int)方法:传入的参数依次为年月日,用参数修改对象的属性值

 

设计时间类Time需支持以下操作:

Time::Time()无参构造方法:初始化为0时0分0秒

Time::Time(int,int,int)构造方法:传入的参数依次为时分秒,用参数将时间初始化。

Time::showTime()方法:按格式输出Time对象。

Time::setTime(int,int,int)方法:传入的参数依次为时分秒,用参数修改对象的属性值

 

-----------------------------------------------------------------------------

 

你设计DateTime类、Date类和Time类,使得main()函数能够正确运行。

 

函数调用格式见append.cc。

 

append.cc中已给出main()函数。

 

 

Input

输入的第一个整数n,表示有n组测试数据。

后面的输入每行为一组测试数据。每组测试数据的前3个整数是日期的年月日,后3个整数是时间的时分秒。

 

 

Output

每组测试数据对应一行输出。日期的输出格式为“yyyy-mm-dd”,时间的输出格式为“hh:mm:ss”,中间用一个空格分开。
 

 

Sample Input

3 1982 10 1 0 0 0 2000 2 28 23 59 59 2014 7 2 13 30 01

Sample Output

1000-10-10 01:01:01 1982-10-01 00:00:00 2000-02-28 23:59:59 2014-07-02 13:30:01

HINT

 

输出格式用头文件<iomanip>中流操作算子:

setw(w)   :设置数据的输出宽度为w个字符

setfill(c):设置用字符c作为填充字符

 

 

Append Code

 1 #include<iostream>
 2 #include<iomanip>
 3 using namespace std;
 4 class Date{
 5     friend class DateTime;
 6 private:
 7     int y,m,d;
 8 public:
 9     Date():y(1),m(1),d(1){}
10     Date(int yy,int mo,int dd):y(yy),m(mo),d(dd){}
11     void showDate(){cout<<setw(4)<<setfill('0')<<y<<"-"<<setw(2)<<setfill('0')<<m<<"-"<<setw(2)<<setfill('0')<<d<<" ";}
12     void setDate(int a,int b,int c){y=a;m=b;d=c;}
13 };
14 class Time{
15     friend class DateTime;
16 private:
17     int h,m,s;
18 public:
19     Time():h(0),m(0),s(0){}
20     Time(int hh,int mm,int ss):h(hh),m(mm),s(ss){}
21     void showTime(){cout<<setw(2)<<setfill('0')<<h<<":"<<setw(2)<<setfill('0')<<m<<":"<<setw(2)<<setfill('0')<<s;}
22     void setTime(int a,int b,int c){h=a;m=b;s=c;}
23 };
24 class DateTime{
25     friend class Date;
26     friend class Time;
27 private:
28     Date d;
29     Time t;
30 public:
31     DateTime(){}
32     DateTime(const Date& D,const Time& T){d=D;t=T;}
33     DateTime(int yy,int mo,int dd,int hh,int mm,int ss):d(yy,mo,dd),t(hh,mm,ss){}
34     void showDateTime(){d.showDate();t.showTime();}
35     DateTime& setDateTime(int yy,int mo,int dd,int hh,int mm,int ss){d.setDate(yy,mo,dd);t.setTime(hh,mm,ss);return *this;}
36 };
37 int main()
38 {
39     Date date(1000, 10, 10);
40     Time time(1, 1, 1);
41     DateTime date_time(date, time);
42     date_time.showDateTime();
43     cout << endl;
44     int cases, flag = 0;
45     cin >> cases;
46     for(int ca = 0; ca < cases; ca++)
47     {
48         int year, month, day;
49         cin >> year >> month >> day;
50         int hour, minute, second;
51         cin >> hour >> minute >> second;
52         if(flag == 0)
53         {
54             flag = 1;
55             DateTime dt(year, month, day, hour, minute, second);
56             dt.showDateTime();
57         }
58         else if(flag == 1)
59         {
60             flag == 0;
61             date_time.setDateTime(year, month, day, hour, minute, second).showDateTime();
62         }
63         cout << endl;
64     }
65 }

 

转载于:https://www.cnblogs.com/auto1945837845/p/5383899.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述 现在我们需要实现一个日期类 MyDate,它应该有以下几个功能: 1.初始化:MyDate(int year, int month, int day),其中 year 表示年份,month 表示月份,day 表示日期。 2.获取日期:int getDay(),获取当前日期。 3.获取月份:int getMonth(),获取当前月份。 4.获取年份:int getYear(),获取当前年份。 5.判断是否为闰年:bool isLeapYear(),如果是闰年则返回 true,否则返回 false。 6.计算两个日期之间相差的天数:int daysBetweenDates(MyDate& date),其中 date 是另一个 MyDate 类型的日期对象,返回当前日期与 date 日期之间相差的天数。假设 date 日期晚于当前日期。 注意事项: 1.年份 year 的范围为 [1900, 2100]。 2.月份 month 的范围为 [1, 12]。 3.日期 day 的范围为 [1, 31]。 4.两个日期之间相差的天数不会超过 int 类型能表示的范围。 5.如果输入的日期不符合要求,则输出 "invalid date"。 输入格式 第一行一个整数 T,表示共有 T 组测试数据。 每组测试数据占一行,包含六个整数 year, month, day, year2, month2, day2,其中 (year, month, day) 表示当前日期,(year2, month2, day2) 表示另一个日期。 输出格式 对于每组测试数据,输出其相差的天数。 如果输入的日期不符合要求,则输出 "invalid date"。 样例输入 3 2012 3 1 2012 3 2 2012 1 1 2012 3 1 2013 1 1 2012 3 1 样例输出 1 60 invalid date 数据说明 样例 #1: 2012 年 3 月 1 日到 2012 年 3 月 2 日相差 1 天。 样例 #2: 2012 年 1 月 1 日到 2012 年 3 月 1 日相差 60 天。 样例 #3: 2013 年 1 月 1 日不符合年份的要求,输出 "invalid date"。 解题思路 这道题目其实就是一个日期类的实现,我们需要根据题目意思,实现 MyDate 类。 首先,我们需要实现 MyDate 类的构造函数,用于初始化年月日。然后,我们需要实现 getDay(),getMonth(),getYear() 和 isLeapYear() 函数,用于获取年月日,并判断是否为闰年。 最后,我们需要实现 daysBetweenDates() 函数,用于计算两个日期之间相差的天数。我们可以先将两个日期转化为从 1900 年 1 月 1 日开始的天数,然后计算两者之间的差值即可。 注意事项: 1.要注意闰年的判断,闰年的条件是:能被 4 整除但不能被 100 整除,或者能被 400 整除。 2.要注意月份和日期的范围,例如 2 月份的日期不能超过 29。 3.要注意输入的日期是否符合要求,如果不符合要求,则输出 "invalid date"。 参考代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值