题目描述
定义一个时间类,包括私有成员时、分、秒,再定义一个友元函数用来计算2个时间点相差的秒数。
输入
包括多组数据,每组数据有6个整数,用来表示2个时间点。
输出
2个时间点相差的秒数。
样例输入 Copy
15 45 10 5 45 40
20 55 40 20 12 50
样例输出 Copy
15:45:10-5:45:40=35970
20:55:40-20:12:50=2570
# include <iostream>
using namespace std;
class Time
{
public:
friend void secd(Time&, Time&);
Time(int h, int m, int s):hour(h), minute(m), sec(s){}
private:
int hour;
int minute;
int sec;
};
void secd(Time &t1, Time& t2)
{
int ch, cm, cs;
ch = t1.hour - t2.hour;
cm = t1.minute - t2.minute;
cs = t1.sec - t2.sec;
if (cs < 0)
{
cm--;
cs += 60;
}
if (cm < 0)
{
ch--;
cm += 60;
}
cout << t1.hour << ":" << t1.minute << ":" << t1.sec << "-"
<< t2.hour << ":" << t2.minute << ":" << t2.sec;
cout << "=" << ch * 3600 + cm * 60 + cs << endl;
}
int main()
{
int a, b, c, d, e, f;
while (cin >> a >> b >> c >> d >> e >> f)
{
Time t1(a, b, c), t2(d, e, f);
secd(t1, t2);
}
return 0;
}