⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wordppl.cpp

📁 中文信息逆向分词程序 是用api实现的
💻 CPP
字号:

/*-----------------------------中文分词程序------------------------------*/


// wordppl.cpp : Defines the entry point for the application.

#include "stdafx.h"
#include "resource.h"
#include <stdio.h>
#include <commdlg.h>

#define MAX_LOADSTRING 100

// Global Variables:
extern char tt[][40];

extern char * source,*sourceres;		//文件暂存数组
extern int sum;				//字典长度
extern FILE *fp1,*fp2;		//定义两个文件指针
extern long filelen;		//输入文件的长度
extern char scrbuf[MD];		//屏幕文件缓冲区
extern char scrres[SD];		//屏幕文件结果区

int POS=1;				//选择算法,1为正向解析,0为反向解析,默认为正向解析

HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];								// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];						// The title bar text
char FileNameStr[MAX_LOADSTRING];							//文件路径缓冲区

// Foward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	FILETRANSFER(HWND, UINT, WPARAM, LPARAM);
bool				OnFileOpen(HWND);
bool				OnFileSave(HWND);
int					initialize();		//初始化函数
int					compare(const void*,const void*);	//比较函数
int					binsearch(int,char *);				//折半查找算法
void				scrnegcutword();				//采用逆向最大匹配算法



int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_WORDPPL, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_WORDPPL);

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return msg.wParam;
}


