首先禁止构造函数自动将整数转换为时间,即使下面的程序无法通过编译。
void Test1()
{
int a;
MYTIME b;
cin >> a;
b = a; // 使这里无法通过编译
cout << b << endl;
}
将 a = b; 改为 a = MYTIME(b); 即改成下面的样子就可以通过。
void Test1()
{
int a;
MYTIME b;
cin >> a;
b = MYTIME(a); // 现在可以通过编译
cout << b << endl;
}
输入样例
8
输出样例
Create 00:00:00
Create 08:00:00
Assign 08:00:00
Destroy 08:00:00
08:00:00
Destroy 08:00:00
然后添加将时间类对象转为整数(时数)的函数。用户必须通过强制类型转换运算符来实现类型转换,即使下面的代码通过编译。
void Test2()
{
int h, m, s, b;
const MYTIME a(h, m, s);
b = int(a);
cout << b << endl;
}
输入样例
20 35 47
输出样例
Create 20:35:47
20
Destroy 20:35:47
请完成类型转换运算符的设计。
#include <iostream>
#include <iomanip>
using namespace std;
/* 你提交的代码将被嵌在这里 */
void Test1()
{
int a;
MYTIME b;
cin >> a;
b = MYTIME(a);
cout << b << endl;
}
void Test2()
{
int h, m, s, b;
cin >> h >> m >> s;
const MYTIME a(h, m, s);
b = int(a);
cout << b << endl;
}
int main()
{
Test1();
Test2();
return 0;
}
输入样例
8 20 35 47
输出样例
Create 00:00:00
Create 08:00:00
Assign 08:00:00
Destroy 08:00:00
08:00:00
Destroy 08:00:00
Create 20:35:47
20
Destroy 20:35:47
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
答案
class MYTIME
{
public:
explicit MYTIME(int h = 0, int m = 0, int s = 0);
operator int() const;//强制类型转换
MYTIME(const MYTIME &time);
~MYTIME();
void Set(int h = 0, int m = 0, int s = 0);
void Hour(int h);
void Minute(int m);
void Second(int s);
void Get(int& h, int& m, int& s) const;
int Hour() const;
int Minute() const;
int Second() const;
MYTIME& operator=(const MYTIME& time);
void TotalSecond(int s);
int TotalSecond() const;
MYTIME Sub(int s) const;
MYTIME Add(int s) const;
int Sub(const MYTIME& time) const;
bool Gt(const MYTIME& time) const;
bool Ge(const MYTIME& time) const;
bool Lt(const MYTIME& time) const;
bool Le(const MYTIME& time) const;
bool Eq(const MYTIME& time) const;
bool Ne(const MYTIME& time) const;
friend bool operator > (const MYTIME& p, const MYTIME& p1);
friend bool operator >= (const MYTIME& p, const MYTIME& p1);
friend bool operator < (const MYTIME& p, const MYTIME& p1);
friend bool operator <= (const MYTIME& p, const MYTIME& p1);
friend bool operator == (const MYTIME& p, const MYTIME& p1);
friend bool operator != (const MYTIME& p, const MYTIME& p1);
static bool IsValidHour(int h);
static bool IsValidMinute(int m);
static bool IsValidSecond(int s);
static bool IsValid(int h, int m, int s);
static void FlagOn();
static void FlagOff();
static const int hoursPerDay;
static const int minutesPerHour;
static const int secondsPerMinute;
static const int secondsPerDay;
static const int secondsPerHour;
friend istream& operator >>(istream& is, MYTIME& p);
friend ostream& operator <<(ostream& os, const MYTIME& p);
MYTIME operator +(int second) const;
MYTIME operator -(int second) const;
friend int operator -(const MYTIME& time1, const MYTIME& time2);
MYTIME& operator +=(int second);
MYTIME& operator -=(int second);
MYTIME& operator ++();
MYTIME& operator --();
MYTIME operator ++(int);
MYTIME operator --(int);
private:
int hour, minute, second;
static bool flag;
};
MYTIME::MYTIME(int h, int m, int s)
{
if (MYTIME::IsValid(h, m, s))
{
hour = h;
minute = m;
second = s;
}
else
{
hour = 0; minute = 0; second = 0;
}
cout << "Create ";
cout << setfill('0') << setw(2) << hour
<< ':' << setw(2) << minute
<< ':' << setw(2) << second
<< setfill(' ');
cout << endl;
}
MYTIME::~MYTIME()
{
cout << "Destroy ";
cout << setfill('0') << setw(2) << hour
<< ':' << setw(2) << minute
<< ':' << setw(2) << second
<< setfill(' ');
cout << endl;
}
void MYTIME::Set(int h, int m, int s)
{
if (MYTIME::IsValid(h, m, s))
{
hour = h; minute = m; second = s;
}
}
void MYTIME::Hour(int h)
{
if (MYTIME::IsValidHour(h))
{
hour = h;
}
}
void MYTIME::Minute(int m)
{
if (MYTIME::IsValidMinute(m))
{
minute = m;
}
}
void MYTIME::Second(int s)
{
if (MYTIME::IsValidSecond(s))
{
second = s;
}
}
void MYTIME::Get(int& h, int& m, int& s) const
{
h = hour; m = minute; s = second;
}
int MYTIME::Hour() const
{
return hour;
}
int MYTIME::Minute() const
{
return minute;
}
int MYTIME::Second() const
{
return second;
}
MYTIME::MYTIME(const MYTIME& p)
{
hour = p.hour; minute = p.minute; second = p.second;
cout << "Clone ";
cout << setfill('0') << setw(2) << hour
<< ':' << setw(2) << minute
<< ':' << setw(2) << second
<< setfill(' ');
cout << endl;
}
MYTIME& MYTIME::operator=(const MYTIME& time)
{
if (this != &time)
{
hour = time.hour;
minute = time.minute;
second = time.second;
}
cout << "Assign ";
cout << setfill('0') << setw(2) << hour
<< ':' << setw(2) << minute
<< ':' << setw(2) << second
<< setfill(' ');
cout << endl;
return *this;
}
void MYTIME::TotalSecond(int s)
{
int t = (86400 + s % 86400) % 86400;
second = t % 60;
t /= 60;
minute = t % 60;
hour = t / 60;
}
int MYTIME::TotalSecond() const
{
return hour * 3600 + minute * 60 + second;
}
MYTIME MYTIME::Sub(int s) const
{
MYTIME result;
result.TotalSecond(TotalSecond() - s % 86400);
return result;
}
MYTIME MYTIME::Add(int s) const
{
MYTIME result;
result.TotalSecond(TotalSecond() + s % 86400);
return result;
}
int MYTIME::Sub(const MYTIME& time) const
{
return TotalSecond() - time.TotalSecond();
}
MYTIME MYTIME :: operator +(int second) const
{
MYTIME result;
result.TotalSecond(TotalSecond() + second % 86400);
return result;
}
MYTIME MYTIME :: operator -(int second) const
{
MYTIME p;
p.TotalSecond(TotalSecond() - second % secondsPerDay);
return p;
}
int operator -(const MYTIME& time1, const MYTIME& time2)
{
return time1.TotalSecond() - time2.TotalSecond();
}
bool MYTIME::Gt(const MYTIME& time) const
{
return TotalSecond() > time.TotalSecond();
}
bool operator > (const MYTIME& p, const MYTIME& p1)
{
return p.TotalSecond() > p1.TotalSecond();
}
bool MYTIME::Ge(const MYTIME& time) const
{
return TotalSecond() >= time.TotalSecond();
}
bool operator >= (const MYTIME& p, const MYTIME& p1)
{
return p.TotalSecond() >= p1.TotalSecond();
}
bool MYTIME::Lt(const MYTIME& time) const
{
return TotalSecond() < time.TotalSecond();
}
bool operator < (const MYTIME& p1, const MYTIME& p2)
{
return p1.TotalSecond() < p2.TotalSecond();
}
bool MYTIME::Le(const MYTIME& time) const
{
return TotalSecond() <= time.TotalSecond();
}
bool operator <= (const MYTIME& p, const MYTIME& p1)
{
return p.TotalSecond() <= p1.TotalSecond();
}
bool MYTIME::Eq(const MYTIME& time) const
{
return TotalSecond() == time.TotalSecond();
}
bool operator == (const MYTIME& p, const MYTIME& p1)
{
return p.TotalSecond() == p1.TotalSecond();
}
bool MYTIME::Ne(const MYTIME& time) const
{
return TotalSecond() != time.TotalSecond();
}
bool operator != (const MYTIME& p, const MYTIME& p1)
{
return p.TotalSecond() != p1.TotalSecond();
}
bool MYTIME::IsValidHour(int h)
{
return h < MYTIME::hoursPerDay && h >= 0;
}
bool MYTIME::IsValidMinute(int m)
{
return m < MYTIME::minutesPerHour && m >= 0;
}
bool MYTIME::IsValidSecond(int s)
{
return s < MYTIME::secondsPerMinute && s >= 0;
}
bool MYTIME::IsValid(int h, int m, int s)
{
return h < MYTIME::hoursPerDay && h >= 0 && m < MYTIME::minutesPerHour && m >= 0 && s < MYTIME::secondsPerMinute && s >= 0;
}
void MYTIME::FlagOn()
{
flag = true;
}
void MYTIME::FlagOff()
{
flag = false;
}
const int MYTIME::hoursPerDay = 24;
const int MYTIME::minutesPerHour = 60;
const int MYTIME::secondsPerMinute = 60;
const int MYTIME::secondsPerHour = 3600;
const int MYTIME::secondsPerDay = 86400;
bool MYTIME::flag = false;
istream& operator>>(istream& is, MYTIME& p)
{
int hour, minute, second;
char s1, s2;
is >> hour >> s1 >> minute >> s2 >> second;
if (MYTIME::IsValid(hour, minute, second))
{
p.hour = hour;
p.minute = minute;
p.second = second;
}
return is;
}
ostream& operator << (ostream& os, const MYTIME& time)
{
os << setfill('0') << setw(2) << time.hour
<< ':' << setw(2) << time.minute
<< ':' << setw(2) << time.second
<< setfill(' ');
if (MYTIME::flag)
{
os << ' ' << (time.hour < 12 ? "am" : "pm");
}
return os;
}
MYTIME& MYTIME::operator +=(int second)
{
TotalSecond(TotalSecond() + second % secondsPerDay);
return *this;
}
MYTIME& MYTIME::operator -=(int second)
{
TotalSecond(TotalSecond() - second % secondsPerDay);
return *this;
}
MYTIME& MYTIME::operator ++()
{
TotalSecond(TotalSecond() + 1);
return *this;
}
MYTIME& MYTIME::operator --()
{
TotalSecond(TotalSecond() - 1);
return *this;
}
MYTIME MYTIME :: operator ++(int)
{
MYTIME p(*this);
TotalSecond(TotalSecond() + 1);
return p;
}
MYTIME MYTIME :: operator --(int)
{
MYTIME p(*this);
TotalSecond(TotalSecond() - 1);
return p;
}
MYTIME::operator int() const
{
return hour;
}