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

来源:互联网 发布:手机移动办公软件 编辑:程序博客网 时间:2024/05/29 17:47
HomeWeb BoardProblemSetStandingStatusStatistics

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

Time Limit: 4 Sec  Memory Limit: 128 MB
Submit: 3096  Solved: 1871
[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

append.cc,
[Submit][Status][Web Board]
#include<iostream>#include<iomanip>using namespace std;class Date{public:    Date(){ int year = 1, month = 1, day = 1; }    Date(int y, int m, int d)    {        year=y;  month=m;  day=d;        cout << "CREATE Date : (" << y <<", "<< m <<", "<< d <<")"<< endl;    }    void showDate()const    {        cout << setw(4) << setfill('0') << year << "-"             << setw(2) << setfill('0') << month << "-"             << setw(2) << setfill('0') << day ;    }    void setDate(int y1, int m1, int d1)    {        year = y1;  month = m1;  day = d1;    }private:    int year, month, day;};class Time{public:    Time(){ int hour = 0, minute = 0, second = 0; }    Time(int h, int mi, int s){        hour=h;  minute=mi;  second=s;        cout << "CREATE Time : (" << h <<", "<< mi <<", "<< s <<")"<< endl;    }    void showTime()const{        cout << setw(2) << setfill('0') << hour << ":"             << setw(2) << setfill('0') << minute << ":"             << setw(2) << setfill('0') << second ;    }    void setTime(int h1, int mi1, int s1){        hour=h1;  minute=mi1;  second=s1;    }private:    int hour, minute, second ;};class DateTime{public:    DateTime():date(1,1,1),time(0,0,0)    {        cout << "CREATE DateTime : (" << 1<< ", " << 1 << ", "             << 1 << ", " << 0 << ", " << 0 << ", " << 0 << ")" << endl;    }    DateTime(int y, int m, int d, int h, int mi, int s):date(y,m,d),time(h,mi,s)    {        cout << "CREATE DateTime : (" << y << ", " << m << ", "             << d << ", " << h << ", " << m << ", " << s << ")" << endl;    }    DateTime(const Date&d,const Time&t):date(d),time(t){}    void showDateTime()const    {        date.showDate();        cout<<" ";        time.showTime();    }    DateTime&setDateTime( int y, int m, int d, int h, int mi, int s )    {        date.setDate(y,m,d);        time.setTime(h,mi,s);        return *this;    }private:    Time time;    Date date;};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;    }}

0 0
原创粉丝点击