/*
创建一个时间类Time,用秒数表示一天当中某个时间点的时间,该类包含如下的构造函数,
Time(); //无参构造函数,默认将时间设置为0时,0分,0秒
Time(int h); //一参构造函数
Time(int h, int m); //二参构造函数
Time(int h, int m, int s); //三参构造函数
并为小时,分钟和秒设计set函数和get函数,
// set 函数
void setTime( int, int, int ); // set hour, minute, second
void setHour( int ); // set hour (after validation)
void setMinute( int ); // set minute (after validation)
void setSecond( int ); // set second (after validation)
// get 函数
int getHour(); // return hour
int getMinute(); // return minute
int getSecond(); // return second
最后以如下方式显示时间
24小时制:以(HH:MM:SS)方式显示时间,小时0-9的前面需要添加0;
【输入样例】
2
21 34
12 25 42
27 74 99
【输出样例】
Constructed with:
t1: all arguments defaulted 00:00:00
t2: hour specified; minute and second defaulted 02:00:00
t3: hour and minute specified; second defaulted 21:34:00
t4: hour, minute and second specified 12:25:42
t5: all invalid values specified 00:00:0
*/
#include <iostream>
#include <iomanip>
using namespace std;
class Time
{
public:
Time(); //无参构造函数
Time(int h); //一参构造函数
Time(int h, int m); //二参构造函数
Time(int h, int m, int s); //三参构造函数
~Time();
// set 函数
void setTime( int, int, int ); // set hour, minute, second
void setHour( int ); // set hour (after validation)
void setMinute( int ); // set minute (after validation)
void setSecond( int ); // set second (after validation)
// get 函数
int getHour(); // return hour
int getMinute(); // return minute
int getSecond(); // return second
// 访问函数
void Print_Universal(); // output Time in universal-Time format
private:
int seconds; // 0 - 86399(一天86400秒)
}; // end class Time
Time::Time() //无参构造函数
{
setTime( 0, 0, 0 );
}
Time::Time(int h)//一参构造函数
{
setTime( h, 0, 0 );
}
Time::Time(int h, int m) //二参构造函数
{
setTime( h, m, 0 );
}
Time::Time(int h, int m, int s) //三参构造函数
{
setTime( h, m, s );
}
Time::~Time(){}
// set 函数
void Time::setTime( int h, int m, int s ) // set hour, minute, second
{
setHour(h);
setMinute(m);
setSecond(s);
}
void Time::setHour( int h) // set hour (after validation)
{
h=(h>=0&&h<24)?h:0;
seconds=h*3600+getMinute()*60+getSecond();
}
void Time::setMinute( int m) // set minute (after validation)
{
m=(m>=0&&m<60)?m:0;
seconds=getHour()*3600+m*60+getSecond();
}
void Time::setSecond( int s) // set second (after validation)
{
s=(s>=0&&s<60)?s:0;
seconds=getHour()*3600+getMinute()*60+s;
}
// get 函数
int Time::getHour()
{ return seconds / 3600 % 24; }
int Time::getMinute()
{ return (seconds % 3600) / 60; }
int Time::getSecond()
{ return seconds % 60; }
// 访问函数
void Time::Print_Universal() // output Time in universal-Time format
{
cout<<setw(2)<<setfill('0')<<getHour()<<":"<<setw(2)<<setfill('0')<<getMinute()<<":"<<setw(2)<<setfill('0')<<getSecond()<<endl;
}
int main()
{
int h,m,s;
Time t1;
cin>>h;
Time t2( h );
cin>>h>>m;
Time t3( h, m );
cin>>h>>m>>s;
Time t4( h, m, s );
cin>>h>>m>>s;
Time t5( h, m, s );
Time & rt4 = t4;
Time * pt5 = &t5;
cout << "Constructed with:\nt1: all arguments defaulted ";
t1.Print_Universal(); // 00:00:00
cout << "\nt2: hour specified; minute and second defaulted ";
t2.Print_Universal(); // 02:00:00
cout << "\nt3: hour and minute specified; second defaulted ";
t3.Print_Universal(); // 21:34:00
cout << "\nt4: hour, minute and second specified ";
rt4.Print_Universal(); // 12:25:42
cout << "\nt5: all invalid values specified ";
pt5->Print_Universal(); // 00:00:00
cout << endl;
return 0;
} // end main