17、提醒队列

问题描述 :

 

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

实验内容:

设计一个类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个操作的描述。

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

 

输入范例 :

1
2010 1 1 9 0 0 go to class
1
2010 1 1 7 0 0 get up
1
2010 1 1 7 0 0 call zhang san
3
2
2
3

输出范例 :

3 notes:
2010-01-01 07:00:00 get up
2010-01-01 07:00:00 call zhang san
2010-01-01 09:00:00 go to class
2010-01-01 07:00:00 get up
2010-01-01 07:00:00 call zhang san
1 notes:
2010-01-01 09:00:00 go to class

解题代码: 

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<algorithm>
#include<set>
#include<vector>
using namespace std;
class Note
{
public:
    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)
    {
        event=new char[200];
        strcpy(event,msg);
        Note::year=year;
        Note::month=month;
        Note::day=day;
        Note::hour=hour;
        Note::minute=minute;
        Note::second=second;
    }
    Note()
    {
        ;
    }
    Note(const Note & note)
    {
        Note::event=new char[100];
        strcpy(Note::event,note.event);
        Note::year=note.year;
        Note::month=note.month;
        Note::day=note.day;
        Note::hour=note.hour;
        Note::minute=note.minute;
        Note::second=note.second;
    }
    ~Note()
    {
       // delete event ;
    }
    ShowNote()
    {
        printf("%02d-%02d-%02d %02d:%02d:%02d %s\n",year,month,day,hour,minute,second,event);
    }

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

};
bool cmp(Note a,Note b)
{
    if(a.year==b.year)
    {
        if(a.month==b.month)
        {
            if(a.day==b.day)
            {
                if(a.hour==b.hour)
                {
                    if(a.minute==b.minute)
                    {
                        return a.second<b.second;
                    }
                    else
                        return a.minute<b.minute;
                }
                else
                    return a.hour<b.hour;
            }
            else
                return a.day<b.day;
        }
        else
            return a.month<b.month;
    }
    else
        return a.year<b.year;
}
class NoteList
{
private:
    vector<Note>a;
public:
    void AddNote(Note n)///:插入提醒信息(插入后保持按时间升序排序)
    {
        a.push_back(n);
        sort(a.begin(),a.end(),cmp);
    }
    Note DeleteFirst()///: 删除数组中第0个提醒信息,并将其作为函数值返回
    {
        Note hc=a[0];
        a.erase(a.begin(),a.begin()+1);
        return hc;
    }
    void PrintAll()///:输出所有提醒信息
    {
        int i;
        for(i=0;i<a.size();i++)
            a[i].ShowNote();
    }
    int GetNoteCount()///:获得提醒信息的数量
    {
        return a.size();
    }
};

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;
}

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值