H - Windows Message Queue

本文介绍了一个基于Windows系统的消息队列模拟程序。该程序能够处理多达60,000条命令,包括添加(PUT)和获取(GET)消息操作。通过使用优先队列实现了消息的优先级调度和相同优先级下的先进先出(FIFO)原则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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!

按优先度,然后相同的FIFO,不能用vector然后自己排,复杂度太高会T,只能用优先队列;

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h> 
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
#include<stack>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
typedef long double ld;
typedef double db;
const ll mod=1e9+100;
const db e=exp(1);
using namespace std;
const double pi=acos(-1.0);
struct pp
{
    char name[20];
    int num,lev,id;
    friend bool operator <(pp a,pp b)
     {
        if(a.lev ==b.lev)
        return a.id >b.id ;//要加个这个,符合FIFO,不然会错
        return a.lev >b.lev;
      } 
};
priority_queue<pp>v;
void Get()
{
    if(v.empty()) pf("EMPTY QUEUE!\n");
    else{
        pp t=v.top();
        pf("%s %d\n",t.name,t.num);
        v.pop();    
    }
}
int main()
{
    int ans=1;
    while(!v.empty()) v.pop();
    char q[5];
    while(~sf("%s",q))
    {
        if(q[0]=='G')
        Get();
        else
        {
            pp t;
            sf("%s%d%d",t.name,&t.num,&t.lev);
            t.id =ans++;
            v.push(t); 
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/wzl19981116/p/9408144.html

//duobanbeijianrong // Mandatory UF Includes #include <uf.h> #include <uf_object_types.h> // Internal Includes #include <NXOpen/ListingWindow.hxx> #include <NXOpen/NXMessageBox.hxx> #include <NXOpen/UI.hxx> // Internal+External Includes #include <NXOpen/Annotations.hxx> #include <NXOpen/Assemblies_Component.hxx> #include <NXOpen/Assemblies_ComponentAssembly.hxx> #include <NXOpen/Body.hxx> #include <NXOpen/BodyCollection.hxx> #include <NXOpen/Face.hxx> #include <NXOpen/Line.hxx> #include <NXOpen/NXException.hxx> #include <NXOpen/NXObject.hxx> #include <NXOpen/Part.hxx> #include <NXOpen/PartCollection.hxx> #include <NXOpen/Session.hxx> #include "uf_all.h" #include "HuNXOpen.h" #include <algorithm> // 包含std::sort算法 #include <map> #include <set> // 包含std::set容器 #include <vector> // 包含std::vector容器 #include <cmath> // 包含数学函数 #include <limits> // 包含数值极限 #include <memory> // 包含智能指针 #include <queue> // 添加队列支持 // Std C++ Includes #include <iostream> #include <sstream> #include <Windows.h> // 包含所有Windows API定义 #include <cstdio> // 用于sprintf_s using namespace NXOpen; using std::string; using std::exception; using std::stringstream; using std::endl; using std::cout; using std::cerr; //------------------------------------------------------------------------------ // NXOpen c++ test class //------------------------------------------------------------------------------ class MyClass { // class members public: static Session *theSession; static UI *theUI; MyClass(); ~MyClass(); void do_it(); void print(const NXString &); void print(const string &); void print(const char*); private: BasePart *workPart, *displayPart; NXMessageBox *mb; ListingWindow *lw; LogFile *lf; }; //------------------------------------------------------------------------------ // Initialize static variables //------------------------------------------------------------------------------ Session *(MyClass::theSession) = NULL; UI *(MyClass::theUI) = NULL; //------------------------------------------------------------------------------ // Constructor //------------------------------------------------------------------------------ MyClass::MyClass() { // Initialize the NX Open C++ API environment MyClass::theSession = NXOpen::Session::GetSession(); MyClass::theUI = UI::GetUI(); mb = theUI->NXMessageBox(); lw = theSession->ListingWindow(); lf = theSession->LogFile(); workPart = theSession->Parts()->BaseWork(); displayPart = theSession->Parts()->BaseDisplay(); } //------------------------------------------------------------------------------ // Destructor //------------------------------------------------------------------------------ MyClass::~MyClass() { } //------------------------------------------------------------------------------ // Print string to listing window or stdout //------------------------------------------------------------------------------ void MyClass::print(const NXString &msg) { if(! lw->IsOpen() ) lw->Open(); lw->WriteLine(msg); } void MyClass::print(const string &msg) { if(! lw->IsOpen() ) lw->Open(); lw->WriteLine(msg); } void MyClass::print(const char * msg) { if(! lw->IsOpen() ) lw->Open(); lw->WriteLine(msg); } //------------------------------------------------------------------------------ // Do something //------------------------------------------------------------------------------ void MyClass::do_it() { char* release = NULL; // 声明指针,用于接收版本字符串 // 调用UF_get_release获取版本字符串 int result = UF_get_release(&release); // 检查函数调用是否成功 if (result != 0 || release == NULL) { mb->Show("错误", NXMessageBox::DialogTypeError, "获取NX版本失败!"); return; } // 将版本号转换为字符串便于处理 std::string version(release); // 释放由UF_get_release分配的内存 - 必须调用! UF_free(release); // 输出获取的版本信息到列表窗口 print("当前NX版本: " + version); // 定义DLL路径 const char* dllPath = nullptr; // 检查版本是否匹配NX 12.0 if (version.find("NX V12.0") == 0) { print("检测到NX 12.0版本,加载对应工具"); dllPath = "D:\\MICHTOOLS\\Application\\qiaowei_tool_12.0.dll"; } // 检查版本是否匹配NX 2212系列 else if (version.find("NX V2212") == 0 || version.find("V22.12") == 0 || version.find("NX 2212") != std::string::npos) { print("检测到NX 2212系列版本,加载对应工具"); dllPath = "D:\\MICHTOOLS\\Application\\qiaowei_tool_2212.dll"; } else { // 版本不匹配时显示错误对话框 mb->Show("版本错误", NXMessageBox::DialogTypeError, "未找到匹配的工具版本!\n当前版本: " + version); return; // 终止函数执行 } // 加载对应的DLL HMODULE hModule = LoadLibraryA(dllPath); if (hModule == NULL) { DWORD errorCode = GetLastError(); char errorMsg[256]; sprintf_s(errorMsg, "加载DLL失败! 错误代码: %d", errorCode); mb->Show("DLL错误", NXMessageBox::DialogTypeError, errorMsg); return; } // 获取DLL中的入口函数 typedef void (*ToolEntryFunction)(); ToolEntryFunction runTool = (ToolEntryFunction)GetProcAddress(hModule, "run_tool"); if (runTool == NULL) { DWORD errorCode = GetLastError(); char errorMsg[256]; sprintf_s(errorMsg, "获取函数地址失败! 错误代码: %d", errorCode); mb->Show("函数错误", NXMessageBox::DialogTypeError, errorMsg); FreeLibrary(hModule); // 释放DLL return; } print("版本检查通过,开始执行主程序..."); try { // 调用DLL中的函数 runTool(); } catch (...) { mb->Show("执行错误", NXMessageBox::DialogTypeError, "工具执行过程中发生异常!"); } // 释放DLL FreeLibrary(hModule); } // TODO: add your code here //------------------------------------------------------------------------------ // Entry point(s) for unmanaged internal NXOpen C/C++ programs //------------------------------------------------------------------------------ // Explicit Execution extern "C" DllExport void ufusr( char *parm, int *returnCode, int rlen ) { UF_initialize(); //开发的许可函数,初始化 try { // Create NXOpen C++ class instance MyClass *theMyClass; theMyClass = new MyClass(); theMyClass->do_it(); delete theMyClass; } catch (const NXException& e1) { UI::GetUI()->NXMessageBox()->Show("NXException", NXOpen::NXMessageBox::DialogTypeError, e1.Message()); } catch (const exception& e2) { UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, e2.what()); } catch (...) { UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, "Unknown Exception."); }UF_terminate(); //释放许可函数,结束化 } //------------------------------------------------------------------------------ // Unload Handler //------------------------------------------------------------------------------ extern "C" DllExport int ufusr_ask_unload() { return (int)NXOpen::Session::LibraryUnloadOptionImmediately; } 具体放在我代码的什么位置。最好给我一份完整的代码,每行中文注解
最新发布
08-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值