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

📄 wordcountdlg.cpp

📁 一个统计英语单词出现频率的软件源程序,结果按频率从大到小排列,并给出频率.该软件将统计结果存于记事本中.
💻 CPP
字号:
// WordCountDlg.cpp : implementation file
//

#include "stdafx.h"
#include "WordCount.h"
#include "WordCountDlg.h"
#include "Afx.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()

/////////////////////////////////////////////////////////////////////////////
// CWordCountDlg dialog

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

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

/////////////////////////////////////////////////////////////////////////////
// CWordCountDlg message handlers

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

void CWordCountDlg::OnOK() 
{
	// TODO: Add extra validation here
	CFileDialog MyFileDlg(TRUE,NULL,NULL,0,"文本文件(*.txt)|*.txt||");
//	CFileDialog MyFileDlg(TRUE,NULL,NULL,0,"文本文件 (*.txt)|*.txt|All Files (*.*)|*.*||");
	struct Node
	{
		int count;
		Node *nextNode;
		CString word;
	};
	CString temp,t;
	char byBuffer,pre=' ';
	Node *head=new Node;
	head->count=100000;
	Node *p;
	head->count=0;
	head->nextNode=NULL;
	CString strFileName;
	if(MyFileDlg.DoModal()==IDOK)
	{	
		
		try
		{
			strFileName=MyFileDlg.GetFileName();
			CFile MyFile(strFileName,CFile::modeRead);
			while(MyFile.Read(&byBuffer,1)==1)
			{
				if((byBuffer<=90&&byBuffer>=65)||(byBuffer<=122&&byBuffer>=97)||(byBuffer=='\''))
				{
					temp=temp+byBuffer;
					pre=byBuffer;
					continue;
				}
				else
				if(byBuffer=='-')
				{
					if(MyFile.Read(&byBuffer,1)==1)
					{
						if((byBuffer<=90&&byBuffer>=65)||(byBuffer<=122&&byBuffer>=97))
						{
							
							temp=temp+'-';
							pre=byBuffer;
							temp=temp+byBuffer;
							continue;
						}
						else
						if(byBuffer==0x0A)
						{
							continue;
						}
					}
					
				}
				else
				if(pre!=' '&&((byBuffer=='!')||(byBuffer=='!')||(byBuffer=='”')||(byBuffer=='“')||(byBuffer=='。')||(byBuffer==',')||(byBuffer==')')||(byBuffer=='(')||(byBuffer=='(')||(byBuffer==')')||(byBuffer==' ')||(byBuffer==',')||(byBuffer=='.')||(byBuffer=='\"'))||(byBuffer==10)||(byBuffer==13))
				{
					p=head;
					temp.MakeLower();
					while((p->word!=temp)&&(p->nextNode!=NULL))
					{
						p=p->nextNode;
					}
					if(p->word==temp)
					{
						p->count+=1;
						temp=t;
					}
					else
					{
						p->nextNode=new Node;
						p=p->nextNode;
						p->count=1;
						p->nextNode=NULL;
						p->word=temp;
						temp=t;
					}
				}
				pre=byBuffer;
			}
		}
		catch(CFileException *)
		{
			MessageBox("无法打开文件!");
		}

		if((pre<=90&&pre>=65)||(pre<=122&&pre>=97))//出来最后没有标点的情况
		{
					p=head;
					temp.MakeLower();
					while((p->word!=temp)&&(p->nextNode!=NULL))
					{
						p=p->nextNode;
					}
					if(p->word==temp)
					{
						p->count+=1;
						temp=t;
					}
					else
					{
						p->nextNode=new Node;
						p=p->nextNode;
						p->count=1;
						p->nextNode=NULL;
						p->word=temp;
						temp=t;
					}
		}

		//排序
/////////////////////////////////////////
		if(head->nextNode==NULL)
		{
			MessageBox("没有英文单词!");
			exit(1);
		}
		Node *p1,*p2,*p3;
		p1=(head->nextNode);
		p2=p1->nextNode;
		if(p2==NULL)
			goto L;
		p3=NULL;
		for(;p2->nextNode!=p3;)
		{

			while(p2->nextNode!=p3)
			{
				if((p1->count)<(p2->count))//比较相邻的两元素大小,使小的放前面
				{
					int num;
					num=p1->count;
					p1->count=p2->count;
					p2->count=num;

					CString s;
					s=p1->word;
					p1->word=p2->word;
					p2->word=s;

				}
				if((p2->nextNode->nextNode==p3))
				{
					if((p1->nextNode->count)<(p2->nextNode->count))
					{
						int num;
						num=(p1->nextNode->count);
						(p1->nextNode->count)=(p2->nextNode->count);
						(p2->nextNode->count)=num;

						CString t;
						t=(p1->nextNode->word);
						(p1->nextNode->word)=(p2->nextNode->word);
						(p2->nextNode->word)=t;


					}
				}
				if(p2!=NULL)
				{
					p1=p1->nextNode;
					p2=p2->nextNode;
				}
				
			};
			p3=p2;
			p1=(head->nextNode);
			p2=p1->nextNode;
		}
		if(p1->count<p2->count)
		{
			int num;
			num=p1->count;
			p1->count=p2->count;
			p2->count=num;

			CString m;
			m=p1->word;
			p1->word=p2->word;
			p2->word=m;
		}
////////////////////////////////////////////////////
		//写数据
		L:	CFileDialog MyFileDlg1(FALSE,NULL,NULL,1,"文本文件(*.txt)|*.txt||");
		if(MyFileDlg1.DoModal()==IDOK)
		{
			strFileName=MyFileDlg1.GetFileName();
			if(strchr(strFileName, '.') == NULL)
				strFileName=strFileName+".txt";
			CFile f(strFileName,CFile::modeCreate|CFile::modeWrite);
			p=head->nextNode;
			Node b;
			char a[100];
			int temNum;
			while(p!=NULL)
			{
				for(int i=0;i<100;i++)
					a[i]='\0';
				int len=p->word.GetLength();
				int j;
				for(j=0;j<len;j++)
				{
					a[j]=p->word.GetAt(j);
				}
				a[++j]=' ';
				temNum=p->count;
				gcvt(temNum,4,&a[++j]);
				j=j+3;
				a[++j]=13;
				a[++j]=10;
				f.Write(&a,++j);
				p=p->nextNode;
			}
		}
	}
	while(head!=NULL)
	{
		p=head;
		head=head->nextNode;
		delete p;

	}
		
//	CDialog::OnOK();
}

⌨️ 快捷键说明

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