已有一个日期类Date,包括三个protected成员数据 int year; int month; int day; 另有一个时间类Time,包括三个protected成员数据 int hour; int minute; int second; 现需根据输入的日程的日期时间,安排前后顺序,为此以Date类和Time类为基类,建立一个日程类Schedule,包括以下新增成员: int ID;//日程的ID bool operator < (const Schedule & s2);//判断当前日程时间是否早于s2 生成以上类,并编写主函数,根据输入的各项日程信息,建立日程对象,找出需要最早安排的日程,并输出该日程对象的信息。 输入格式: 测试输入包含若干日程,每个日程占一行(日程编号ID 日程日期(****//)日程时间(::**))。当读入0时输入结束,相应的结果不要输出。 输入样例: 1 2014/06/27 08:00:01 2 2014/06/28 08:00:01 0 输出样例: The urgent schedule is No.1: 2014/6/27 8:0:1 代码长度限制 16 KB 时间限制 400 ms
时间: 2023-11-27 16:53:28 浏览: 119
日期类data
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
class Date {
protected:
int year;
int month;
int day;
public:
Date(int y = 0, int m = 0, int d = 0) : year(y), month(m), day(d) {};
bool operator < (const Date & d2) const {
if (year != d2.year) return year < d2.year;
else if (month != d2.month) return month < d2.month;
else return day < d2.day;
}
};
class Time {
protected:
int hour;
int minute;
int second;
public:
Time(int h = 0, int m = 0, int s = 0) : hour(h), minute(m), second(s) {};
bool operator < (const Time & t2) const {
if (hour != t2.hour) return hour < t2.hour;
else if (minute != t2.minute) return minute < t2.minute;
else return second < t2.second;
}
};
class Schedule : public Date, public Time {
private:
int ID;
public:
Schedule(int id = 0, int y = 0, int m = 0, int d = 0, int h = 0, int mi = 0, int s = 0)
: ID(id), Date(y, m, d), Time(h, mi, s) {};
bool operator < (const Schedule & s2) {
if (Date::operator<(s2)) return true;
else if (Date::operator>(s2)) return false;
else {
if (Time::operator<(s2)) return true;
else return false;
}
}
void print() {
cout << "The urgent schedule is No." << ID << ": ";
cout << setw(4) << setfill('0') << year << "/" << setw(2) << setfill('0') << month << "/" << setw(2) << setfill('0') << day << " ";
cout << setw(2) << setfill('0') << hour << ":" << setw(2) << setfill('0') << minute << ":" << setw(2) << setfill('0') << second << endl;
}
};
int main() {
int id, y, m, d, h, mi, s;
vector<Schedule> sch;
while (cin >> id && id != 0) {
char c;
cin >> y >> c >> m >> c >> d >> h >> c >> mi >> c >> s;
Schedule t(id, y, m, d, h, mi, s);
sch.push_back(t);
}
sort(sch.begin(), sch.end());
sch[0].print();
return 0;
}
```
阅读全文