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

📄 txtcountdlg.cpp

📁 英文文章一二阶信息熵的统计计算
💻 CPP
字号:
// txtcountDlg.cpp : implementation file
//

#include "stdafx.h"
#include "txtcount.h"
#include "txtcountDlg.h"
#include <atlconv.h>
#include <math.h>

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
int generaterand(int m_range);

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()

/////////////////////////////////////////////////////////////////////////////
// CTxtcountDlg dialog

CTxtcountDlg::CTxtcountDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTxtcountDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTxtcountDlg)
		// 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 CTxtcountDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTxtcountDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTxtcountDlg, CDialog)
	//{{AFX_MSG_MAP(CTxtcountDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_EN_CHANGE(IDC_EDIT3, OnChangeEdit3)
	ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_EN_CHANGE(IDC_EDIT2, OnChangeEdit2)
	ON_EN_CHANGE(IDC_EDIT4, OnChangeEdit4)
	ON_EN_CHANGE(IDC_EDIT5, OnChangeEdit5)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTxtcountDlg message handlers

BOOL CTxtcountDlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTxtcountDlg::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 CTxtcountDlg::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 CTxtcountDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}



CString sFilePath;
void CTxtcountDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	//显示文件打开对话框
	CFileDialog dlg(TRUE, "*.txt", NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"Text Files(*.txt)|*.txt|All Files(*.*)|*.*||"); 
	if ( dlg.DoModal()!=IDOK ) return;
	//获取文件的绝对路径
	sFilePath=dlg.GetPathName();
	SetDlgItemText(IDC_EDIT3,sFilePath);




}

void CTxtcountDlg::OnChangeEdit1() 
{

	
	
}


void CTxtcountDlg::OnChangeEdit3() 
{


}

