【东华大学oj】20 提醒队列(面向对象)

本文介绍了如何在C++中设计一个包含时间和事件的Note类,以及基于Note类构建的提醒列表类NoteList,包括构造函数、析构函数、拷贝构造、重载等于号和排序功能的实现。
摘要由CSDN通过智能技术生成

20 提醒队列

作者: Turbo时间限制: 1S章节: 类与对象

问题描述 :

实验目的:学习拷贝构造函数、析构函数的使用以及重载等于号。

实验内容:

设计一个类Note(提醒),包含时间和事件内容,时间由年月日时分秒组成,事件内容用字符串表示。

类的编码大致如下:

class Note

{

int year, month, day, hour, minute, second;

char *event;  //本题要求event必须为一个字符串指针,不能为字符数组或string字符串

//以下只说明了两个构造函数,可根据需要自己增加构造函数的重载

Note(char * msg, int year, int month, int day, int hour=0, int minute=0, int second=0);

Note(const Note & note);

~Note();

//还需要重载等于号,还可根据需要设计ShowNote()方法,用来显示“提醒”的时间和内容

}

再设计一个提醒列表类:NoteList,

私有数据成员包含一个Note数组(如果可能,最好使用vector代替数组)

还有以下成员函数:

插入提醒信息(插入后保持按时间升序排序,如果两个提醒信息时间完全相同,则后插入的排在后)

删除数组中第0个提醒信息并作为函数值返回(因为是按时间排序,所以第0个肯定是时间最早的一个)

输出所有提醒信息

获得提醒信息的数量

NoteList的成员大致为:

数据成员:

一个Note数组:noteArr

一个int成员:noteCount

函数成员:

构造函数

AddNote(Note n):插入提醒信息(插入后保持按时间升序排序)

Note DeleteFirst(): 删除数组中第0个提醒信息,并将其作为函数值返回

PrintAll():输出所有提醒信息

GetNoteCount():获得提醒信息的数量

可根据自己需要再增加其它成员。

main函数参考如下代码:

int main()
{
 char eventMsg[100];
 Note n2;
 int year,month,day,hour,minute,second;
 int i,n;
 NoteList notes;
 int op;
    while (cin >> op)
    {
        switch (op)
        {
            case 1:
            {
                cin>>year;
                cin>>month;
                cin>>day;
                cin>>hour;
                cin>>minute;
                cin>>second;
                cin.get();
                cin.getline(eventMsg,90);
                Note nt(eventMsg,year,month,day,hour,minute,second);
                notes.AddNote(nt);
                break;
            }
            case 2:
            {
                if (notes.GetNoteCount()>0)
                {
                    Note nt=notes.DeleteFirst();
                    nt.ShowNote();
                }               
                break;
            }
            case 3:
            {
                int cnt = notes.GetNoteCount();
                cout<<cnt<<" notes:"<<endl;
                notes.PrintAll();
                break;
            }
        }
    }
 return 0;
}

输入说明 :

可输入多组测试数据,每组测试数据包含一行或者两行:

第一行输入一个操作的种类:

1:输入一个提醒信息并插入

2:输出并删除提醒列表中时间最早的信息

3:输出所有提醒信息。

第二行输入所需要的参数:

对于第1个操作,第二行输入年月日时分秒及提醒信息字符串,以一个空格分隔各数据,提醒信息中可包含空格。

对于第2个操作和第3个操作,不需要输入额外数据。

输入中无多余空格或空行,两组测试数据之间也无空行。

输出说明 :

第1个操作无输出。

第2个操作输出一个提醒事件的具体信息,格式如下:

2020-01-01 08:10:10 go to class

其中,年月日之间以一个减号分隔,时分秒之间以一个冒号分隔,日期后及秒钟后跟一个空格,最后是提醒事件的内容(字符串),注意不要包含多余空格。

月日及时分秒的输出均占两位,如果不足两位,在前面补“0”。

