10-多继承
题目描述
已有一个日期类Date,包括三个protected成员数据year,month,day;
另有一个时间类Time,包括三个protected成员数据hour,minute,second,12小时制;
现需根据输入的日程的日期时间,安排前后顺序,为此以Date类和Time类为基类,建立一个日程类Schedule,包括以下新增成员:
int ID;//日程的ID
定义友元函数bool before(const Schedule & s1,const Schedule & s2);//判断日程s1时间是否早于日程s2。
编写主函数,根据输入的各项日程信息,建立日程对象,找出需要最早安排的日程(日期和时间相等时,输出较早建立的日程),并输出该日程对象的信息。
输入
测试输入包含若干日程,每个日程占一行(日程ID 日程日期 日程时间)。
当读入0时输入结束,相应的结果不要输出。
输出
时间最靠前的日程
1 2019 6 27 8 0 1
2 2019 6 28 8 0 1
3 2020 1 1 8 0 0
0
The urgent schedule is No.1: 2019/06/27 08:00:01
#include<iostream>
#include<iomanip>
using namespace std;
class date
{
protected:
int year,month,day;
public:
date():year(0),month(0),day(0){}
date(int y,int m,int d):year(y),month(m),day(d){}
};
class time
{
protected:
int hour,minute,second;
public:
time():hour(0),minute(0),second(0){}
time(int h,int m,int s):hour(h),minute(m),second(s){}
};
class schedule:public date,public time
{
int id;
public:
schedule(int i,int y,int m,int d,int h,int min,int s):id(i),date(y,m,d),time(h,min,s){}
friend bool before(const schedule &s1,const schedule &s2);//友元函数比较两个日期早晚
void print()
{
cout<<"The urgent schedule is No."<<id<<": "<<year<<"/";
cout<<setfill('0')<<setw(2)<<month<<"/"<<setfill('0')<<setw(2)<<day;//自动补0
cout<<" "<<setfill('0')<<setw(2)<<hour<<":"<<setfill('0')<<setw(2)<<minute<<":"<<setfill('0')<<setw(2)<<second<<endl;
}
};
bool before(const schedule &s1,const schedule &s2)
{
double sum1,sum2;
sum1=s1.year*10000+s1.month*100+s1.day+s1.hour*0.36+s1.month*0.006+s1.second*0.0001;
sum2=s2.year*10000+s2.month*100+s2.day+s2.hour*0.36+s2.month*0.006+s2.second*0.0001;
if(sum1<sum2)
return true;
else
return false;
}
int main()
{
int i;
schedule Smax(10000,10000,0,0,0,0,0);//先把时间设置的够大
while(cin>>i)
{
if(i==0)
break;
else
{
int y,m,d,h,min,s;
cin>>y>>m>>d>>h>>min>>s;
schedule S(i,y,m,d,h,min,s);
if(before(S,Smax)==true)
Smax=S;//找出最小的时间
}
}
Smax.print();
return 0;
}