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

📄 asdlg.cpp

📁 利用Visual c++编程思想方法实现基于图书编码分类算法的程序
💻 CPP
字号:
// asDlg.cpp : implementation file
//

#include "stdafx.h"
#include "as.h"
#include "asDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CAsDlg dialog

CAsDlg::CAsDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAsDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAsDlg)
	m_strCode1 = _T("");
	m_strCode2 = _T("");
	m_strCode3 = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	pStru = NULL;
	memset(&newStru, 0, sizeof(HEAPSORT));
}

void CAsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAsDlg)
	DDX_Control(pDX, IDC_CODE3, m_code3);
	DDX_Control(pDX, IDC_CODE2, m_code2);
	DDX_Control(pDX, IDC_CODE1, m_code1);
	DDX_Control(pDX, IDC_COMBO1, m_select);
	DDX_Control(pDX, IDC_LIST3, m_List2);
	DDX_Control(pDX, IDC_LIST1, m_List1);
	DDX_Control(pDX, IDC_LIST4, m_codeList);
	DDX_Text(pDX, IDC_CODE1, m_strCode1);
	DDV_MaxChars(pDX, m_strCode1, 7);
	DDX_Text(pDX, IDC_CODE2, m_strCode2);
	DDV_MaxChars(pDX, m_strCode2, 7);
	DDX_Text(pDX, IDC_CODE3, m_strCode3);
	DDV_MaxChars(pDX, m_strCode3, 7);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAsDlg, CDialog)
	//{{AFX_MSG_MAP(CAsDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BT_START, OnBtStart)
	ON_BN_CLICKED(IDC_BT_READ_DATA, OnBtReadData)
	ON_EN_SETFOCUS(IDC_CODE1, OnSetfocusCode1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAsDlg message handlers

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

	// Add "About..." menu item to system menu.
	DWORD dwStyle = m_codeList.GetStyle();
/*	dwStyle |= */
	// 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_select.SetCurSel(0);
	int nIndex = 0;
	FILE *fp;
	char buf[1024] = {0};
	if ((fp = fopen("code.txt","r")) == NULL)
	{
		MessageBox("不能打开文件code.txt", "提示", MB_OK | MB_ICONWARNING);
	}
	else
	{
		while (feof(fp) == 0)
		{
			fgets(buf, 1023, fp);
			m_codeList.InsertString(nIndex, buf);
			nIndex+=1;
		}
		fclose(fp);
	}
	m_List1.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
	m_List2.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
	m_List1.InsertColumn(0, "出版社代码", LVCFMT_LEFT, 75);
	m_List1.InsertColumn(1, "未知编码", LVCFMT_LEFT, 60);
	m_List1.InsertColumn(2, "中图分类代码", LVCFMT_LEFT, 100);
	m_List2.InsertColumn(0, "出版社代码", LVCFMT_LEFT, 75);
	m_List2.InsertColumn(1, "未知编码", LVCFMT_LEFT, 60);
	m_List2.InsertColumn(2, "中图分类代码", LVCFMT_LEFT, 100);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CAsDlg::sift_press(HEAPSORT *p[], int k, int i, int l)
{
	int m,n;
     char *t;
     HEAPSORT *w;
     t=(*p[i+k]).press;w=p[i+k];n=2*(i+1)-1;m=i;
     while(n<=l-1)
     {
       if((n<l-1)&&(strcmp((*p[n+k]).press,(*p[n+k+1]).press)<0))
        n=n+1;
        if(strcmp(t,(*p[n+k]).press)<0)
        {
           p[m+k]=p[n+k];
           m=n;n=2*(m+1)-1;
         } 
         else n=l;
     }
    p[m+k]=w;
    return;
}

void CAsDlg::sift_unknow(HEAPSORT *p[], int k, int i, int l)
{
	int m,n;
	char *t;
	HEAPSORT *w;
	t=(*p[i+k]).unknow;w=p[i+k];n=2*(i+1)-1;m=i;
	while(n<=l-1)
	{
		if((n<l-1)&&(strcmp((*p[n+k]).unknow,(*p[n+k+1]).unknow)<0))
			n=n+1;
        if(strcmp(t,(*p[n+k]).unknow)<0)
        {
			p[m+k]=p[n+k];
			m=n;n=2*(m+1)-1;
		} 
		else n=l;
	}
    p[m+k]=w;
    return;
}

void CAsDlg::sift_kind(HEAPSORT *p[], int k, int i, int l)
{
	int m,n;
	char *t;
	HEAPSORT *w;
	t=(*p[i+k]).kind;w=p[i+k];n=2*(i+1)-1;m=i;
	while(n<=l-1)
	{
		if((n<l-1)&&(strcmp((*p[n+k]).kind,(*p[n+k+1]).kind)<0))
			n=n+1;
        if(strcmp(t,(*p[n+k]).kind)<0)
        {
			p[m+k]=p[n+k];
			m=n;n=2*(m+1)-1;
		} 
		else n=l;
	}
    p[m+k]=w;
    return;
}

void CAsDlg::mtheap(HEAPSORT *p[], int n, int k, int m, void (*sift)(HEAPSORT *p[], int k, int i, int l))
{
	int i,l;
   HEAPSORT *w;
   if(k<0) k=0;
   if(m>n-1) m=n-1;
    l=m-k+1;
    for(i=l/2-1;i>=0;i--)
      (*sift)(p,k,i,l);
    for(i=l-1;i>=1;i--)
    {
     w=p[k];p[k]=p[i+k];p[i+k]=w;
     (*sift)(p,k,0,i);
     }
   return;
}

void CAsDlg::OnBtStart() 
{
	// TODO: Add your control notification handler code here
	int nCount = m_List1.GetItemCount();
	if (nCount <= 0)
	{
		return;
	}
	if (pStru != NULL)
	{
		delete [] pStru;
		pStru = NULL;
	}
	pStru = new HEAPSORT[nCount];
	memset(pStru, 0, sizeof(HEAPSORT) * nCount);
	int nIndex = 0;
	CString str;
	while (nIndex < nCount)
	{
		str = m_List1.GetItemText(nIndex, 0);
		strncpy(pStru[nIndex].press, str, 11);
		str = m_List1.GetItemText(nIndex, 1);
		strncpy(pStru[nIndex].unknow, str, 11);
		str = m_List1.GetItemText(nIndex, 2);
		strncpy(pStru[nIndex].kind, str, 11);
		nIndex += 1;
	}

	switch(m_select.GetCurSel())
	{
	case 0:
		press(nCount);
		break;
	case 1:
		unknow(nCount);
		break;
	case 2:
		kind(nCount);
		break;
	}
	nIndex = 0;
	nCount = m_List2.GetItemCount();
	while (nIndex < nCount)
	{
		if (m_List2.GetItemText(nIndex, 0) == newStru.press &&
			m_List2.GetItemText(nIndex, 1) == newStru.unknow &&
			m_List2.GetItemText(nIndex, 2) == newStru.kind)
		{
			break;
		}
		nIndex += 1;
	}
	nIndex = nIndex % nCount;
	m_List2.SetHotItem(nIndex);
	m_List2.SetTextColor(RGB(255,0,0));
}

void CAsDlg::OnBtReadData() 
{
	// TODO: Add your control notification handler code here
	CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "文本文件(*.txt) |*.txt||",NULL);
	if (dlg.DoModal() == IDOK)
	{
		m_List1.DeleteAllItems();
		FILE *fp;
		char buf[1024] = {0};
		CString str;
		int nIndex = 0;
		if ((fp = fopen(dlg.m_ofn.lpstrFile,"r")) == NULL)
		{
			str.Format("打不开文件%s", dlg.m_ofn.lpstrFile);
			MessageBox(str, "提示", MB_OK | MB_ICONWARNING);
		}
		else 
		{
			while (feof(fp) == 0)
			{
				fgets(buf, 1023, fp);
				str = buf;
				str.TrimLeft();
				str.TrimRight();
				m_List1.InsertItem(nIndex, str.Left(str.Find(" ")));
				str.Delete(0, str.Find(" "));
				str.TrimLeft();
				str.TrimRight();
				m_List1.SetItemText(nIndex, 1, str.Left(str.Find(" ")));
				str.Delete(0, str.Find(" "));
				str.TrimLeft();
				str.TrimRight();
				m_List1.SetItemText(nIndex, 2, str);
				nIndex+=1;
			}
			fclose(fp);
		}
	}
}

