日程安排(多重继承+重载)

已有一个日期类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

#include <iostream>
using namespace std;

class Date {
	public:
		Date(int y, int mo, int d): year(y), month(mo), day(d) {
		}
	protected:
		int year;
		int month;
		int day;
};

class Time {
	protected:
		int hour;
		int minute;
		int second;
	public:
		Time(int h, int mi, int s): hour(h), minute(mi), second(s) {
		}
};

class Schedule: public Date, public Time {
		int ID;
	public:
		Schedule();
		Schedule(int y, int mo, int d, int h, int mi, int s, int id): Date(y, mo, d), Time(h, mi, s) {
			ID = id;
		}
		bool operator<(const Schedule &s2) {
			if (year < s2.year) {
				return 1;
			} else if (year == s2.year) {
				if (month < s2.month)
					return 1;
				else if (month == s2.month) {
					if (day < s2.day)
						return 1;
					else if (day == s2.day) {
						if (hour < s2.hour)
							return 1;
						else if (hour == s2.hour) {
							if (minute < s2.minute)
								return 1;
							if (second < s2.second)
								return 1;
						}
					}
				}
			}
			return 0;
		}

		void disp() {
			cout << "The urgent schedule is No." << ID << ':' << " " << year << '/' << month << '/' << day << ' ' << hour << ':' <<
			     minute
			     << ':' << second <<
			     endl;
		}
};

int main() {
	int id, i;
	int y, mo, d, h, mi, s;
	char op1, op2, op3, op4;
	Schedule *sche[100];
	Schedule *early;
	int count = 0;
	cin >> id;
	while (id != 0) {
		cin >> y >> op1 >> mo >> op2 >> d >> h >> op3 >> mi >> op4 >> s;
		sche[count++] = new Schedule(y, mo, d, h, mi, s, id);
		cin >> id;
	}
	early = sche[0];

	for (i = 1; i < count; i++) {
		if ((*sche[i] < *early)) {
			early = sche[i];
		}
	}
	early->disp();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值