建立一个windows对话框图形环境应用程序,添加相关编辑框和按钮,点击按钮直接运行固定txt文件(或打开文件对话框,选择 txt文件),统计文件中词的频数,并在图形界面编辑框中显示频数最大10个词,和它的频数。
时间: 2024-03-05 13:48:21 浏览: 45
好的,我可以为您提供一个简单的实现方案,具体步骤如下:
1. 新建一个Windows对话框应用程序,添加一个编辑框和一个按钮控件。
2. 在按钮的单击事件响应函数中,使用Windows API函数打开文件对话框,让用户选择要统计词频的文本文件。
3. 如果用户选择了文件,就打开文件并读取文件内容,将单词及其出现次数存储在一个map容器中,统计完成后关闭文件。
4. 对map容器中的单词出现次数进行排序,取出出现次数最多的前10个单词及其出现次数,并将它们按照一定格式显示在编辑框中。
下面是一个简单的示例代码:
```cpp
#include <Windows.h>
#include <map>
#include <string>
#include <fstream>
#include <algorithm>
#include <vector>
#define MAX_LOADSTRING 100
// 全局变量:
HINSTANCE hInst; // 当前实例
WCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
WCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
void OnOpenFile(HWND hWnd);
std::wstring GetFileContent(const std::wstring& filePath);
std::map<std::wstring, int> CountWords(const std::wstring& content);
bool CompareWordFrequency(const std::pair<std::wstring, int>& a, const std::pair<std::wstring, int>& b);
// 入口函数
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// 初始化全局字符串
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_WIN32APP, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32APP));
MSG msg;
// 主消息循环:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
// 注册窗口类
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32APP));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WIN32APP);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
// 初始化窗口
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // 将实例句柄存储在全局变量中
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
// 消息处理函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case IDM_OPEN:
OnOpenFile(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: 在此处添加使用 hdc 的任何绘图代码...
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// 打开文件对话框并统计词频
void OnOpenFile(HWND hWnd)
{
WCHAR szFile[MAX_PATH];
ZeroMemory(szFile, MAX_PATH * sizeof(WCHAR));
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWnd;
ofn.lpstrFilter = L"Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = szFile;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = L"txt";
if (GetOpenFileName(&ofn) == TRUE)
{
std::wstring filePath(szFile);
std::wstring content = GetFileContent(filePath);
std::map<std::wstring, int> wordCount = CountWords(content);
// 将单词及其出现次数存储到vector中,并按出现次数从大到小排序
std::vector<std::pair<std::wstring, int>> wordCountVec(wordCount.begin(), wordCount.end());
std::sort(wordCountVec.begin(), wordCountVec.end(), CompareWordFrequency);
// 构造要显示在编辑框中的文本
std::wstring displayText;
for (int i = 0; i < 10 && i < wordCountVec.size(); i++)
{
std::wstring word = wordCountVec[i].first;
int frequency = wordCountVec[i].second;
displayText += L"\"" + word + L"\": " + std::to_wstring(frequency) + L"\r\n";
}
// 在编辑框中显示文本
SetWindowText(GetDlgItem(hWnd, IDC_EDIT_RESULT), displayText.c_str());
}
}
// 获取文件内容
std::wstring GetFileContent(const std::wstring& filePath)
{
std::wifstream file(filePath);
if (!file.is_open())
{
return L"";
}
std::wstringstream buffer;
buffer << file.rdbuf();
return buffer.str();
}
// 统计单词出现次数
std::map<std::wstring, int> CountWords(const std::wstring& content)
{
std::map<std::wstring, int> wordCount;
std::wstring::const_iterator it = content.begin();
std::wstring::const_iterator end = content.end();
std::wstring word;
while (it != end)
{
if (iswalpha(*it))
{
word += *it;
}
else
{
if (!word.empty())
{
wordCount[word]++;
word.clear();
}
}
it++;
}
if (!word.empty())
{
wordCount[word]++;
}
return wordCount;
}
// 比较单词出现次数,用于排序
bool CompareWordFrequency(const std::pair<std::wstring, int>& a, const std::pair<std::wstring, int>& b)
{
return a.second > b.second;
}
```
注意:该示例代码中使用的是Windows API函数和C++ STL,如果您对它们不熟悉,可以参考相关文档或教程进行学习。