Description
封装一个时间类Time,用于时间处理的相关功能,支持以下操作:
- Time::Time()无参构造方法。
- Time::Time(int,int,int)构造方法:传递时分秒的三个参数构造对象。
- Time::Time(const T&)拷贝构造方法。
- 成员读函数: Time::hour() :返回Time的小时数; Time::minute():返回Time的分钟数; Time::second():返回Time的秒数。
- 成员写函数: Time::hour(int) :传参修改Time的小时数; Time::minute(int):传参修改Time的分钟数; Time::second(int):传参修改Time的秒数。
- 对象整体读写方法: Time::setTime(int,int,int)方法:传递时分秒三个参数修改Time对象的时分秒数。该方法返回修改后的对象。
Time::setTime(const T&)方法:传递一个参数修改Time对象的时分秒数。该方法返回修改后的对象。
Time::getTime()方法:返回对象自身的引用。其实,t.getTime()即t。
仅在Time类中的Time::getTime()方法实在是多余,在组合或者继承关系时才会有机会用到。- Time::inputTime()方法:按格式从标准输入读取数据修改Time对象的时分秒数值。该方法返回修改后的对象。
- Time::showTime()方法:输出“hh:mm:ss”,不足两位的要前面补0。如果对象不是合法的时间,则输出“Time error”。 你设计一个时间类Time,使得main()函数能够正确运行。 函数调用格式见append.cc。
append.cc中已给出main()函数。main()函数内容稍微繁复,仅为测试对象的各种调用情况。
Input
输入的第一个整数n,表示有n组测试数据,每组3个整数:hh,mm,ss,分别表示时、分、秒,其值都在int范围内。
Output
每组测试数据对应一组输出“hh:mm:ss”,不足两位的输出需要前面补0。如果输入的时间不合法,则输出“Time
error”。格式见sample。
Sample Input
6
0 0 1
0 59 59
1 1 60
23 0 0
23 59 59
24 1 0
Sample Output
00:00:01
00:59:59
Time error
23:00:00
23:59:59
Time error
HINT
输出格式用头文件<iomanip>中流操作算子:
setw(w) :设置数据的输出宽度为w个字符
setfill(c):设置用字符c作为填充字符
Append Code
int main()
{
Time t;
int cases;
cin>>cases;
for(int i = 1; i <= cases; ++i)
{
if(i % 4 == 0)
{
int hour, minute, second;
cin>>hour>>minute>>second;
Time tt(hour, minute, second);
tt.showTime();
}
if(i % 4 == 1)
{
int hour, minute, second;
cin>>hour>>minute>>second;
t.setTime(hour, minute, second).showTime();
}
if(i % 4 == 2)
t.inputTime().showTime();
if(i % 4 == 3)
{
int hour, minute, second;
cin>>hour>>minute>>second;
t.hour(hour);
t.minute(minute);
t.second(second);
t.showTime();
}
}
}
AC代码
#include <iostream>
#include <iomanip>
using namespace std;
class Time {
private:
int hour1, minute1, second1;
public:
Time(): hour1(0), minute1(0), second1(0) {}
Time(int h, int m, int s): hour1(h), minute1(m), second1(s) {}
/*Time(const Time& t):hour1(t.hour1), minute1(t.minute1), second1(t.second1) {
cout << "There was a call to the copy constructor : " << hour1 << ',' << minute1 << ',' << second1 << endl;
}*/
Time& setTime(int h, int m, int s) {
hour1 = h; minute1 = m; second1 = s;
return *this;
}
Time& setTime(const Time& t) {
hour1 = t.hour1; minute1 = t.minute1; second1 = t.second1;
return *this;
}
Time& getTime() { return *this; }
static bool check(const Time t) {
return t.hour1 >= 0 && t.hour1 < 24 && t.minute1 >= 0 && t.minute1 < 60 && t.second1 >= 0 && t.second1 < 60;
}
void showTime() {
if (check(*this))
cout << setw(2) << setfill('0')
<< setw(2) << setfill('0') << hour1 << ":" <<
setw(2) << setfill('0') << minute1 << ":" <<
setw(2) << setfill('0') << second1 << endl;
else {
cout << "Time error\n";
}
}
int hour() {return hour1;}
int minute() {return minute1;}
int second() {return second1;}
Time& hour(int h) { hour1 = h; return *this; }
Time& minute(int m) { minute1 = m; return *this; }
Time& second(int s) { second1 = s; return *this; }
Time& inputTime() {
int h, m, s;
cin >> h >> m >> s;
hour1 = h; minute1 = m; second1 = s;
return *this;
}
};