7-5 日程安排(多重继承+重载) (40 分)
已有一个日期类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
收获
- 不要忘记输出
- 对cin来说数字中间有非空格字符,要用char ch接受
- operator < 可以看做一个函数名
#include<iostream>
using namespace std;
class Date{
protected:
int year;
int month;
int day;
public:
Date(int a,int b,int c):year(a),month(b),day(c){};
};
class Time{
protected:
int hour;
int minute;
int second;
public:
Time(int a,int b,int c):hour(a),minute(b),second(c){};
};
class Schedule:public Date,public Time{
private:
int ID;
public:
Schedule(int a=0,int b=0,int c=0,int d=0,int e=0,int f=0,int g=0):ID(a),Date(b,c,d),Time(e,f,g){};
bool operator <(const Schedule &s2){
if(year<s2.year){
return true;
}else if(year>s2.year){
return false;
}else if(month<s2.month){
return true;
}else if(month>s2.month){
return false;
}else if(day<s2.day){
return true;
}else if(day>s2.day){
return false;
}else if(hour<s2.hour){
return true;
}else if(hour>s2.hour){
return false;
}else if(minute<s2.minute){
return true;
}else if(minute>s2.minute){
return false;
}else if(second<s2.second){
return true;
}else if(second>s2.second){
return false;
}else{
return true;
}
};
void display(){
cout<<"The urgent schedule is No."<<ID<<": "<<year<<"/"<<month<<"/"<<day<<" "<<hour<<":"<<minute<<":"<<second;
}
};
int main(){
Schedule d1,d2;
int a,b,c,d,e,f,g;
char ch;
cin>>a>>b>>ch>>c>>ch>>d>>e>>ch>>f>>ch>>g;
d1=Schedule(a,b,c,d,e,f,g);
cin>>a;
while(a){
cin>>b>>ch>>c>>ch>>d>>e>>ch>>f>>ch>>g;
d2=Schedule(a,b,c,d,e,f,g);
if(d2.operator<(d1)){
d1=d2;
}
cin>>a;
}
d1.display();
return 0;
}
版权声明:本文为m0_50997138原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。