Windows Message Queue

Message queue is the basic fundamental of windows system. For each process, the system maintains a message queue. If something happens to this process, such as mouse click, text change, the system will add a message to the queue. Meanwhile, the process will do a loop for getting message from the queue according to the priority value if it is not empty. Note that the less priority value means the higher priority. In this problem, you are asked to simulate the message queue for putting messages to and getting message from the message queue.
Input
There’s only one test case in the input. Each line is a command, “GET” or “PUT”, which means getting message or putting message. If the command is “PUT”, there’re one string means the message name and two integer means the parameter and priority followed by. There will be at most 60000 command. Note that one message can appear twice or more and if two messages have the same priority, the one comes first will be processed first.(i.e., FIFO for the same priority.) Process to the end-of-file.
Output
For each “GET” command, output the command getting from the message queue with the name and parameter in one line. If there’s no message in the queue, output “EMPTY QUEUE!”. There’s no output for “PUT” command.
Sample Input
GET
PUT msg1 10 5
PUT msg2 10 4
GET
GET
GET
Sample Output
EMPTY QUEUE!
msg2 10
msg1 10
EMPTY QUEUE!
翻译:
消息队列是windows系统的基本(basic fundamental)。对于每个程序,系统都会有一个消息队列。如果这个程序被操作,比如鼠标点击,文本(text)修改,系统将会给队列发送消息。与此同时,在非空的情况下,程序将会根据优先级值(priority value)从队列中不断得到消息。注意,优先级值越低表示的优先级越高。在这个问题中,你要模仿接收消息和发送消息的队列。

输入

输入项中只有一种情况。每一行是一个指令,接受或发送,即接收消息和发送消息。如果指令是发送,则会有一个表示消息名称的字符串还有两个整数,一个表示参数(parameter),一个表示优先权(priority)最多会有60000条指令。注意,一条消息能够出现两次或者更多,如果两条消息具有同样的优先权,那么先接收的那条将被执行(IE,FIFO都是这样的)。直至文件结束。

输出

对于每条接受指令,从消息队列中输出一行消息名和参数。如果在队列中没有消息,那么输出空队列。对于接受指令则没有输出。
这道题用结构体先把数据定义好,然后根据GET或者PUT来输出输入。

#include<stdio.h>
#include<string.h>
#include<queue>

using namespace std;

struct edd
{
    char name[10];
    int num,id,no;
    friend bool operator < (edd a,edd b)
    {
        if(a.no!=b.no) return a.no>b.no;
        return a.id>b.id;
    }
};
priority_queue<edd> q;

int main()
{
    char str[10];
    int i=0;
    while(~scanf("%s",str))
    {
        edd temp;
        if(strcmp(str,"GET")==0)
        {
            if(!q.empty())
            {
                printf("%s %d\n",q.top().name,q.top().num);
                q.pop();
            }
            else
            {
                 printf("EMPTY QUEUE!\n");  
            }
        }
        else
        {
            scanf("%s%d%d",temp.name,&temp.num,&temp.no);
            temp.id=++i;
            q.push(temp);
        }
    }
    return 0;
 } 

附上队列的知识:
头文件:

#include<queue>

声明方法:
1,普通声明:

queue<int>q;

2,结构体声明:

struct edd
{
    int x,y;
    };
    queue<edd>q;
基本操作:
push(x) 将x压入队列的末端

pop() 弹出队列的第一个元素(队顶元素),注意此函数并不返回任何值

front() 返回第一个元素(队顶元素)

back() 返回最后被压入的元素(队尾元素)

empty() 当队列为空时,返回true

size() 返回队列的长度
STL priority_queue的使用方法:
优先队列与队列一样,只能从队尾插入元素,从队首删除元素。但是队列中最大的元素总是位于队首,所以出队的顺序有所改变。但是可以用“<”来重新定义比较规则。
基本操作:
empty() 如果队列为空返回真

pop() 删除对顶元素

push() 加入一个元素

size() 返回优先队列中拥有的元素个数

top() 返回优先队列对顶元素
声明方法:

priority_queue<int>q;

//通过操作,按照元素从大到小的顺序出队。

priority_queue<int,vextor<int>,greater<int>>q;

//通过操作,按照元素从小到大的顺序出队。
自定义出队:

struct cmp{

oprator bool()(int x,int y)
    {return x>y;}
    };
    //x小的优先级高
struct node {     
  int x, y;     
  friend bool operator < (node a, node b)     
  {         
    return a.x > b.x;    //结构体中,x小的优先级高     
  }
};
priority_queue<node>q;   //定义方法
//在该结构中,y为值, x为优先级。
//通过自定义operator<操作符来比较元素中的优先级。
//在重载”<”时,最好不要重载”>”,可能会发生编译错误
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值