int n;
void CTxtcountDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	//读取文本

  double  word=0,mem=0;
  double avglenth=0,H=0,H2=0;
  n=0;

  CStdioFile   file1;   
  CString   str;   
  BOOL   bExist   = file1.Open(sFilePath,   CFile::modeRead   |   CFile::typeText   ); 
  if   (bExist)//判断这个文件是否存在,处理异常
  {
	  	CString letter=" abcdefghijklmnopqrstuvwxyz",samletter;
		//随机抽取n个字母作为样本
		int n=6;
		double a[27][27]={0},na=0,a2[27][27][27]={0},na2=0;
		/*int sam;
		//抽取6个不一样的字母作样本
		for(int k=0;k<=n-1;k++)
		{
			sam=generaterand(27);
			CString letter1=letter.GetAt(sam);
			if(samletter.Find(letter1)==-1)//samletter中不存在当前要插入的字母
			   samletter.Insert(0,letter.GetAt(sam));
			else 
				k=k-1;
			
		}*/
		samletter=" abcdefghijklmnopqrstuvwxyz";
		samletter.MakeLower();//将字符串转为小写
        while(NULL   !=   file1.ReadString(str))//当文件存在时,读取一行并存到变量str中   
		{
			if(n>100000)
				break;
			int fileseek=generaterand(300);
			file1.Seek(fileseek,CFile::current);
			CString str2,str3,str4,str5;
			if((str!=""))
			{
				int k=str.GetLength();
			    str5=str.GetAt(k-1);
			    if(('A'<=str5 && str5<='Z')||('a'<=str5 && str5<='z'))
			          word=word+1;
			
						for(int i=0;i<str.GetLength()-1;i++)
						{ 	
							str2=str.GetAt(i);
							str3=str.GetAt(i+1);
							if(str.GetLength()-2>i)
								str4=str.GetAt(i+2);
							//将读取到的字母转为小写
							str2.MakeLower();
							str3.MakeLower();
							//
							if((samletter.Find(str2)!=-1)&&(samletter.Find(str3)!=-1))//samletter中同时存在str2,str3
							{
								a[samletter.Find(str2)][samletter.Find(str3)]=a[samletter.Find(str2)][samletter.Find(str3)]+1;
								na=na+1;
								if(samletter.Find(str4)!=-1)
								{
									a2[samletter.Find(str2)][samletter.Find(str3)][samletter.Find(str4)]=a2[samletter.Find(str2)][samletter.Find(str3)][samletter.Find(str4)]+1;
									na2=na2+1;
								}
							}


							if(('a'<=str2 && str2<='z')&&(!('a'<=str3 && str3<='z'))&&str3!='-')
							{
								word=word+1;
								if(str.GetLength()-2>=i)
									if(str3=='\'')
									{
										CString str6;
										str6=str.GetAt(i+2);
										if(('A'<=str6 && str6<='Z')||('a'<=str6 && str6<='z'))
											word=word-1;
									}


							}				
						
						}

						for(int j=0;j<str.GetLength();j++)
						{
							str2=str.GetAt(j);
							if (('A'<=str2 && str2<='Z')||('a'<=str2 && str2<='z'))
								  mem=mem+1;
						}
						
			}


		}
		//一阶合并相同字符的出现次数
		for(int x=0;x<27;x++)
			for(int q=0;q<27;q++)
			{
				if(x>q)
				{
					a[q][x]+=a[x][q];
					a[x][q]=0;
				}
			}
		//计算一阶信息商
		double pa;
		if(na!=0)
		  for(int m=0;m<27;m++)
			for(int q=0;q<27;q++)
			{
				if(a[m][q]!=0)
				{
				   pa=a[m][q]/na;
				   H=H+(-pa*(log(pa)/log(2)));

				}
			}


		//合并相同字符的出现次数
		for(int y=0;y<27;y++)
			for(int q=0;q<27;q++)
				for(int m=0;m<27;m++)
				{
					if(y>q&&q>m)
					{
						a2[y][q][m]+=a2[y][m][q]+a2[m][y][q]+a2[m][q][y]+a2[q][y][m]+a2[q][m][y];
						a2[y][m][q]=a2[m][y][q]=a2[m][q][y]=a2[q][y][m]=a2[q][m][y]=0;
					}
				}
		//计算二阶信息商
		double pa2;
		if(na!=0)
		  for(int m=0;m<27;m++)
			  for(int y=0;y<27;y++)
				for(int q=0;q<27;q++)
				{
					if(a2[m][q][y]!=0)
					{
					   pa2=a2[m][q][y]/na2;
					   H2=H2+(-pa2*(log(pa2)/log(2)));

					}
				}


		



		CString t;
		avglenth=mem/word;
		t.Format("%.4f",avglenth); 
        SetDlgItemText(IDC_EDIT4,t);
		t.Format("%f",H/2);
        SetDlgItemText(IDC_EDIT1,t);
		t.Format("%f",H2/3);
        SetDlgItemText(IDC_EDIT2,t);




		/*CFile file;
		file.Open(sFilePath1, CFile::modeCreate | CFile::modeReadWrite | CFile::shareDenyWrite, NULL);
		int j=s4.GetLength();
		file.Write(s4, j);
		SetDlgItemText(IDC_EDIT5,sFilePath1);
		//定位读取文件中的一个字符
		
		file1.Seek(10,CFile::begin);
		file1.ReadString(str);
		AfxMessageBox(str);
		file.Close();
		file.Remove(sFilePath1);*/
		




		
  }
  else
  {
		CString   str1="错误:找不到文件!";
		AfxMessageBox(str1);

		
  }


	
}

void CTxtcountDlg::OnChangeEdit2() 
{

	
}

void CTxtcountDlg::OnChangeEdit4() 
{

	
}

void CTxtcountDlg::OnChangeEdit5() 
{

	
}


//产生随机数的函数
int generaterand(int m_range) 
{
	//m_range为随机数的最大值,即生成0~m_range之间的随机数
	int m_random;
    CTime   t=CTime::GetCurrentTime();   
    m_random = (rand()+t.GetSecond())%m_range;
	n=n+1;
	return m_random;

}



⌨️ 快捷键说明

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