void CAsDlg::press(int nCount)
{
	m_List2.DeleteAllItems();
	HEAPSORT **p = new HEAPSORT*[nCount];
	for (int i =0; i < nCount; i++)
		p[i] = &pStru[i];
	mtheap(p,nCount,0,nCount-1, sift_press);
	i = 0;
	int nIndex = 0;
	while (i < nCount)
	{
		m_List2.InsertItem(nIndex, p[i]->press);
		m_List2.SetItemText(nIndex, 1, p[i]->unknow);
		m_List2.SetItemText(nIndex, 2, p[i]->kind);
		nIndex += 1;
		if (i < nCount-1 && strcmp(p[i]->press, p[i+1]->press) != 0)
		{
			m_List2.InsertItem(nIndex, "");
			nIndex += 1;
		}
		i += 1;	
	}
	delete [] p;
}

void CAsDlg::unknow(int nCount)
{
	m_List2.DeleteAllItems();
	HEAPSORT **p = new HEAPSORT*[nCount];
	for (int i =0; i < nCount; i++)
		p[i] = &pStru[i];
	mtheap(p,nCount,0,nCount-1, sift_unknow);
	i = 0;
	int nIndex = 0;
	while (i < nCount)
	{
		m_List2.InsertItem(nIndex, p[i]->press);
		m_List2.SetItemText(nIndex, 1, p[i]->unknow);
		m_List2.SetItemText(nIndex, 2, p[i]->kind);
		nIndex += 1;
		if (i < nCount-1 && strcmp(p[i]->unknow, p[i+1]->unknow) != 0)
		{
			m_List2.InsertItem(nIndex, "");
			nIndex += 1;
		}
		i += 1;	
	}
	delete [] p;
}