//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX); 

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, (LPCTSTR)IDI_WORDPPL);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= (LPCSTR)IDC_WORDPPL;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	int i;	//普通变量	
	FILE *fp;
	PAINTSTRUCT ps;
	HDC hdc;
	static HWND hWndEdit;
	TCHAR szHello[MAX_LOADSTRING];
	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

	switch (message) 
	{
		case WM_CREATE:
			//创建一个编辑控件
			hWndEdit = CreateWindow(
				"EDIT",						//窗口内名称		
				NULL,						//无标题文字
				WS_CHILD|WS_VISIBLE|WS_VSCROLL|ES_AUTOVSCROLL|ES_LEFT|
				ES_MULTILINE,				//可容纳和编辑多行文本
				0,0,0,0,					//位置和尺寸待定
				hWnd,						//父窗口句柄
				(HMENU)ID_EDITCONTROL,		//控件标识号
				hInst,						//实例句柄
				NULL);	
			CheckMenuRadioItem(GetMenu(hWnd),IDM_POS,IDM_NEG,IDM_POS,MF_BYCOMMAND);
			//初始化字典
			if(!initialize())
				MessageBox(hWnd,"词典初始化失败!","错误",MB_OK);
			break;

		case WM_SETFOCUS:		//设置输入焦点			
			SetFocus(hWndEdit);
			break;
		case WM_SIZE:
			MoveWindow(hWndEdit,
				0,0,
				LOWORD(lParam),
				HIWORD(lParam),
				TRUE);
			break;

		case WM_INITMENUPOPUP:
			/*判断当前编辑控件是否可以进行撤销操作,以此来
			  设置[撤销]菜单项的状态。				   */	
			if(SendMessage(hWndEdit,EM_CANUNDO, 0 , 0 ))
				EnableMenuItem(GetMenu(hWnd),IDM_EDITUNDO,
					MF_BYCOMMAND|MF_ENABLED);
			else
				EnableMenuItem(GetMenu(hWnd),IDM_EDITUNDO,
					MF_BYCOMMAND|MF_GRAYED);
			break;

		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
			switch (wmId)
			{
				

				case IDM_FILEOPEN:				//打开文件
					OnFileOpen(hWnd);
					
					if(FileNameStr[0] == 0)					//没有文件路径,取消
						break;
					fp = fopen(FileNameStr,"r");
					if(fp!=NULL)
					{	SendMessage(hWndEdit,EM_SETSEL,0,-1);
						SendMessage(hWndEdit,WM_CLEAR,0,0);
						i = fread(scrbuf,sizeof(char),MD,fp);
						scrbuf[i]=0;
						if(i==MD)
							MessageBox(hWnd,"文件数超过32768,不能完全显示!","错误",MB_OK);						
						fclose(fp);
						//在编辑控件区显示文件内容
						SendMessage(hWndEdit,WM_SETTEXT,0,(LPARAM)scrbuf);
					}
					else
					{	MessageBox(hWnd,"文件打开失败","错误",MB_OK);
					}
					break;

				case IDM_FILESAVE:				//保存文件					
					OnFileSave(hWnd);
					
					if(strcmp(FileNameStr,".txt") == 0)		//没有文件路径,取消
						break;
					if(fp = fopen(FileNameStr,"w"))
					{	//读取编辑控件内容并输入到文件
						SendDlgItemMessage(hWnd,ID_EDITCONTROL,WM_GETTEXT,(WPARAM)MD,(LPARAM)scrbuf);
						fprintf(fp,"%s",scrbuf);
						fclose(fp);
					}else{//错误
						MessageBox(hWnd,"文件存储失败","错误",MB_OK);
					}
					break;


				//以下为菜单操作
				case IDM_EDITUNDO:			/* 撤销 */
					SendMessage(hWndEdit,WM_UNDO,0,0);
					break;
				case IDM_EDITCUT:			/* 剪切 */
					SendMessage(hWndEdit,WM_CUT,0,0);
					break;
				case IDM_EDITCOPY:			/* 复制 */
					SendMessage(hWndEdit,WM_COPY,0,0);
					break;
				case IDM_EDITPASTE:			/* 粘贴 */
					SendMessage(hWndEdit,WM_PASTE,0,0);
					break;
				case IDM_EDITDELETE:		/* 删除 */
					SendMessage(hWndEdit,WM_CLEAR,0,0);
					break;
				case IDM_EDITSELECTALL:		/* 全部选定 */
					SendMessage(hWndEdit,EM_SETSEL,0,-1);
					break;

				case IDM_NEG:
					CheckMenuRadioItem(GetMenu(hWnd),IDM_POS,IDM_NEG,IDM_NEG,MF_BYCOMMAND);
					POS=0;
					SendDlgItemMessage(hWnd,ID_EDITCONTROL,WM_GETTEXT,(WPARAM)MD,(LPARAM)scrbuf);	

					source = (char *)malloc( filelen*sizeof(char) );		//动态为待分析文件分配内存
					if( source == NULL )			//如果内存分配失败,则返回
					{ MessageBox(hWnd,"内存分配失败!","错误",MB_OK); break; }
					
					if(POS==0)
						scrnegcutword();
					//将结果显示在编辑控件中
					SendMessage(hWndEdit,WM_SETTEXT,0,(LPARAM)scrres);

					free(source);
					break;
			case IDM_ABOUT:				/* 关于对话框 */
				   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
				   break;
			case IDM_EXIT:				/* 退出 */
				   DestroyWindow(hWnd);
				   break;
				default:
				   return DefWindowProc(hWnd, message, wParam, lParam);
			}

		case WM_PAINT:
			hdc = BeginPaint(hWnd, &ps);
			// TODO: Add any drawing code here...
			RECT rt;
			GetClientRect(hWnd, &rt);
			DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
			EndPaint(hWnd, &ps);
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}



// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_INITDIALOG:
				return TRUE;

		case WM_COMMAND:
			if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
			{
				EndDialog(hDlg, LOWORD(wParam));
				return TRUE;
			}
			break;
	}
    return FALSE;
}

