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

📄 mainfrm.cpp

📁 文本中汉字频率统计的程序
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "HZ_Freq.h"

#include "MainFrm.h"
#include "MyFileApp.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define MaxHzNum 70000

int zipin_table[257][257];
int token_counter, type_counter;

struct HzFreq {
	unsigned char HighByte,LowByte;
	int freq;
} myHzFreq[MaxHzNum];


/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_HandleFiles, OnHandleFiles)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CMDIFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CMDIFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CMDIFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

void hzCountInAFile(CString FileName) 
{// 统计单个文件中单字出现次数
	
	int j=0, k=0;
	
	unsigned char tmpc1,tmpc2;

	FILE * in;

	in=fopen((const char *) FileName, "rb");
	if(in==NULL) {
		AfxMessageBox("can't open the file");
		return;
	}

	while (!feof(in)) 
	{
		tmpc1=(unsigned char) fgetc(in);

		if(tmpc1<127 || tmpc1>254) // 超过254,低于127都不是中文字符
			continue;

////////////////////////////////////////////////////////////
// GBK编码 HighByte从 0x81开始, 0xA1 - 0xA9 为非汉字区
// Big5编码 HighByte 从 0xA0 开始,0xA0 - 0xA3 为非汉字区,其中有个别生造汉字		
////////////////////////////////////////////////////////////
		
		if (tmpc1>=161 && tmpc1<=169)
		{//如果在161到169之间,说明是非汉字字符,再读进一个字节
			tmpc2 = fgetc(in);
			continue;
		}
// 如果文本中出现 0xAAAE, 0xADB8, 0xAAB2 等字符,
// 在统计结果中显示为空字符

		tmpc2=fgetc(in);

		zipin_table[tmpc1][tmpc2]++;
		token_counter++;
	}

	fclose(in);
	return;
}

int compare( const void *arg1, const void *arg2 )
{// 比较频度大小,进行排序的函数
   
	if (((struct HzFreq *)arg1)->freq >= ((struct HzFreq *) arg2)->freq)
		return -1;
	else 
		return 1;
}

void CMainFrame::OnHandleFiles() 
{
	// TODO: Add your command handler code here
	
	token_counter=0; // 开始一次新的统计
	type_counter=0;

	int i = 0, j = 0, k = 0;

	/* 二维数组初始化置0 */

	while (j<257 && k<257) {
		while (k<257){
			zipin_table[j][k]=0;
			k++;
		}
		j++;
		if (j<257) 
			k=0;
	}

	ProcessFiles("","*.*",hzCountInAFile); // 批处理多个文件
	
	// 将字频数据写入字频结构
	if (token_counter==0)
		return;

	for(i=0;i<257;i++)
	{
		for(j=0;j<257;j++)
		{
			if (zipin_table[i][j])
			{
				myHzFreq[type_counter].HighByte = i;
				myHzFreq[type_counter].LowByte = j;
				myHzFreq[type_counter].freq = zipin_table[i][j];
				type_counter++;
			}
		}
	}

	// 把zipin结构中的汉字按字频从大到小排序
	qsort(myHzFreq, type_counter, sizeof(HzFreq), compare);

	FILE *outFile=fopen("HzFrequencyList.txt","wt");
	
	int skip=0;

	for (int id=0;id<type_counter;id++) {
/*
		if (myHzFreq[id].freq>1000)
			fprintf(outFile,"%c%c:%d\t",myHzFreq[id].HighByte,myHzFreq[id].LowByte,myHzFreq[id].freq);
		else 
			fprintf(outFile,"%c%c:%d\t\t",myHzFreq[id].HighByte,myHzFreq[id].LowByte,myHzFreq[id].freq);
*/	
		fprintf(outFile,"%c%c:%d\t",myHzFreq[id].HighByte,myHzFreq[id].LowByte,myHzFreq[id].freq);
		if (++skip == 5){
			fprintf(outFile,"\n");
			skip = 0;
		}
	}

	fprintf(outFile,"\n\nToken number of total Chinese Characters:%d",token_counter);
	fprintf(outFile,"\nType number of total Chinese Characters:%d",type_counter);

	fclose(outFile);
}

/////////////////////////////////////////
// 可以进一步改进的地方:
// 1. 由用户指定哪些字符不在统计范围内
// 2. 由用户指定输出报表内容,比如给出频次分级报表,出现1万次以上的字数
//     出现5000次以上的字数,等等。
//   詹卫东 2002-11-8
//////////////////////////////////////////////

⌨️ 快捷键说明

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