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

📄 mycontroldlg.cpp

📁 B+树的演示程序
💻 CPP
字号:
// MyControlDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "MyControlDlg.h"
#include "BPlusTree.h"
#include "MainFrm.h"
#include ".\mycontroldlg.h"

// CMyControlDlg 对话框

IMPLEMENT_DYNAMIC(CMyControlDlg, CDialog)
CMyControlDlg::CMyControlDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyControlDlg::IDD, pParent)
	, m_N(0)
	, m_key(0)
	, m_BView(FALSE)
{
}

CMyControlDlg::~CMyControlDlg()
{
}

void CMyControlDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_N, m_N);
	DDV_MinMaxInt(pDX, m_N, MINIMUS_NUMBER_OF_NODE, MAXIMUM_NUMBER_OF_NODE);
	DDX_Text(pDX, IDC_EDIT_KEY, m_key);
	DDV_MinMaxLong(pDX, m_key, MINIMUS_NUMBER_OF_KEY, MAXIMUM_NUMBER_OF_KEY);
	DDX_Control(pDX, IDC_SPIN1, m_spin);
	DDX_Check(pDX, IDC_CHECK1, m_BView);
}


BEGIN_MESSAGE_MAP(CMyControlDlg, CDialog)
	ON_BN_CLICKED(IDC_BTN_CREATE_TREE, OnBnClickedBtnCreateTree)
	ON_BN_CLICKED(IDC_BTN_DELETE_TREE, OnBnClickedBtnDeleteTree)
	ON_BN_CLICKED(IDC_BTN_SEARCH, OnBnClickedBtnSearch)
	ON_BN_CLICKED(IDC_BTN_INSERT, OnBnClickedBtnInsert)
	ON_BN_CLICKED(IDC_BTN_DELETE, OnBnClickedBtnDelete)
	ON_BN_CLICKED(IDC_BTN_INSERT_STORE, OnBnClickedBtnInsertStore)
	ON_BN_CLICKED(IDC_BTN_INSERT_RANDOM, OnBnClickedBtnInsertRandom)
	ON_BN_CLICKED(IDC_BTN_DELETE_RANDOM, OnBnClickedBtnDeleteRandom)
	ON_BN_CLICKED(IDC_CHECK1, OnBnClickedCheck1)
END_MESSAGE_MAP()


// CMyControlDlg 消息处理程序

void CMyControlDlg::CreateSpinCtrl(void)
{
	CWnd* pEdit = GetDlgItem(IDC_EDIT_N);
	m_spin.SetRange(3,10);
	m_spin.SetBuddy(pEdit);
	m_spin.SetPos(3);
}

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

	// TODO:  在此添加额外的初始化
	GetDlgItem(IDC_BTN_CREATE_TREE)->SetWindowText(_T("新建B+树"));
	m_N = 3;
	srand((unsigned)time(NULL));
	m_key = rand()%100;
	m_BView = TRUE;
	CreateSpinCtrl();
	UpdateData(FALSE);

	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}

void CMyControlDlg::OnBnClickedBtnCreateTree()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData();
	CString str;
	GetDlgItem(IDC_BTN_CREATE_TREE)->GetWindowText(str);
	if(str == "新建B+树")
	{
		GetDlgItem(IDC_BTN_CREATE_TREE)->SetWindowText(_T("修改N值"));
		GetDlgItem(IDC_BTN_DELETE_TREE)->EnableWindow();
		GetDlgItem(IDC_EDIT_KEY)->EnableWindow();
		GetDlgItem(IDC_BTN_SEARCH)->EnableWindow();
		GetDlgItem(IDC_BTN_INSERT)->EnableWindow();
		GetDlgItem(IDC_BTN_DELETE)->EnableWindow();
		GetDlgItem(IDC_BTN_INSERT_STORE)->EnableWindow();
		GetDlgItem(IDC_BTN_INSERT_RANDOM)->EnableWindow();
		GetDlgItem(IDC_BTN_DELETE_RANDOM)->EnableWindow();

		((CMainFrame*)AfxGetMainWnd())->m_control.Create(m_N);
		((CMainFrame*)AfxGetMainWnd())->m_control.DisplayTree();
	}
	else
	{
		int i = ((CMainFrame*)AfxGetMainWnd())->m_control.ModifyTree(m_N);
		if(i == -1)
			MessageBox("N should at the range of 3 to 10.\nPlease try againg");
	}
}