//文件分词对话框
LRESULT CALLBACK FILETRANSFER(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_INITDIALOG:
				return TRUE;

		case WM_COMMAND:
			switch(LOWORD(wParam)){
				case IDOK:		//确定
					//读取输入文件路径
					SendDlgItemMessage(hDlg,IDC_FILEIN,WM_GETTEXT,(WPARAM)MAX_LOADSTRING,(LPARAM)FileNameStr);
					if(FileNameStr[0] == 0)//路径为空
						return TRUE;
					fp1 = fopen(FileNameStr,"r");
					if(fp1 == NULL){//打开失败
						EndDialog(hDlg, LOWORD(wParam));
						FileNameStr[0] = 0;
						return TRUE;
					}
					//读取输出文件路径
					SendDlgItemMessage(hDlg,IDC_FILEOUT,WM_GETTEXT,(WPARAM)MAX_LOADSTRING,(LPARAM)FileNameStr);
					if(FileNameStr[0] == 0)//路径为空
						return TRUE;
					fp2 = fopen(FileNameStr,"w");
					fclose(fp2);
					fp2 = fopen(FileNameStr,"w+");
					if(fp2 == NULL){//打开失败
						fclose(fp1);
						EndDialog(hDlg, LOWORD(wParam));
						FileNameStr[0] = 0;
						return TRUE;
					}
					EndDialog(hDlg, LOWORD(wParam));
					return TRUE;
				case IDCANCEL:		//取消
					FileNameStr[0] = 1;
					EndDialog(hDlg, LOWORD(wParam));
					return TRUE;
				case IDC_BROWSEOUT:		//输出文件浏览
					OnFileOpen(hDlg);
					//将文件目录显示在编辑框
					SendMessage(GetDlgItem(hDlg,IDC_FILEOUT),WM_SETTEXT,0,(LPARAM)FileNameStr);
					break;
				case IDC_BROWSEIN:		//输入文件浏览
					OnFileOpen(hDlg);
					//将文件目录显示在编辑框
					SendMessage(GetDlgItem(hDlg,IDC_FILEIN),WM_SETTEXT,0,(LPARAM)FileNameStr);
					FileNameStr[strlen(FileNameStr)-4] = 0;
					if(POS==1)strcat(FileNameStr,"_pos.txt");
					else strcat(FileNameStr,"_neg.txt");
					//自动将默认输出文件显示在编辑框
					SendMessage(GetDlgItem(hDlg,IDC_FILEOUT),WM_SETTEXT,0,(LPARAM)FileNameStr);
					break;
			}
			break;

	}
    return FALSE;
}

//打开文件
bool OnFileOpen(HWND hwnd)
{
	OPENFILENAME ofn;

	FileNameStr[0]=0;

	ofn.lStructSize=sizeof(OPENFILENAME);
	ofn.hwndOwner=hwnd;
	ofn.hInstance=hInst;
	ofn.lpstrFilter="文本文件(*.txt)\0*.txt\0所有文件(*.*)\0*.*";
	ofn.lpstrCustomFilter=NULL;
	ofn.nMaxCustFilter=0;
	ofn.nFilterIndex=1;
	ofn.lpstrFile=FileNameStr;
	ofn.nMaxFile=sizeof(FileNameStr);
	ofn.lpstrFileTitle=NULL;
	ofn.nMaxFileTitle=0;
	ofn.lpstrInitialDir=NULL;
	ofn.lpstrTitle=NULL;
	ofn.Flags=OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST|OFN_EXPLORER;
	ofn.nFileOffset=0 ;
	ofn.nFileExtension=0;
	ofn.lpfnHook=NULL;
	ofn.lCustData=0;
	ofn.lpstrDefExt=NULL;
	ofn.lpTemplateName=NULL;
	if(GetOpenFileName(&ofn))
		return true;
	return false;
}

//保存文件
bool OnFileSave(HWND hwnd)
{
	OPENFILENAME ofn;
	
	FileNameStr[0]=0;

	ofn.lStructSize=sizeof(OPENFILENAME);
	ofn.hwndOwner=hwnd;
	ofn.hInstance=hInst;
	ofn.lpstrFilter="文本文件(*.txt)\0*.txt\0所有文件(*.*)\0*.*";
	ofn.lpstrCustomFilter=NULL;
	ofn.nMaxCustFilter=0;
	ofn.nFilterIndex=1;
	ofn.lpstrFile=FileNameStr;
	ofn.nMaxFile=sizeof(FileNameStr);
	ofn.lpstrFileTitle=NULL;
	ofn.nMaxFileTitle=0;
	ofn.lpstrInitialDir=NULL;
	ofn.lpstrTitle=NULL;
	ofn.Flags=OFN_OVERWRITEPROMPT|OFN_PATHMUSTEXIST|OFN_EXPLORER;
	ofn.nFileOffset=0;
	ofn.nFileExtension=0;
	ofn.lpfnHook=NULL;
	ofn.lCustData=0;
	ofn.lpstrDefExt=NULL;
	ofn.lpTemplateName=NULL;

	if(strcmpi(FileNameStr+sizeof(FileNameStr)-4,".txt"))
		strcat(FileNameStr,".txt");
	if(GetSaveFileName(&ofn))
		return true;
	return false;
}







⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -