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

  • Post author:
  • Post category:其他


已有一个日期类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 <bits/stdc++.h>
using namespace std;

class Date{
  protected:
    int year, month, day;
  public:
    Date(int y, int m, int d): year(y), month(m), day(d){};    //初始化
};

class Time{
  protected:
    int hour, minute, second;
  public:
    Time(int hh, int mm, int ss): hour(hh), minute(mm), second(ss){};    //初始化
};

class Schedule: public Date, public Time{    //继承基类
  private:
    int ID;
  public:
    Schedule(int i, int y, int m, int d, int hh, int mm, int ss):Date(y, m, d), Time(hh, mm, ss) {
      ID = i;    //初始化
    }
    bool operator < (const Schedule & s2) {    //判断当前日程时间是否早于s2
      if(year<s2.year)return true;
      if(year==s2.year&&month<s2.month)return true;
      if(year==s2.year&&month==s2.month&&day<s2.day)return true;
      if(year==s2.year&&month==s2.month&&day==s2.day&&hour<s2.hour)return true;
      if(year==s2.year&&month==s2.month&&day==s2.day&&hour==s2.hour&&minute<s2.minute)return true;
      if(year==s2.year&&month==s2.month&&day==s2.day&&hour==s2.hour&&minute==s2.minute&&second<s2.second)return true;
      return false;
    }
  void show() {    //输出
    cout<<"The urgent schedule is No."<<ID<<": "<<year<<"/"<<month<<"/"<<day<<" "<<hour<<":"<<minute<<":"<<second<<endl;
  }
};

int main() {
  int i, y, m, d, hh, mm, ss;
  char a;
  cin >> i >> y >> a >> m >> a >> d >> hh >> a >> mm >> a >> ss;
  Schedule s1(i, y, m, d, hh, mm, ss);
  cin >> i;
  int flag=0;
  while(i){      //判断输入0,输入0则结束
    cin >> y >> a >> m >> a >> d >> hh >> a >> mm >> a >> ss;
    Schedule s2=Schedule(i, y, m, d, hh, mm, ss);
    if(s2<s1) {     //若后者s2比前者s1小(日期前),则将s2给s1
      s1=s2;                //s1保存最小(日期早)的值
    };
    cin>>i;
    flag++;    //判断第一行是否直接输入0
  }
  if(flag!=0) {     //若第一行直接为0,则不作任何输出
    s1.show();
  };
  return 0;
}



版权声明:本文为weixin_72707764原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。