void CMyControlDlg::OnBnClickedBtnDeleteTree()
{
	// TODO: 在此添加控件通知处理程序代码
	GetDlgItem(IDC_BTN_CREATE_TREE)->SetWindowText(_T("新建B+树"));
	GetDlgItem(IDC_BTN_DELETE_TREE)->EnableWindow(FALSE);
	GetDlgItem(IDC_EDIT_KEY)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_SEARCH)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_INSERT)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_DELETE)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_INSERT_STORE)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_INSERT_RANDOM)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_DELETE_RANDOM)->EnableWindow(FALSE);
	((CMainFrame*)AfxGetMainWnd())->m_control.DeleteTree();	
}

void CMyControlDlg::OnBnClickedBtnSearch()
{
	UpdateData();
	int i = (int) ((CMainFrame*)AfxGetMainWnd())->m_control.Search(m_key);
    if(i == -1)
		MessageBox("Sorry!\nThere is no this key.");
	// else
	//	MessageBox("Ok!\nThis key is in the B+ tree.\n");
}

void CMyControlDlg::OnBnClickedBtnInsert()
{
	UpdateData();
	int i = ((CMainFrame*)AfxGetMainWnd())->m_control.Insert(m_key);
	m_key = rand()%100;
	UpdateData(FALSE);
	if(i == -1)
	{
		MessageBox("Sorry!\nThis key is already in the B+ tree.\nPlease try another one.");
		return;
	}
	else if(i == -2)
	{
		MessageBox("ERROR!\nMemory error.Please check and try again a few minutes.");
		return;
	}
	else if(i == -3)
	{
		MessageBox("Sorry!This is a shared version.\nYou can only input 100 key to try.\nIf you want more. Please buy it now.");
		return;
	}	
}

void CMyControlDlg::OnBnClickedBtnDelete()
{
	UpdateData();
	int i = ((CMainFrame*)AfxGetMainWnd())->m_control.Delete(m_key);
	if(i == -1)
	{
		MessageBox("Sorry!\nThere isn't this key in B+ tree.\nPlease try another one.");
		return;
	}
	else if(i == -2)
	{
		MessageBox("Warning!\nThere is none key in B+ tree.\nYou can't delete anyone else.");
		return;
	}
}

void CMyControlDlg::OnBnClickedBtnInsertStore()
{
	((CMainFrame*)AfxGetMainWnd())->m_control.InsertStore();
}

void CMyControlDlg::OnBnClickedBtnInsertRandom()
{
	UpdateData();
	int i = ((CMainFrame*)AfxGetMainWnd())->m_control.PreInsertRandom(m_key);
	CString str;
	if(i < 0)
	{
		int num = -i;		
		str.Format("Sorry!This is a shared version.\nYou can only insert %d random key to try.\nIf you want more. Please buy it now.",num);
		MessageBox(str);
		return;
	}

	str.Format("Are you sure insert %d random long key",i);
	if(MessageBox(str,"Insert Randmon",MB_OKCANCEL) == IDOK)
		((CMainFrame*)AfxGetMainWnd())->m_control.InsertRandom(i);
}

void CMyControlDlg::OnBnClickedBtnDeleteRandom()
{
	GetDlgItem(IDC_BTN_DELETE_RANDOM)->EnableWindow(FALSE);
	int i = ((CMainFrame*)AfxGetMainWnd())->m_control.DeleteRandom();
	if(i == -1)
		MessageBox("Sorry!\nThere isn't this key in B+ tree.\nPlease try another one.");
	else if(i == -2)
		MessageBox("Warning!\nThere is none key in B+ tree.\nYou can't delete anyone else.");
	GetDlgItem(IDC_BTN_DELETE_RANDOM)->EnableWindow();}

void CMyControlDlg::OnBnClickedCheck1()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData();
	((CMainFrame*)AfxGetMainWnd())->m_BView = m_BView;
}

⌨️ 快捷键说明

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