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

📄 my_b_treeview.cpp

📁 B树的实现以及图形化显示
💻 CPP
字号:
// My_B_treeView.cpp : implementation of the CMy_B_treeView class
//
//////////////////////////////////////////////////////////////////////////
//姓名: 林文清
//学号: 0610374
//专业: 计算机科学与技术
//课程: 数据结构
//////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "My_B_tree.h"

#include "My_B_treeDoc.h"
#include "My_B_treeView.h"
#include "My_Creat_Btree.h"
#include "My_Fisrt_Insertion.h"
#include "My_Insert_Dlg.h"
#include "My_RemoveDlg.h"
#include "B_tree.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMy_B_treeView

IMPLEMENT_DYNCREATE(CMy_B_treeView, CView)

BEGIN_MESSAGE_MAP(CMy_B_treeView, CView)
	//{{AFX_MSG_MAP(CMy_B_treeView)
	ON_COMMAND(My_Creat_B_tree, OnCreatBtree)
	ON_COMMAND(My_Insertion, OnInsertion)
	ON_COMMAND(My_Remove, OnRemove)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy_B_treeView construction/destruction

B_tree<int> btree;   //声明B-Tree

CMy_B_treeView::CMy_B_treeView()
{
	// TODO: add construction code here
	options=0;
	whether_created=false;
}

CMy_B_treeView::~CMy_B_treeView()
{
}

BOOL CMy_B_treeView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMy_B_treeView drawing

void CMy_B_treeView::OnDraw(CDC* pDC)
{
	CMy_B_treeDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	Error_code result;
	if (options==1)   //创建B-Tree后,打印B-Tree
	{
		options=0;   //将选择标记清零
		My_Creat_Btree mycreatdlg;   //打开创建B-Tree的对话框
		mycreatdlg.DoModal();        //关掉对话框
		int order,num;               //阶数和初始插入关键字的个数
		order=mycreatdlg.get_order();//获取对话框输入的阶数
		num=mycreatdlg.get_num();    //获取对话框输入的关键字的个数
		if (order<3||num<0)         //若直接关掉对话框
		{
			AfxMessageBox("Fail to creat B_tree!\norder>=3?\nnum>=0?");
			return;
		}
		whether_created=true;           //是否创建B-Tree标记设为"是"
		result=btree.set_order(order);  //设置B-Tree阶数
		if (result==duplicate_error)    //若已经创建了B-Tree
		{
			AfxMessageBox("The B_tree have been created!");
		}
		else  //若还没有创建B-Tree
		{
			for (int i=0;i<num;i++)
			{
				My_Fisrt_Insertion myfirstinsertion;  //打开初始插入关键字的对话框
				myfirstinsertion.DoModal();
				result=btree.insert(myfirstinsertion.get_key());   //返回插入关键字的结果
				if (result==duplicate_error)   //若要插入的关键字在B-Tree中已有
				{
					AfxMessageBox("Duplicate Error!");
					i--;     //重新输入
				}
			}
		}
	}
	if (options==2)    //插入关键字
	{
		options=0;   //将选择标记清零
		if (!whether_created)  //若还没有创建B-Tree
		{
			AfxMessageBox("The B_tree havn't been created!");
		}
		else    //若已经创建B-Tree
		{
			My_Insert_Dlg myinsertdlg;   //打开插入关键字的对话框
			myinsertdlg.DoModal();
			result=btree.insert(myinsertdlg.get_key());   //返回插入关键字结果
			if (result==duplicate_error)  //若重复插入
			{
				AfxMessageBox("Duplicate Error!");
			}
		}
	}
	if (options==3)    //删除关键字
	{
		options=0;   //将选择标记清零
		if (!whether_created)   //若还没有创建B-Tree
		{
			AfxMessageBox("The B_tree havn't been created!");
		}
		else   //若已经创建B-Tree
		{
			My_RemoveDlg myremovedlg;  //打开删除关键字的对话框
			myremovedlg.DoModal();
			result=btree.remove(myremovedlg.get_key());   //返回删除关键字的结果
			if (result!=success)   //若删除不成功
			{
				AfxMessageBox("The key can't be removed!");
			}
		}
	}
	CRect rect;
	GetClientRect(rect);  //获取客户区矩形
	CSize size=rect.Size(); //获取客户区矩形的大小
	CPoint point;    //打印B-Tree的起始位置
	point.x=size.cx/2;
	point.y=5;
	btree.display(pDC,point);
}

/////////////////////////////////////////////////////////////////////////////
// CMy_B_treeView printing

BOOL CMy_B_treeView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMy_B_treeView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMy_B_treeView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMy_B_treeView diagnostics

#ifdef _DEBUG
void CMy_B_treeView::AssertValid() const
{
	CView::AssertValid();
}

void CMy_B_treeView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMy_B_treeDoc* CMy_B_treeView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy_B_treeDoc)));
	return (CMy_B_treeDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMy_B_treeView message handlers

void CMy_B_treeView::OnCreatBtree() 
{
	// TODO: Add your command handler code here
	options=1;
	InvalidateRect(NULL);
}

void CMy_B_treeView::OnInsertion() 
{
	// TODO: Add your command handler code here
	options=2;
	InvalidateRect(NULL);
}

void CMy_B_treeView::OnRemove() 
{
	// TODO: Add your command handler code here
	options=3;
	InvalidateRect(NULL);
}

⌨️ 快捷键说明

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