Problem B: time and date class (III), problemiii
Problem B: Time and date class (III) Time Limit: 4 Sec Memory Limit: 128 MB
Submit: 2889 Solved: 1732
[Submit] [Status] [Web Board] Description
Design a date and time class to read the input data and output the date and time by format.
The DateTime design Date and Time class consists of two members: a Date class object and a Time class object;
To design a DateTime class, you must support the following operations:
DateTime: DateTime () construction method without parameters: initialized to January 1, 00:00:00;
DateTime: DateTime (int, int) constructor: Initialize the object according to the parameter (in the order of year, month, day, hour, minute, and second;
In the constructors of the preceding two DateTime classes, the output is: "CREATE DateTime: (y, m, d, hh, mm, ss )", y, m, and d are the year, month, and day values of the initialization object, and h, m, and s are the hour, minute, and second values of the initialization object. See output.
DateTime: DateTime (const Date &, const Time &) constructor: Initialize the Object Based on the Date and Time passed in the parameter;
In the constructors of this DateTime class, the output is: "CREATE DateTime: (y, m, d) (hh, mm, ss )", y, m, and d are the year, month, and day values of the initialization object, and h, m, and s are the hour, minute, and second values of the initialization object. See output.
DateTime: showDateTime () method: outputs DateTime objects by format;
DateTime: setDateTime (int, int) method: Modify the property value of an object according to the parameter (in the order of year, month, day, hour, minute, and second;
The DateTime class contains two classes: the Date class and the Time class.
The Date design Date class must support the following operations:
Date: Date () construction method without parameters: initialized to January 1, January 1
Date: Date (int, int, int) constructor: the input parameters are year, month, and day, and the Date is initialized with the parameters.
In the constructor of the Date class, the output is "CREATE Date: (y, m, d)", where y, m, and d are the year, month, and day values when the object is initialized. See output.
Date: showDate () method: outputs the Date object by format.
Date: setDate (int, int, int) method: the input parameters are year, month, and day in sequence, and the attribute values of the object are modified with parameters.
The following operations must be performed on the design Time:
Time: Time () construction method without parameters: initialization is 00:00:00
Time: Time (int, int, int) constructor: input parameters are Time, minute, and second in sequence, and Time is initialized using parameters.
In the Time class constructor, the output is "CREATE Time: (h, m, s)", where h, m, and s are the Time, minute, and second values when the object is initialized. See output
Time: showTime () method: outputs Time objects by format.
Time: setTime (int, int, int) method: the input parameters are in sequence hour, minute, and second, and the attribute value of the object is modified using parameters.
-----------------------------------------------------------------------------
You design the DateTime, Date, and Time classes to make the main () function run correctly.
For the function call format, see append. cc.
The main () function is provided in append. cc.
Input
The first integer n represents n groups of test data.
Each line following the input is a set of test data. The first three Integers of each group of test data are the year, month, and day of the date, and the last three integers are the time, minute, and second of the time.
Output: each group of test data corresponds to a row of Output. The date output format is "yyyy-mm-dd", and the time output format is "hh: mm: ss", separated by a space.
Sample Input31982 10 1 0 02000 2 28 23 592014 7 2 13 30 01 Sample OutputCREATE 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-02-28 23: 59: 592014-07-02 13: 30: 01 HINT
The output format uses the header file <iomanip> flow OPERATOR:
Setw (w): Set the data output width to w characters
Setfill (c): Set to use character c as the filling character
Append Code
# Include <iostream>
# Include <iomanip>
Using namespace std;
Int k = 0;
Class Time
{
Friend class DateTime;
Public:
Int h, m, s;
Time (int a = 0, int B = 0, int c = 0): h (a), m (B), s (c ){}
Void showTime ()
{
Cout <setw (2) <setfill ('0')
}
Time setTime (int a, int B, int c)
{
* This = Time (a, B, c );
Return * this;
}
};
Class Date
{
Friend class DateTime;
Public:
Int year, month, day;
Date (int a = 1, int B = 1, int c = 1): year (a), month (B), day (c ){}
Void showDate ()
{
Cout <setw (4) <setfill ('0') <year <"-" <setw (2) <setfill ('0 ') <month <"-" <setw (2) <setfill ('0') <day;
}
Date setDate (int a, int B, int c)
{
* This = Date (a, B, c );
Return * this;
}
};
Class DateTime: public Time, public Date
{
Public:
DateTime (): Date (, 1), Time (, 0) {cout <"CREATE Time: (" <Time: h <", "<Time: m <", "<Time: s <") "<endl; cout <" CREATE Date: ("<Date:: year <"," <Date: month <"," <Date: day <")" <endl; cout <"CREATE DateTime: ("<Date: year <", "<Date: month <", "<Date: day <", "<Time :: h <"," <Time: m <"," <Time: s <")" <endl ;}
DateTime (const Date & p, const Time & q): Date (p. year, p. month, p. day), Time (q. h, q. m, q. s ){}
DateTime (int a, int B, int c, int d, int e, int f): Date (a, B, c), Time (d, e, f ){}
Void showDateTime ()
{
Date: showDate ();
Cout <"";
Time: showTime ();
}
DateTime & setDateTime (int a, int B, int c, int d, int e, int f)
{
* This = DateTime (a, B, c, d, e, f );
Return * this;
}
};
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;
}
}