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

已有一个日期类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{
protected:
    int year;
    int month;
    int day;
public:
    Date(int year, int month, int day) : year(year), month(month), day(day) {}

public:
    int toIntData(){
        return year*10000+month*100+day;
    }
    void showdate(){cout<<" "<<year<<"/"<<month<<"/"<<day;}
};
class Time{
protected:
    int hour;
    int minute;
    int second;
public:
    Time(int hour, int minute, int second) : hour(hour), minute(minute), second(second) {}

public:
    int toIntTime(){
        return hour*10000+minute*100+second;
    }
    void showtime(){cout<<" "<<hour<<":"<<minute<<":"<<second;}
};
class Schedule:public Date,Time{
protected:
    int id;
public:
    Schedule(int id,int year, int month, int day, int hour, int minute, int second) : Date(year, month, day),
                                                                                       Time(hour, minute, second),
                                                                                       id(id) {}//构造函数

public:
    bool operator < (Schedule s2){
        if (this->toIntData()!=s2.toIntData()){
            return toIntData()<s2.toIntData();
        } else{
            return toIntTime()<s2.toIntTime();
        }
    }
    void show(){
        cout<<"No."<<id<<":";
        Date::showdate();
        Time::showtime();
    }
};
int main(){
    int n;
    int a,b,c,d,e,f;
    Schedule s2(0,9999,9999,9999,999,999,99);
    while (cin>>n,n!=0){
        scanf("%d/%d/%d",&a,&b,&c);
        scanf("%d:%d:%d",&d,&e,&f);
        Schedule s1(n,a,b,c,d,e,f);
        if(s1<s2){
            s2=s1;
        }
    }
    cout<<"The urgent schedule is ";s2.show();

}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值