题目描述
小明同学有着严重的拖延症,每次老师布置的作业都要到快要截止的时候才会开始动手完成,因此现在有着许许多多的作业完成。你是小明的好朋友,请帮小明找出最紧急的作业(即最早截止的作业)。
要求如下:
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的时间。
输入
输入若干作业,每个作业占一行(作业id 年 月 日 时 分 秒)
当输入0时结束,相应的结果不要输出。
输出
时间最靠前的作业。
样例查看模式
正常显示查看格式
输入样例1 <-复制
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
输出样例1
The urgent Work is No.2: 2020/06/13 05:07:08
代码部分:
#include<iostream>
#include<iomanip>
#include<cstring>
#include <limits>
#include<cmath>
using namespace std;
class date
{
protected:
int year, month, day;
public:
date() {}
date(int y, int m, int d) :year(y), month(m), day(d) {}
};
class time
{
protected:
int hour, minute, second;
public:
time() {}
time(int h, int m, int s) :hour(h), minute(m), second(s) {}
};
class work :public date, time
{
protected:
int id;
public:
work() {}
work(int id1, int y, int m, int d, int h, int m1, int s) :date(y, m, d), time(h, m1, s)
{
id = id1;
}
friend bool before(const work& w1, const work& w2);
void input()
{
cout << "The urgent Work is No." << id << ": "
<<year << "/" << setfill('0') << setw(2) << month << "/"
<< setfill('0') << setw(2) << day << " "
<< setfill('0') << setw(2) << hour << ":"
<< setfill('0') << setw(2) <<minute << ":"
<< setfill('0') << setw(2) << second << endl;
}
};
bool before(const work& w1, const work& w2)
{
if (w1.year < w2.year) return true;//说明w1的要更早交
else if (w1.year == w2.year && w1.month < w2.month) return true;
else if (w1.year == w2.year && w1.month == w2.month && w1.day < w2.day)return true;
else if (w1.year == w2.year && w1.month == w2.month && w1.day < w2.day && w1.hour < w2.hour)return true;
else if (w1.year == w2.year && w1.month == w2.month && w1.day < w2.day && w1.hour == w2.hour && w1.minute < w2.minute)return true;
else if (w1.year == w2.year && w1.month == w2.month && w1.day < w2.day && w1.hour == w2.hour && w1.minute == w2.minute && w1.second < w2.second)return true;
else return false;
}
int main()
{
int id, year, month, day, hour, minute, second;
work firstwork;//紧急作业
cin >> id >> year >> month >> day >> hour >> minute >> second;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
firstwork = work(id, year, month, day, hour, minute, second);
while (true)
{
cin >> id;
if (id == 0)
{
break;
}
cin >> year >> month >> day >> hour >> minute >> second;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
work w1(id, year, month, day, hour, minute, second);//当前的作业
if (before(w1, firstwork))
{
firstwork = w1;
}
}
firstwork.input();
return 0;
}