PTA 7-2 日程安排(多重继承+重载)

已有一个日期类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>
#include <stdio.h>
using namespace std;
class Data {
protected:
	int year;
	int month;
	int day;
public:
	Data() {}
	Data(int y, int mo, int d) {
		year = y;
		month = mo;
		day = d;
	}
	void printdata() {
		cout << " " << year << "/" << month << "/" << day;
	}
};
class Time {
protected:
	int hour;
	int minute;
	int second;
public:
	Time() {}
	Time(int h, int mi, int s) {
		hour = h;
		minute = mi;
		second = s;
	}
	void printtime() {
		cout << " " << hour << ":" << minute << ":" << second;
	}
};
class Schedule :public Data, public Time {

	int ID;
public:
	Schedule() {}
	Schedule(int y, int mo, int d, int h, int mi, int s, int id) :Data(y, mo, d), Time(h, mi, s) {
		ID = id;
	}
	void printschedule() {
		cout << "No." << ID << ":";
		Data::printdata();
		Time::printtime();
	}
	bool operator<(const Schedule& s2);
};
bool Schedule::operator<(const Schedule& s2) {
	if (year < s2.year) {
		return 1;
    }
	else if (year > s2.year){
		return 0;
    }
	else {
		if (month < s2.month){
			return 1;
        }
		else if (month > s2.month) {
			return 0;
        }
		else {
			if (day < s2.day){ 
				return 1;
            }
			else if (day > s2.day) {
				return 0;
            }
			else {
				if (hour < s2.hour){ 
					return 1;
                }
				else if (hour > s2.hour) {
					return 0;
                }
				else {
					if (minute < s2.minute) {
						return 1;
                    }
					else if (minute > s2.minute) {
						return 0;
                    }
					else {
						if (second < s2.second){ 
							return 1;
                        }
						else{
							return 0;
                        }

					}
				}
			}
		}
	}
}
int main() {


	int id, y, mon, d, h, min, s, i = 0;
	Schedule s2(9999, 9999, 9999, 999, 999, 99,0);
	while (1)
	{
		cin >> id;
		if (id == 0)
			break;
		i++;
		scanf("%d/%d/%d", &y, &mon, &d);
		scanf("%d:%d:%d", &h, &min, &s);
		Schedule s1(y, mon, d, h, min, s,id);
		if (s1 < s2)
			s2 = s1;
	}
	if (i != 0)
	{
		cout << "The urgent schedule is "; s2.printschedule();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值