第3个操作输出所有提醒事件的信息,格式如下:

2 notes:

2020-01-01 08:10:10 go to class

2020-01-01 10:00:00 go to library

第一行输出共有多少个notes,然后跟n行,

每个提醒信息占一行,输出格式见第2个操作的描述。

输出中无多余空格,也无多余空行。

#include <iostream>
#include <vector>
#include <cstring>
#include <iomanip>
#include <algorithm>
using namespace std;
class Note {
private:
    int year, month, day, hour, minute, second;
    char* event;

public:
    // 主构造函数
    Note(char *msg, int y, int mo, int d, int h = 0, int mi = 0, int s = 0)
        : year(y), month(mo), day(d), hour(h), minute(mi), second(s) {
        event = new char[strlen(msg) + 1];
        strcpy(event, msg);
    }

    // 拷贝构造函数
    Note(const Note &note) {
        year = note.year;
        month = note.month;
        day = note.day;
        hour = note.hour;
        minute = note.minute;
        second = note.second;
        event = new char[strlen(note.event) + 1];
        strcpy(event, note.event);
    }

    // 析构函数
    ~Note() {
        delete[] event;
    }

    // 重载等于号
    Note& operator=(const Note &rhs) {
        if (this != &rhs) {
            delete[] event;
            year = rhs.year;
            month = rhs.month;
            day = rhs.day;
            hour = rhs.hour;
            minute = rhs.minute;
            second = rhs.second;
            event = new char[strlen(rhs.event) + 1];
            strcpy(event, rhs.event);
        }
        return *this;
    }

    // 显示Note信息
    void ShowNote() const {
        cout << setfill('0') << setw(4) << 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 << " "
                  << event <<endl;
    }

    // 为了排序和比较,需要定义时间的比较函数
    bool isEarlierThan(const Note &other) const {
        if (year != other.year)
            return year < other.year;
        if (month != other.month)
            return month < other.month;
        if (day != other.day)
            return day < other.day;
        if (hour != other.hour)
            return hour < other.hour;
        if (minute != other.minute)
            return minute < other.minute;
        return second < other.second;
    }
};

// NoteList定义
class NoteList {
private:
    vector<Note> noteArr;

public:
    // 构造函数
    NoteList() {} // 插入提醒信息
    void AddNote(Note n) {
        noteArr.push_back(n);
        sort(noteArr.begin(), noteArr.end(),
            [](const Note &a, const Note &b) { return a.isEarlierThan(b); });
    }

    // 删除并返回第一个提醒信息
    Note DeleteFirst() {
        if (noteArr.empty()) {
            throw out_of_range("No notes to delete");
        }
        Note first = noteArr.front();
        noteArr.erase(noteArr.begin());
        return first;
    }

    // 输出所有提醒信息
    void PrintAll() const {
        for (const auto &note : noteArr) {
            note.ShowNote();
        }
    }

    // 获取提醒信息的数量
    int GetNoteCount() const {
        return noteArr.size();
    }
};

int main()
{
 char eventMsg[100];

 int year,month,day,hour,minute,second;

 NoteList notes;
 int op;
    while (cin >> op)
    {
        switch (op)
        {
            case 1:
            {
                cin>>year;
                cin>>month;
                cin>>day;
                cin>>hour;
                cin>>minute;
                cin>>second;
                cin.get();
                cin.getline(eventMsg,90);
                Note nt(eventMsg,year,month,day,hour,minute,second);
                notes.AddNote(nt);
                break;
            }
            case 2:
            {
                if (notes.GetNoteCount()>0)
                {
                    Note nt=notes.DeleteFirst();
                    nt.ShowNote();
                }
                break;
            }
            case 3:
            {
                int cnt = notes.GetNoteCount();
                cout<<cnt<<" notes:"<<endl;
                notes.PrintAll();
                break;
            }
        }
    }
 return 0;
}

  • 27
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ixll625

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值