使用C++编写程序:
题目描述
读入两个用“时:分:秒”表示的时间点,计算以秒为单位的时间间隔。
输入
输入有两行,每行是一个用“时:分:秒”表示的时间点。测试数据保证第二个时间点晚于第一个时间点。
输出
输出一个整数,表示时间间隔的秒数。
样例输入 Copy
08:00:00
09:00:00
样例输出 Copy
3600
程序代码如下:
一个类的情况代码:
#include<iostream>
#define ElemType_U unsigned
#define ElemType_C char
using namespace std;
class TimeInterval
{
public:
TimeInterval(ElemType_U H, ElemType_U M, ElemType_U S, ElemType_U h, ElemType_U m, ElemType_U s) ;
ElemType_U GetResult();
private:
ElemType_U Hour_1, Minute_1, Second_1;
ElemType_U Hour_2, Minute_2, Second_2;
};
inline TimeInterval::TimeInterval(ElemType_U H, ElemType_U M, ElemType_U S, ElemType_U h, ElemType_U m, ElemType_U s)
{
Hour_1 = H;
Minute_1 = M;
Second_1 = S;
Hour_2 = h;
Minute_2 = m;
Second_2 = s;
}
inline ElemType_U TimeInterval::GetResult()
{
return (Hour_2 - Hour_1) * 3600 + ( Minute_2 - Minute_1 ) * 60 + ( Second_2 - Second_1 );
}
int main()
{
ElemType_U H, M, S, h, m, s;
ElemType_C C_1, C_2, c_1, c_2;
cin >> H >> C_1 >> M >> C_2 >> S;
cin >> h >> c_1 >> m >> c_2 >> s;
TimeInterval Time(H, M, S, h, m, s);
cout<<Time.GetResult();
return 0;
}
类模板的代码:
#include<iostream>
#define ElemType_U unsigned
#define ElemType_C char
using namespace std;
template<class ElemType>
class TimeInterval
{
public:
TimeInterval(ElemType H, ElemType M, ElemType S, ElemType h, ElemType m, ElemType s);
ElemType_U GetResult();
private:
ElemType Hour_1, Minute_1, Second_1;
ElemType Hour_2, Minute_2, Second_2;
};
template<class ElemType>
inline TimeInterval<ElemType>::TimeInterval(ElemType H, ElemType M, ElemType S, ElemType h, ElemType m, ElemType s)
{
Hour_1 = H;
Minute_1 = M;
Second_1 = S;
Hour_2 = h;
Minute_2 = m;
Second_2 = s;
}
template<class ElemType>
inline ElemType_U TimeInterval<ElemType>::GetResult()
{
return (Hour_2 - Hour_1) * 3600 + (Minute_2 - Minute_1) * 60 + (Second_2 - Second_1);
}
int main()
{
ElemType_U H, M, S, h, m, s;
ElemType_C C_1, C_2, c_1, c_2;
cin >> H >> C_1 >> M >> C_2 >> S;
cin >> h >> c_1 >> m >> c_2 >> s;
TimeInterval<ElemType_U> Time(H, M, S, h, m, s);
cout << Time.GetResult();
return 0;
}
友元类的情况代码:
#include<iostream>
#define ElemType_U unsigned
#define ElemType_C char
using namespace std;
class TimeInterval_1
{
public:
TimeInterval_1(ElemType_U h_1, ElemType_U m_1, ElemType_U s_1) :H_1(h_1), M_1(m_1), S_1(s_1) {};
private:
friend class TimeInterval_2;
ElemType_U H_1, M_1, S_1;
};
class TimeInterval_2
{
public:
TimeInterval_2(ElemType_U h_1, ElemType_U m_1, ElemType_U s_1, ElemType_U h_2, ElemType_U m_2, ElemType_U s_2) :H_2(h_2), M_2(m_2), S_2(s_2), TimeVal_1(h_1, m_1, s_1) {};
ElemType_U GetResult();
TimeInterval_1 TimeVal_1;
private:
ElemType_U H_2, M_2, S_2;
};
inline ElemType_U TimeInterval_2::GetResult()
{
return (H_2 - TimeVal_1.H_1) * 3600 + (M_2 - TimeVal_1.M_1) * 60 + (S_2 - TimeVal_1.S_1);
}
int main()
{
ElemType_U h_1, m_1, s_1, h_2, m_2, s_2;
ElemType_C C_1, C_2, c_1, c_2;
cin >> h_1 >> C_1 >> m_1 >> C_2 >> s_1;
cin >> h_2 >> c_1 >> m_2 >> c_2 >> s_2;
TimeInterval_2 Time(h_1, m_1, s_1, h_2, m_2, s_2);
cout << Time.GetResult();
return 0;
}