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

📄 汉字统计程序.txt

📁 简单的汉字频率统计程序 比如分析古文的时候可以看看哪些字出现次数高 C++/VC/MFC
💻 TXT
字号:
// hzDlg.cpp : implementation file
//

#include "stdafx.h"
#include "hz.h"
#include "hzDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHzDlg dialog

CHzDlg::CHzDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CHzDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CHzDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CHzDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CHzDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CHzDlg, CDialog)
	//{{AFX_MSG_MAP(CHzDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHzDlg message handlers

BOOL CHzDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_fileName.SubclassDlgItem(IDC_FileName, this);
	m_tongJi.SubclassDlgItem(IDC_TongJi,this);
	m_ciShu.SubclassDlgItem(IDC_CiShu,this);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CHzDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CHzDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CHzDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CHzDlg::OnOK() 
{
	// TODO: Add extra validation here
	CString str;  //被统计文本	
	CString strHanZi=_T(""); // 清楚非汉字后的统计, 既重复汉字统计
    CString sCiShu;  //统计 ciShu字数以上的字
	GetDlgItemText(IDC_CiShu,sCiShu);
	int nCiShu;
	nCiShu= int(atof(sCiShu));
   GetDlgItemText(IDC_FileName,str);
   int length;
   str.Replace(" ","");
   length=str.GetLength();

 

 CString strHz=_T("");  //不重复汉字统计
 CString sTemp;




 for(int i=0;i<length;i++)
 {
	if ((UCHAR)str.GetAt(i)<128) continue;  //非汉字跳过
  
 
	  sTemp=str.Mid(i++,2); //得到下一个汉字
	  if( strHz.Find(sTemp) == -1 )  //判断此汉字是否已经记录
      {
	    strHz+=sTemp;   //更新 
	  }
	  //	int Find(LPCTSTR lpszSub, int nStart) const;

 }

sTemp="";

 for( i=0;i<length;i++)
 {
	if ((UCHAR)str.GetAt(i)<128) continue;  //非汉字跳过
  
 
	  sTemp=str.Mid(i++,2); //得到下一个汉字
 
	    strHanZi+=sTemp;   //更新 
 
	  //	int Find(LPCTSTR lpszSub, int nStart) const;

 }
int HanZiTotal=strHanZi.GetLength();


   CString hz[5000];
   int tongJi[5000];
   CString strPinLv("");

 for(i=0;i<strHz.GetLength();i++)
 {
	  sTemp=strHz.Mid(i++,2); //得到下一个汉字
	  LPTSTR p = sTemp.GetBuffer(0);
      tongJi[i]=str.Replace(p,p);
	  hz[i]=sTemp;
	  if(tongJi[i] >nCiShu )
	  {
       CString sNum;
	   sNum.Format("%d|",tongJi[i]);
       strPinLv= strPinLv + sTemp + sNum;
	  }
	  //	int Find(LPCTSTR lpszSub, int nStart) const;

 }



 int FewCount=0;  /*少量汉字统计*/
 for(i=0;i<strHz.GetLength();i++)
 {
	  sTemp=strHz.Mid(i++,2); //得到下一个汉字
	  LPTSTR p = sTemp.GetBuffer(0);
      tongJi[i]=str.Replace(p,p);
	  hz[i]=sTemp;
	  if(tongJi[i] <2 )
	  {
		  
       CString sNum;
	   sNum.Format("%d|",tongJi[i]);
       strPinLv= strPinLv + sTemp + sNum;
	   FewCount=FewCount+1;
	  }
	  //	int Find(LPCTSTR lpszSub, int nStart) const;

 }



   CString sTongJi;
   sTongJi.Format("总数:[%d] 不重复汉字数 [%d] 频率超过%d的字 \r\n%s ",
	   str.GetLength(),strHz.GetLength(), nCiShu, strPinLv );
   SetDlgItemText(IDC_TongJi,sTongJi);
	//CDialog::OnOK();
}


/*
 * 把字符串的标点统一成非全角标点
 */
void QuanJiao(CString str)
{
	
 //全角字符统一成普通字符
/*
char * sQuanJiao;
CString s1,s2;
s1.Format("%s","?");
s2.Format("%s","?");
strHz = str.Replace (s1.GetBuffer(9000),s2.GetBuffer(9000)); 

strcpy(sQuanJiao,"。");
strHz = str.Replace (sQuanJiao,".");
strcpy(sQuanJiao,"!");
strHz = str.Replace (sQuanJiao,"!");
strcpy(sQuanJiao,",");
strHz = str.Replace (sQuanJiao,",");
strcpy(sQuanJiao,":");
strHz = str.Replace (sQuanJiao,":");
strcpy(sQuanJiao,";");
strHz = str.Replace (sQuanJiao,";");
strHz = str.Replace ("“","\"");
strHz = str.Replace ("”","\"");
*/
	str.Replace("”","\"");
}

⌨️ 快捷键说明

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