Problem C: 时间和日期类(III)

本文介绍了一个日期时间类的设计实现,包括Date和Time两个子类,支持多种构造方式及日期时间的显示与设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem C: 时间和日期类(III)

Time Limit: 4 Sec   Memory Limit: 128 MB
Submit: 4184   Solved: 2506
[ Submit][ Status][ Web Board]

Description

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

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

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

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

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

在上述两个DateTime类的构造函数中输出:“CREATE DateTime : (y, m, d, hh, mm, ss)”,其中y、m、d为初始化对象时的年月日值,h、m、s为初始化对象时的时分秒值。参见输出。

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

在这个DateTime类的构造函数中输出:“CREATE DateTime : (y, m, d) (hh, mm, ss)”,其中y、m、d为初始化对象时的年月日值,h、m、s为初始化对象时的时分秒值。参见输出。

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类的构造函数中输出:“CREATE Date : (y, m, d)”,其中y、m、d为初始化对象时的年月日值。参见输出。

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

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

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

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

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

在Time类的构造函数中输出:“CREATE Time : (h, m, s)”,其中h、m、s为初始化对象时的时分秒值。参见输出

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

31982 10 1 0 0 02000 2 28 23 59 592014 7 2 13 30 01

Sample Output

CREATE Time : (0, 0, 0)CREATE Date : (1, 1, 1)CREATE DateTime : (1, 1, 1, 0, 0, 0)0001-01-01 00:00:001982-10-01 00:00:002000-02-28 23:59:592014-07-02 13:30:01

HINT

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

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

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

Append Code

[ Submit][ Status][ Web Board]
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
class Time{
    private :
       friend class DateTime;
       int hh,mm,ss;
    public :
       Time():hh(0),mm(0),ss(0){
           cout<< "CREATE Time : (" <<hh<< ", " <<mm<< ", " <<ss<< ")" <<endl;
       }
       Time( int a, int b, int c):hh(a),mm(b),ss(c){}
       Time &setTime( int a, int b, int c){hh=a;mm=b;ss=c; return * this ;}
       void showTime(){
         cout<<setw(2)<<setfill( '0' )<<hh<< ":" <<setw(2)<<setfill( '0' )<<mm<< ":" <<setw(2)<<setfill( '0' )<<ss;
       }
       ~Time(){}
};
class Date{
    private :
        friend class DateTime;
        int year,month,day;
    public :
        Date():year(1),month(1),day(1){
            cout<< "CREATE Date : (" <<year<< ", " <<month<< ", " <<day<< ")" <<endl;
        }
        Date( int a, int b, int c):year(a),month(b),day(c){}
        Date &setDate( int a, int b, int c){year=a;month=b;day=c; return * this ;}
        void showDate() {
            cout<<setfill( '0' )<<setw(4)<<year<< "-" <<setfill( '0' )<<setw(2)<<month<< "-" <<setfill( '0' )<<setw(2)<<day<< " " ;
        }
        ~Date(){}
};
class DateTime{
    private :
        friend class Date;
        friend class Time;
        Time T;
        Date D;
        int year1,month1,day1,hh1,mm1,ss1;
    public :
        DateTime(){
          cout<< "CREATE DateTime : (" <<D.year<< ", " <<D.month<< ", " <<D.day<< ", " <<T.hh<< ", " <<T.mm<< ", " <<T.ss<< ")" <<endl;
        }
        DateTime( const Date& d, const Time& t):D(d),T(t){}
        DateTime( int a, int b, int c, int d, int e, int f):D(a,b,c),T(d,e,f){}
        void showDateTime(){
          D.showDate();  T.showTime();
        }
        DateTime &setDateTime( int a, int b, int c, int d, int e, int f){
            D.setDate(a,b,c);T.setTime(d,e,f); return * this ;
        }
        ~DateTime(){}
};
int main()
{
     DateTime date_time;
     date_time.showDateTime();
     cout << endl;
     int cases;
     cin >> cases;
     for ( int ca = 0; ca < cases; ca++)
     {
         int year, month, day;
         cin >> year >> month >> day;
         int hour, minute, second;
         cin >> hour >> minute >> second;
         date_time.setDateTime(year, month, day, hour, minute, second);
         date_time.showDateTime();
         cout << endl;
     }
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值