请为时间类添加设置、读取功能。
#include <iostream>
#include <iomanip>
using namespace std;
/* 你提交的代码将被嵌在这里 */
int main()
{
MYTIME a;
int h, m, s;
cin >> h;
a.Set(h);
a.Output();
cout << endl;
cin >> h >> m;
a.Set(h, m);
a.Output();
cout << endl;
cin >> h >> m >> s;
a.Set(h, m, s);
a.Output();
cout << endl;
a.Input();
a.Get(h, m, s);
cout << h << ' ' << m << ' ' << s << endl;
return 0;
}
输入样例
5
6 15
8 30 45
12:24:48
输出样例
05:00:00
06:15:00
08:30:45
12 24 48
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
答案
class MYTIME {
private:
int hour;
int minute;
int second;
public:
void Set(int h=0, int m=0, int s=0) { hour = h, minute = m, second = s; }
void Output()
{
cout << setfill('0') << setw(2) << this->hour << ':' <<
setfill('0') << setw(2) << this->minute << ':' <<
setfill('0') << setw(2) << this->second;
}
void Input()
{
char sign = ':';
cin >> this->hour >> sign >> this->minute >> sign >> this->second;
}
void Get(int &h,int &m,int &s)
{
h = this->hour;
m = this->minute;
s = this->second;
}
};