一、题目描述
小明同学有着严重的拖延症,每次老师布置的作业都要到快要截止的时候才会开始动手完成,因此现在有着许许多多的作业完成。你是小明的好朋友,请帮小明找出最紧急的作业(即最早截止的作业)。
要求如下:
1.定义一个日期类Date,包括三个protected成员数据year, month, day;
2.定义一个时间类Time,包括三个protected成员数据hour, minute, second(24小时制);
3.以Date类和Time类为基类,创建一个作业类Work,包括新增成员:int id; // 作业的id
4.定义一个友元函数bool before(const Work& w1,const Work& w2); // 判断作业w1的时间是否早于作业w2的时间。
二、输入与输出
1.输入
输入若干作业,每个作业占一行(作业id 年 月 日 时 分 秒)
当输入0时结束,相应的结果不要输出。
1 2021 9 25 4 5 6
2 2020 6 13 5 7 8
3 2021 8 21 16 7 9
5 2022 7 8 9 10 11
4 2021 7 26 15 25 30
0
2.输出
时间最靠前的作业。
The urgent Work is No.2: 2020/06/13 05:07:08
三、参考代码
#include <iostream>
#include <vector>
#include<string>
#include<cmath>
#include <memory>
#include<iomanip>
using namespace std;
class Date
{
protected:
int year;
int mon;
int day;
public:
Date(int y,int m,int d):year(y),mon(m),day(d){}
int getdate()
{
return year * 1000 + mon * 100 + day;
}
};
class Time
{
protected:
int hour;
int min;
int sec;
public:
Time(int h,int m,int s):hour(h),min(m),sec(s){}
};
class Work :public Date, public Time
{
int id;
public:
Work(int y, int m, int d, int h, int m1, int s,int num) :Date(y, m, d), Time(h, m1, s)
{
id = num;
}
friend bool before(const Work& a, const Work& b) {
int ad = a.year * 10000 + a.mon * 100 + a.day;
int at = a.hour * 10000 + a.min * 100 + a.sec;
int bd = b.year * 10000 + b.mon * 100 + b.day;
int bt = b.hour * 10000 + b.min * 100 + b.sec;
if (ad < bd)
{
return 1;
}
else if (ad > bd)
{
return 0;
}
else if (ad == bd)
{
if (at < bt)
{
return 1;
}
else
{
return 0;
}
}
};
void pri()
{
cout <<"The urgent Work is No."<<id<<": "<< year << "/" << setw(2) << setfill('0') << mon << "/" << day << " " << setw(2) << setfill('0') << hour << ":" << setw(2) << setfill('0') << min << ":" << setw(2) << setfill('0') << sec << endl;
}
};
int main()
{
int id;
int y, m, d, h, m1, s;
cin >>id>> y >> m >> d>>h >> m1 >> s;
Work a(y, m, d, h,m1, s, id);
while (cin>>id)
{
cin>> y >> m >> d >> h >> m1 >> s;
Work b(y, m, d, h, m1, s, id);
if (before(b,a))
{
a = b;
}
}
a.pri();
}