#include <Windows.h>
#include <sstream>
// 定义单循环链表节点结构
struct Node {
int data;
Node* next;
Node(int value) : data(value), next(NULL) {}
};
// 创建单循环链表
Node* createCircularList(int n) {
Node* head = new Node(1);
Node* current = head;
for (int i = 2; i <= n; ++i) {
current->next = new Node(i);
current = current->next;
}
current->next = head; // 使链表成为循环链表
return head;
}
// 猴子选大王算法(单循环链表实现)
std::string monkeyKingSelection(int n, int m) {
if (n <= 0 || m <= 0) return "";
Node* head = createCircularList(n);
Node* prev = NULL;
Node* current = head;
std::string result;
while (current->next != current) {
for (int i = 1; i < m; ++i) {
prev = current;
current = current->next;
}
std::ostringstream oss;
oss << current->data;
result += oss.str() + " ";
// 删除当前节点
if (prev) {
prev->next = current->next;
} else {
// 如果prev为空,说明要删除的是头节点
Node* temp = current;
while (temp->next != current) {
temp = temp->next;
}
temp->next = current->next; // 更新尾节点的next指向新的头节点
head = current->next; // 更新头节点
}
Node* toDelete = current;
current = current->next;
delete toDelete;
}
std::ostringstream oss;
oss << current->data;
result += oss.str();
delete current;
return result;
}
// 窗口过程函数
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
static HWND hEdit1, hEdit2, hButton, hOutput;
static HWND hLabel1, hLabel2;
switch (msg) {
case WM_CREATE:
// 创建第一个标签
hLabel1 = CreateWindow("STATIC", "个数:",
WS_CHILD | WS_VISIBLE,
10, 10, 50, 20, hwnd, NULL, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
// 创建第一个文本框
hEdit1 = CreateWindow("EDIT", "",
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT,
60, 10, 100, 20, hwnd, (HMENU)1, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
// 创建第二个标签
hLabel2 = CreateWindow("STATIC", "步数:",
WS_CHILD | WS_VISIBLE,
10, 40, 50, 20, hwnd, NULL, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
// 创建第二个文本框
hEdit2 = CreateWindow("EDIT", "",
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT,
60, 40, 100, 20, hwnd, (HMENU)2, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
// 创建确定按钮
hButton = CreateWindow("BUTTON", "确定",
WS_CHILD | WS_VISIBLE,
10, 70, 80, 30, hwnd, (HMENU)3, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
// 创建输出文本框
hOutput = CreateWindow("STATIC", "",
WS_CHILD | WS_VISIBLE | WS_BORDER,
10, 110, 250, 50, hwnd, (HMENU)4, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
break;
case WM_COMMAND:
if (LOWORD(wParam) == 3) { // 点击确定按钮
char buffer1[256], buffer2[256];
GetWindowText(hEdit1, buffer1, sizeof(buffer1));
GetWindowText(hEdit2, buffer2, sizeof(buffer2));
int n = atoi(buffer1);
int m = atoi(buffer2);
std::string result = monkeyKingSelection(n, m);
SetWindowTextA(hOutput, result.c_str());
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
// 主函数,窗体程序的进入点
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// 注册窗口类
WNDCLASS wc = {};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = "MyWindowClass";
RegisterClass(&wc);
// 创建窗口
HWND hwnd = CreateWindow(wc.lpszClassName, "猴子选大王问题", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 300, 200, NULL, NULL, hInstance, NULL);
// 显示窗口
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// 消息循环
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
阐述项目分析的过程、解决问题的思路(必要时可加入图表)和建模所用的数据结构,给出具体的数据类型定义及相关注释说明、各模块的函数声明及功能说明。