void CAsDlg::kind(int nCount)
{
	m_List2.DeleteAllItems();
	HEAPSORT **p = new HEAPSORT*[nCount];
	for (int i =0; i < nCount; i++)
		p[i] = &pStru[i];
	mtheap(p,nCount,0,nCount-1, sift_kind);
	i = 0;
	int nIndex = 0;
	while (i < nCount)
	{
		m_List2.InsertItem(nIndex, p[i]->press);
		m_List2.SetItemText(nIndex, 1, p[i]->unknow);
		m_List2.SetItemText(nIndex, 2, p[i]->kind);
		nIndex += 1;
		if (i < nCount-1 && strcmp(p[i]->kind, p[i+1]->kind) != 0)
		{
			m_List2.InsertItem(nIndex, "");
			nIndex += 1;
		}
		i += 1;	
	}
	delete [] p;
}

void CAsDlg::OnOK() 
{
	// TODO: Add extra validation here
	if (pStru != NULL)
	{
		delete [] pStru;
	}
	CDialog::OnOK();
}

BOOL CAsDlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	if (pMsg->message == WM_KEYDOWN)
	{
		if (pMsg->wParam == VK_ESCAPE)
		{
			return TRUE;
		}
		else if (pMsg->wParam == VK_RETURN)
		{
			switch(nEnter)
			{
			case 1:
				m_code2.SetFocus(); break;
			case 2:
				m_code3.SetFocus(); break;
			case 3:
				UpdateData();
				int nIndex = 0;
				memset(&newStru, 0, sizeof(HEAPSORT));
				m_List1.InsertItem(nIndex, m_strCode1);
				m_List1.SetItemText(nIndex, 1, m_strCode2);
				m_List1.SetItemText(nIndex, 2, m_strCode3);
				strcpy(newStru.press, m_strCode1);
				strcpy(newStru.unknow, m_strCode2);
				strcpy(newStru.kind, m_strCode3);
				GetDlgItem(IDC_BT_START)->SetFocus();
				break;
			}
			nEnter += 1;
			return TRUE;
		}
	}
	return CDialog::PreTranslateMessage(pMsg);
}

void CAsDlg::OnSetfocusCode1() 
{
	// TODO: Add your control notification handler code here
	nEnter = 1;
	m_code1.SetWindowText("");
	m_code2.SetWindowText("");
	m_code3.SetWindowText("");
}

⌨️ 快捷键说明

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