C++设计一个高铁类,可以向旅客提醒该车的出发时间,到达时间,并计算该车的旅途行驶时间
(到达时间减出发时间)。假定火车均为24小时内到达。请根据运行结果和Clock类的实现,完成后面的TrainTrip类及main函数。
类都给你写好了啊,补充完整代码就可以了。
运行结果:
代码:
#include <iostream>
#include <string>
using namespace std;
class Clock
{
private:
int H, M, S;
public:
void showTime()
{
cout << H << ":" << M << ":" << S << endl;
}
void SetTime(int H = 0, int M = 0, int S = 0)
{
this->H = H;
this->M = M;
this->S = S;
}
Clock(int H = 0, int M = 0, int S = 0)
{
this->H = H;
this->M = M;
this->S = S;
}
int GetH() { return H; }
int GetM() { return M; }
int GetS() { return S; }
};
class TrainTrip
{
private:
char* TrainNo;//车次
Clock StartTime;//出发时间
Clock EndTime;//到达时间
public:
TrainTrip(const char* p, Clock start, Clock end)
{
TrainNo = new char[strlen(p) + 1];
strcpy(TrainNo, p);
TrainNo[strlen(p)] = 0;
this->StartTime.SetTime(start.GetH(), start.GetM(), start.GetS());
this->EndTime.SetTime(end.GetH(), end.GetM(), end.GetS());
}
Clock TripTime()
{
Clock t;
int h = EndTime.GetH() - StartTime.GetH();
int m = EndTime.GetM() - StartTime.GetM();
int s = EndTime.GetS() - StartTime.GetS();
if (s < 0)
{
s += 60;
m -= 1;
}
if (m < 0)
{
m += 60;
h -= 1;
}
if (h < 0)
h += 24;
t.SetTime(h, m, s);
return t;
}
};
int main()
{
Clock start(8, 10, 10);
Clock end(6, 1, 2);
TrainTrip trip("复兴号", start, end);
cout << "出发时间:";
start.showTime();
cout << "到达时间:";
end.showTime();
cout << "旅途行驶时间:";
Clock t = trip.TripTime();
t.showTime();
return 0;
}