// DisplayDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Display.h"
#include "DisplayDlg.h"
#include <math.h>

#include <conio.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()

/////////////////////////////////////////////////////////////////////////////
// CDisplayDlg dialog

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

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

/////////////////////////////////////////////////////////////////////////////
// CDisplayDlg message handlers

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

#define MAX_TEST_ITEM_COUNT            128
#define MAX2_TEST_ITEM_COUNT           128
#define TEST_ITEM_COUNT                 48
#define  MAX_ARMATURE_SUPPORT_COUNT     40



typedef struct _tagENVIRONMENTALEX
{
	//ARMATURE_TEST
	char  strModelName[24];
	char  strFile[24];

	int   nBar;
	DWORD dwStyle;
	int   nMachineDot;

	DWORD dwCodePoint;
	DWORD dwCodeStart;
	DWORD dwCodeEnd;

	float fHipotVol;
	float fHipotTime;
	float fHipotCur[3][2];
	float fInsulationVol;
	float fInsulationRes;

	float fsResistance[MAX_ARMATURE_SUPPORT_COUNT][2];
	float fWeldRes;
	float fSurgePercent;

	float fSurgeVol;
	int   nSurgeInternal;
	float fSurgeSampleFreq;

	int   nResistanceRange;
	int   nInsulation;

	BOOL  bsHipot[3];
	BOOL  bInsulation;
	BOOL  bBar;
	BOOL  bWeld;
	BOOL  bSurge;

	BOOL  bsSurge[MAX_ARMATURE_SUPPORT_COUNT];

	//STATOR
	float fStartupCapacitance;
	BOOL  bThreePhase;
	BOOL  bTStyle;
	BOOL  bThreeStyle;

	//VBTION_710

	float fsAmp[2];
	float fsStartAmp[2];
	float fsVol[2];
	float fsSpeed[2];
	float fsVbtionA[2];
	float fsVbtionV[2];
	float fsVbtionS[2];
	float fsFilter[2];
	float fRunningTime;
	int   nDivide;
	int   nFreqResolution;

	BOOL bAutoRange;
	int  nAmpRange;
	int  nStartAmpRange;
	BOOL  bsMask[TEST_ITEM_COUNT];

	//NORMAL
	float fsTest[TEST_ITEM_COUNT][2];
	BOOL  bsTest[TEST_ITEM_COUNT];
}ENVIRONMENTALEX;


#define MAX_TREE_DEGREE 30



#define TRAVERSING_BINARYTREE_FIRST  0 
#define TRAVERSING_BINARYTREE_MIDDLE 1 
#define TRAVERSING_BINARYTREE_LAST   2 

struct CTreeNode
{
	//指向用户信息
	void* pUser;
	//父结点
	CTreeNode* pParent;

	union
	{
		//二叉种结构
		struct
		{
			//若wFlagL = 0, pChildL表左孩子
			//若wFlagL = 1, pChildL表前驱
			WORD wFlagL; 

			//左结点
			CTreeNode* pChildL;

			//若wFlagR = 0, pChildL表右孩子
			//若wFlagR = 1, pChildL表后续
			WORD wFlagR;

			//右结点
			CTreeNode* pChildR;
		}BinaryTree;

		//树结构
		struct
		{
			//若wFlagL = 0, pChild表第一个孩子
			//若wFlagL = 1, pChild表前驱
			WORD wFlagL; 

			//第一个孩子
			CTreeNode* pChild;

			//若wFlagR = 0, pSibling表兄弟
			//若wFlagR = 1, pSibling表后续
			WORD wFlagR; 

			//兄弟
			CTreeNode* pSibling;
		}Tree;
	}Node;
};

//递归的结构体
struct BINARYTREE_RECURSION//recursion
{
	DWORD dwP0; //返回语句标号
	CTreeNode* pTreeNode; //结点指针
};

class CTree
{
protected:
	//度
	int m_nDegree;
	//最大度
	int m_nMaxDegree;
	//列指针
	int* m_pnCol;
	//根结点
	CTreeNode* m_pRoot;
	//遍历类型
	int m_nVisitStyle;
	//遍历参数
	void* m_pVisitUser;
	//析离参数	
	void* m_pMoveUser;
	//删除参数
	void* m_pDeleteUser;
	//遍历筛选函数
	CTreeNode* (*m_pVisitFc)(CTree*, CTreeNode*, void*, int, int);
	//析离结点操作
	CTreeNode* (*m_pMoveFc)(CTreeNode*, void*);
	//删除函数
	CTreeNode* (*m_pDeleteFc)(CTreeNode*, void*);

public:
	//树初始化
	void InitiateTree();
	//树清空
	BOOL ClearTree();
	//求树的根结点
	CTreeNode* GetRoot();
	//求结点所在树的根结点
	static CTreeNode* GetRoot(CTreeNode *pTreeNode);
	//求双亲
	CTreeNode* GetParent(CTreeNode *pTreeNode);
	//求孩子
	CTreeNode* GetChild(CTreeNode* pTreeNode, DWORD dwIndex);
	//求右兄弟
	CTreeNode* GetRightSibling(CTreeNode* pTreeNode, DWORD dwIndex);
	//求第一个兄弟
	CTreeNode* GetRightSibling_First(CTreeNode* pTreeNode)
	{
		return(this->GetRightSibling(pTreeNode, 0));
	}
	//求最后一个兄弟
	CTreeNode* GetRightSibling_Last(CTreeNode* pTreeNode)
	{
		return(this->GetRightSibling(pTreeNode, 0xFFFFFFFFlu));
	}

	//建立一棵以指定结点为根的树
	CTree* BuildTree(CTreeNode* pTreeNode);
	//插入子树
	BOOL InsertChild(CTreeNode* pTarget, DWORD dwIndex, CTreeNode* pSource);
	//删除子树
	BOOL DeleteChild(CTreeNode* pTreeNode, DWORD dwIndex);
	//删除指定结点为根结点的树
	BOOL DeleteChild(CTreeNode* pTreeNode);
	//遍历操作
	BOOL TraverseTree(CTreeNode* pTreeNode, int nStyle, void* pUser);

public:
	//设置遍历类型
	void SetVisitStyle(int nStyle) {m_nVisitStyle = nStyle;}
	//设置遍历函数
	void SetVisitFc(CTreeNode* (*pFc)(CTree*, CTreeNode*, void*, int, int)) {m_pVisitFc = pFc;}
	//设置遍历参数
	void SetVisitUser(void* pUser) {m_pVisitUser = pUser;}

	//设置析离函数
	void SetMoveFc(CTreeNode* (*pFc)(CTreeNode*, void*)) {m_pMoveFc = pFc;}
	//设置析离参数
	void SetMoveUser(void* pUser) {m_pMoveUser = pUser;}

	//设置删除函数
	void SetDeleteFc(CTreeNode* (*pFc)(CTreeNode*, void*)) {m_pDeleteFc = pFc;}
	//设置删除参数
	void SetDeleteUser(void* pUser) {m_pDeleteUser = pUser;}
public:
	int GetDegree(){return(m_nDegree);}
	int GetMaxDegree(){return(m_nMaxDegree);}	
protected:
	//计算度用的访问函数
	static CTreeNode* CalcDegreeFc(CTree* pTree, CTreeNode* pTreeNode, 
		void* pUser, int nDegree, int nCol);
	//访问结点操作
	CTreeNode* VisitTree(CTreeNode* pTreeNode, void* pUser, int nCol);
	//析离结点操作
	CTreeNode* MoveTree(CTreeNode* pTreeNode, void* pUser);
	//删除结点操作
	CTreeNode* DeleteTree(CTreeNode* pTreeNode, void* pUser);

	//初始化成员变量
	void CoTree();
public:
	//构造函数
	CTree();
	//析构函数
	~CTree();
};


class CBinaryTree:public CTree
{	
public:
	//求左边孩子
	CTreeNode* GetChildL(CTreeNode* pTreeNode) 
	{
		return (pTreeNode->Node.BinaryTree.pChildL);
	}
	//求右边孩子
	CTreeNode* GetChildR(CTreeNode* pTreeNode)
	{
		return (pTreeNode->Node.BinaryTree.pChildR);
	}
	//求左兄弟
	CTreeNode* GetSiblingL(CTreeNode* pTreeNode);
	//求右兄弟
	CTreeNode* GetSiblingR(CTreeNode* pTreeNode);
	//建立一棵以指定结点为根的树
	BOOL BuildBinaryTree(CTreeNode* pRoot,
		CTreeNode* pChildL, CTreeNode* pChildR);
	//插入左子树
	BOOL InsertChildL(CTreeNode* pNode, CTreeNode* pChildL);
	//插入右子树
	BOOL InsertChildR(CTreeNode* pNode, CTreeNode* pChildR);
	//删除左子树
	BOOL DeleteChildL(CTreeNode* pNode);
	//删除右子树
	BOOL DeleteChildR(CTreeNode* pNode);
	//遍历树操作
	CTreeNode* TraverseBinaryTree(
		CTreeNode* (*pFc)(CTree* pTree, CTreeNode*, void*, int, int), void* pUser, int nStyle);
	//二叉种的线索化
	BOOL Thread();
public:
	int CalcMaxDegree();
	static void CBinaryTree::CalcCoordinate(RECT* pRc, int nMaxDegree, int nDegree,
		int nCol, int* pnLeft, int* pnTop);
private:
	//指定结点作为根的遍历树操作
	CTreeNode* TraverseBinaryTree(CTreeNode* pTreeNode);
};


//函数功能：初始化成员变量
void CTree::CoTree()
{
	//度
	m_nDegree = 1;
	//树的度数
	m_nMaxDegree = 1;	
	//列
	m_pnCol = NULL;
	//根结点
	m_pRoot = NULL;
	//遍历类型
	m_nVisitStyle = TRAVERSING_BINARYTREE_MIDDLE;
	//遍历参数
	m_pVisitUser = NULL;
	//析离参数	
	m_pMoveUser = NULL;
	//删除参数
	m_pDeleteUser = NULL;
	//遍历筛选函数
	m_pVisitFc = NULL;
	//析离结点操作
	m_pMoveFc = NULL;
	//删除函数
	m_pDeleteFc = NULL;
}

//函数功能：构造函数
CTree::CTree()
{
	//初始化成员变量
	this->CoTree();

	//初始化树
	this->InitiateTree();
}

//函数功能：析构函数
CTree::~CTree()
{
	this->ClearTree();
}

//函数功能：初始化树
void CTree::InitiateTree()
{
	this->ClearTree();

	//初始化成员变量
	this->CoTree();

}

//函数功能：清除树
BOOL CTree::ClearTree()
{
	m_pRoot = NULL;

	if (m_pnCol != NULL)
	{
		delete m_pnCol;
		m_pnCol = NULL;
	}
	return (TRUE);
}

//函数功能：求树的根结点
CTreeNode* CTree::GetRoot()  
{
	return(m_pRoot);
}


//函数功能：得到根结点
CTreeNode* CTree::GetRoot(CTreeNode *pTreeNode)
{
	if (pTreeNode == NULL)
	{
		return (NULL);
	}
	
	while (NULL != pTreeNode->pParent)
	{
		pTreeNode = pTreeNode->pParent;
	}
	return(pTreeNode);
}

//函数功能：求双亲
CTreeNode* CTree::GetParent(CTreeNode *pTreeNode)
{
	return(pTreeNode->pParent);
}

//函数功能：求孩子
CTreeNode* CTree::GetChild(CTreeNode* pTreeNode, DWORD dwIndex)
{
	//判断结点的合法性
	if (pTreeNode == NULL)
	{
		return(NULL);
	}

	CTreeNode* pChild = pTreeNode->Node.Tree.pChild;

	DWORD dwPoint = 0;
	while ((dwPoint++) < dwIndex && NULL != pChild)
	{
		//如果是最后一棵子树
		if (dwIndex == 0xFFFFFFFFlu  && pChild->Node.Tree.pSibling == NULL)
		{
			break;
		}

		pChild = pChild->Node.Tree.pSibling;
	}
	

	return(pChild);
}

//函数功能：求右兄弟
CTreeNode* CTree::GetRightSibling(CTreeNode* pTreeNode, DWORD dwIndex)
{
	//判断结点的合法性
	if (pTreeNode == NULL)
	{
		return(NULL);
	}

	CTreeNode* pSibling = pTreeNode->Node.Tree.pSibling;

	DWORD dwPoint = 0;
	while ((dwPoint++) < dwIndex && NULL != pSibling)
	{
		//如果是最后一棵子树
		if (dwIndex == 0xFFFFFFFFlu  && pSibling->Node.Tree.pSibling == NULL)
		{
			break;
		}

		pSibling = pSibling->Node.Tree.pSibling;
	}
	

	return(pSibling);
}

//函数功能：插入子树
BOOL CTree::InsertChild(CTreeNode* pTarget, DWORD dwIndex, CTreeNode* pSource)
{
	//判断指针的合法性
	if (NULL == pTarget || NULL == pSource)
	{
		return(FALSE);
	}


	CTreeNode* pChild = NULL;
	//第一棵子树
	if (dwIndex == 0)
	{
		pSource->pParent = pTarget;
		pSource->Node.Tree.pSibling = pTarget->Node.Tree.pChild;
		pTarget->Node.Tree.pChild = pSource;
		return (TRUE);
	}
	//最后一棵子树
	else if (dwIndex == 0xFFFFFFFFlu)
	{
		pSource->pParent = pTarget;

		pChild = this->GetChild(pTarget, 0xFFFFFFFFlu);
		//第一棵子树
		if (pChild == NULL)
		{			
			pSource->Node.Tree.pSibling = pTarget->Node.Tree.pChild;
			pTarget->Node.Tree.pChild = pSource;
		}
		//最后一棵子树
		else
		{
			pSource->Node.Tree.pSibling = NULL;
			pChild->Node.Tree.pSibling = pSource;
		}
		return(TRUE);
	}
	//不是第一棵子树
	else
	{
		pChild = this->GetChild(pTarget, dwIndex - 1);
		if (pChild != NULL)
		{
			pSource->pParent = pTarget;
			pSource->Node.Tree.pSibling = pChild->Node.Tree.pSibling;
			pChild->Node.Tree.pSibling = pSource;
			return (TRUE);
		}
	}

	return(FALSE);
}



//函数功能：删除子树
BOOL CTree::DeleteChild(CTreeNode* pTreeNode, DWORD dwIndex)
{
	if (pTreeNode == NULL)
	{
		return(FALSE);
	}

	CTreeNode* pSibling = NULL;
	CTreeNode* pSibling2 = NULL;
	CTreeNode* pChild = this->GetChild(pTreeNode, dwIndex);

	if (pChild == NULL)
	{
		return(FALSE);
	}

	//第一个孩子
	if (dwIndex == 0)
	{
		//修改当前结点的第一个孩子：结点的兄弟结点作为当前结点的孩子
		pTreeNode->Node.Tree.pChild = 
			pTreeNode->Node.Tree.pChild->Node.Tree.pSibling;
	}
	//最后一个孩子
	else if (dwIndex == 0xFFFFFFFFlu)
	{
		pChild = pTreeNode->Node.Tree.pChild;

		pSibling2 = pChild;
		pSibling = pChild->Node.Tree.pSibling;

		while (pSibling->Node.Tree.pSibling != NULL)
		{
			pSibling2 = pSibling;
			pSibling = pSibling->Node.Tree.pSibling;
		}
			

		//第一个孩子
		if (pSibling == NULL)
		{
			//修改当前结点的第一个孩子：NULL
			pTreeNode->Node.Tree.pChild = NULL;;
		}
		//不是第一个孩子
		else
		{
			pSibling2->Node.Tree.pSibling = NULL;

			//修改要删除结点为孩子
			pChild = pSibling;		
		}

	}
	//中间孩子
	else
	{		
		//后一个兄弟		
		pSibling = this->GetChild(pTreeNode, dwIndex + 1);
		//前一个兄弟		
		pSibling2 = this->GetChild(pTreeNode, dwIndex - 1);

		//修改兄弟
		pSibling2->Node.Tree.pSibling = pSibling;
	}

	//删除指定结点
	pChild->pParent = NULL;
	pChild->Node.Tree.pSibling = NULL;
	this->DeleteTree(pChild, m_pDeleteUser);	
	return(FALSE);
}

//函数功能：访问结点操作
CTreeNode* CTree::VisitTree(CTreeNode* pTreeNode, void* pUser, int nCol)
{
	if (m_pVisitFc == NULL)
	{
		return(NULL);
	}
	return (m_pVisitFc(this, pTreeNode, pUser, m_nDegree, nCol));
}

//函数功能：析离结点操作
CTreeNode* CTree::MoveTree(CTreeNode* pTreeNode, void* pUser)
{
	if (m_pMoveFc == NULL)
	{
		return(NULL);
	}
	return (m_pMoveFc(pTreeNode, pUser));
}

//函数功能：删除结点操作
CTreeNode* CTree::DeleteTree(CTreeNode* pTreeNode, void* pUser)
{
	if (m_pDeleteFc == NULL)
	{
		return(NULL);
	}
	return (m_pDeleteFc(pTreeNode, pUser));
}

int CBinaryTree::CalcMaxDegree()
{
	this->TraverseBinaryTree(CTree::CalcDegreeFc, 
		NULL, TRAVERSING_BINARYTREE_FIRST);
	return (m_nMaxDegree);
}

//函数功能：建立树
BOOL CBinaryTree::BuildBinaryTree(CTreeNode* pRoot,
		CTreeNode* pChildL, CTreeNode* pChildR)
{
	this->ClearTree();
	
	if (pRoot == NULL)
	{
		return(TRUE);
	}

	//设置根结点
	m_pRoot = pRoot;
	//设置左结点
	m_pRoot->Node.BinaryTree.wFlagL = 0;
	m_pRoot->Node.BinaryTree.pChildL = pChildL;
	//设置右结点
	m_pRoot->Node.BinaryTree.wFlagR = 0;
	m_pRoot->Node.BinaryTree.pChildR = pChildR;

	//设置左孩子结点的父亲
	if (NULL != pChildL)
	{
		pChildL->pParent = pRoot;
	}

	//设置右孩子结点的父亲
	if (NULL != pChildR)
	{
		pChildR->pParent = pRoot;
	}

	return(TRUE);
}


//遍历筛选函数
CTreeNode* CTree::CalcDegreeFc(CTree* pTree, CTreeNode* pTreeNode, void* pUser, int nDegree, int nCol)
{
	return(NULL);
}


//函数功能：插入左子树
BOOL CBinaryTree::InsertChildL(CTreeNode* pNode, CTreeNode* pChildL)
{
	//移去原来的右子树
	if ((pNode->Node.BinaryTree.pChildL != NULL && 
			m_pRoot->Node.BinaryTree.wFlagL == 0) && NULL != m_pMoveFc)
	{
		m_pMoveFc(pNode->Node.BinaryTree.pChildL, m_pMoveUser);
		pNode->Node.BinaryTree.pChildL = NULL;
	}
	
	//设置新的右子树
	m_pRoot->Node.BinaryTree.wFlagL = 0;
	pNode->Node.BinaryTree.pChildL = pChildL;

	//设置子树的父亲
	if (pChildL != NULL)
	{
		pChildL->pParent = pNode;
	}
	return(TRUE);
}
//函数功能：插入右子树
BOOL CBinaryTree::InsertChildR(CTreeNode* pNode, CTreeNode* pChildR)
{
	//移去原来的右子树
	if ((pNode->Node.BinaryTree.pChildR != NULL && 
		m_pRoot->Node.BinaryTree.wFlagR == 0 ) &&NULL != m_pMoveFc)
	{
		m_pMoveFc(pNode->Node.BinaryTree.pChildR, m_pMoveUser);
		pNode->Node.BinaryTree.pChildR = NULL;
	}
	
	//设置新的右子树
	m_pRoot->Node.BinaryTree.wFlagR = 0;
	pNode->Node.BinaryTree.pChildR = pChildR;

	//设置子树的父亲
	if (pChildR != NULL)
	{
		pChildR->pParent = pNode;
	}
	return(TRUE);
}
//函数功能：以结点为根的遍历操作
CTreeNode* CBinaryTree::TraverseBinaryTree(CTreeNode* pTreeNode)
{
	CTreeNode* pTreeNode_Target = NULL;
	//指针不能为空
	if (pTreeNode == NULL)
	{
		if (m_pnCol != NULL && m_nDegree >= 2)
		{
			for (int i = m_nDegree + 1; i < MAX_TREE_DEGREE; i++)
			{
				m_pnCol[i - 1] += 1 << (i - m_nDegree);
			}

		}
		return(NULL);
	}

	int nOldDegree = m_nDegree;
	//统计最大度
	if (m_nMaxDegree < m_nDegree)
	{
		m_nMaxDegree = m_nDegree;
	}

	//根据不同的遍历类型，作出“先序”、“中序”、“后序”遍历树操作
	switch (m_nVisitStyle)
	{
		//先序遍历
		case TRAVERSING_BINARYTREE_FIRST:
		{
			//度计算
			m_nDegree += 0;			
			//访问根结点
			pTreeNode_Target = this->VisitTree(
				pTreeNode, m_pVisitUser, m_pnCol[m_nDegree - 1]);
			//列计算
			if (m_pnCol != NULL)
			{
				m_pnCol[m_nDegree - 1] += 1;
			}
			//是否是返回结果
			if (pTreeNode_Target != NULL)
			{
				return(pTreeNode_Target);
			}

			//度计算
			m_nDegree++;
			//遍历左子树
			if (0 == pTreeNode->Node.BinaryTree.wFlagL)
			{
				pTreeNode_Target = this->TraverseBinaryTree(
					pTreeNode->Node.BinaryTree.pChildL);
			}
			//列计算
			if (m_pnCol != NULL && 
				(pTreeNode->Node.BinaryTree.pChildL == NULL &&
					pTreeNode->Node.BinaryTree.wFlagL == 0))
			{
				m_pnCol[m_nDegree - 1] += 1;
			}
			//是否返回结果
			if(pTreeNode_Target != NULL)
			{
				return (pTreeNode_Target);
			}
	

			//度计算
			m_nDegree += 0;
			//遍历右子树
			if (0 == pTreeNode->Node.BinaryTree.wFlagR)
			{
				pTreeNode_Target = this->TraverseBinaryTree(
					pTreeNode->Node.BinaryTree.pChildR);
			}
			//列计算
			if (m_pnCol != NULL && 
				(pTreeNode->Node.BinaryTree.pChildR == NULL &&
					pTreeNode->Node.BinaryTree.wFlagR == 0))
			{
				m_pnCol[m_nDegree - 1] += 1;
			}
			//是否返回结果
			if(pTreeNode_Target != NULL)
			{
				return (pTreeNode_Target);
			}


			break;
		}
		//中序遍历
		case TRAVERSING_BINARYTREE_MIDDLE:
		{
			//度计算
			m_nDegree++;
			//遍历左子树
			if (0 == pTreeNode->Node.BinaryTree.wFlagL)
			{
				pTreeNode_Target = this->TraverseBinaryTree(
					pTreeNode->Node.BinaryTree.pChildL);
			}
			//列计算
			if (m_pnCol != NULL && 
				(pTreeNode->Node.BinaryTree.pChildL == NULL &&
					pTreeNode->Node.BinaryTree.wFlagL == 0))
			{
				m_pnCol[m_nDegree - 1] += 1;
			}
			//是否返回结果
			if(pTreeNode_Target != NULL)
			{
				return (pTreeNode_Target);
			}

			//度计算
			m_nDegree--;
			//访问根结点
			pTreeNode_Target = this->VisitTree(
				pTreeNode, m_pVisitUser, m_pnCol[m_nDegree - 1]);
			//列计算
			if (m_pnCol != NULL)
			{
				m_pnCol[m_nDegree - 1] += 1;
			}
			//是否是返回结果
			if (pTreeNode_Target != NULL)
			{
				return(pTreeNode_Target);
			}


			//度计算
			m_nDegree += 1;
			//遍历右子树
			if (pTreeNode->Node.BinaryTree.wFlagR == 0)
			{
				pTreeNode_Target = this->TraverseBinaryTree(
					pTreeNode->Node.BinaryTree.pChildR);
			}
			//列计算
			if (m_pnCol != NULL && 
				(pTreeNode->Node.BinaryTree.pChildR == NULL &&
					pTreeNode->Node.BinaryTree.wFlagR == 0))
			{
				m_pnCol[m_nDegree - 1] += 1;
			}
			//是否返回结果
			if(pTreeNode_Target != NULL)
			{
				return (pTreeNode_Target);
			}

			break;
		}
		//后序遍历
		case TRAVERSING_BINARYTREE_LAST:
		{
			//度计算
			m_nDegree++;
			//遍历左子树
			if (pTreeNode->Node.BinaryTree.wFlagL == 0)
			{
				pTreeNode_Target = this->TraverseBinaryTree(
					pTreeNode->Node.BinaryTree.pChildL);
			}
			//列计算
			if (m_pnCol != NULL && 
				(pTreeNode->Node.BinaryTree.pChildL == NULL &&
					pTreeNode->Node.BinaryTree.wFlagL == 0))
			{
				m_pnCol[m_nDegree - 1] += 1;
			}
			//是否返回结果
			if(pTreeNode_Target != NULL)
			{
				return (pTreeNode_Target);
			}

			//度计算
			m_nDegree += 0;
			//遍历右子树
			if (pTreeNode->Node.BinaryTree.wFlagR == 0)
			{
				pTreeNode_Target = this->TraverseBinaryTree(
					pTreeNode->Node.BinaryTree.pChildR);
			}
			//列计算
			if (m_pnCol != NULL && 
				(pTreeNode->Node.BinaryTree.pChildR == NULL &&
					pTreeNode->Node.BinaryTree.wFlagR == 0))
			{
				m_pnCol[m_nDegree - 1] += 1;
			}
			//是否返回结果
			if(pTreeNode_Target != NULL)
			{
				return (pTreeNode_Target);
			}

			//度计算
			m_nDegree--;
			//访问根结点
			pTreeNode_Target = this->VisitTree(
				pTreeNode, m_pVisitUser, m_pnCol[m_nDegree - 1]);
			//列计算
			if (m_pnCol != NULL)
			{
				m_pnCol[m_nDegree - 1] += 1;
			}
			//是否是返回结果
			if (pTreeNode_Target != NULL)
			{
				return(pTreeNode_Target);
			}

			break;
		}
		//其它
		default:
		{
			break;
		}
	}
	m_nDegree = nOldDegree;
	return(NULL);
}


//函数功能：遍历操作
CTreeNode* CBinaryTree::TraverseBinaryTree(
	CTreeNode* (*pFc)(CTree* pTree, CTreeNode*, void*, int, int), 
	void* pUser, int nStyle)
{
	m_nDegree = 0;
	if (pFc == CTree::CalcDegreeFc)
	{
		m_nMaxDegree = 0;
	}
	//根结点不能为空
	if (m_pRoot == NULL)
	{
		return(NULL);
	}
	
	//设置“度”= 0
	m_nDegree = 1;
	if (pFc == CTree::CalcDegreeFc)
	{
		m_nMaxDegree = 1;
	}
	//列
	m_pnCol = new int[MAX_TREE_DEGREE];
	for (int i = 0; i < MAX_TREE_DEGREE; i++)
	{
		m_pnCol[i] = 0;
	}

	//设置遍历的类型
	this->SetVisitStyle(nStyle);
	//设置遍历的内容
	this->SetVisitUser(pUser);
	//设置遍历函数
	this->SetVisitFc(pFc);

	//从根结点开始遍历
	CTreeNode* pTreeNode = this->TraverseBinaryTree(m_pRoot);

	//删除列
	delete m_pnCol;
	m_pnCol = NULL;

	//返回遍历结果
	return (pTreeNode);
}




void CBinaryTree::CalcCoordinate(RECT* pRc, int nMaxDegree, 
	int nDegree, int nCol, int* pnLeft, int* pnTop)
{
	int nLeft, nTop;	
	//计算最大度的总列数
	int nNodes = 1 << (nMaxDegree - 1);

	//计算“当前度”结点Y向距离
	float fInternalY = 0.0f;
	if (nMaxDegree > 1)
	{
		fInternalY = 1.0f * (pRc->bottom - pRc->top) / (nMaxDegree - 1);
	}

	//计算“最大度”的X向分隔距离
	float fInternalX = 0.0f;
	if (nMaxDegree > 1)
	{
		fInternalX = 1.0f * (pRc->right - pRc->left) / (nNodes - 1);
	}

	//计算“当前度”的Y向坐标值
	nTop = (int)(pRc->top + fInternalY * (nDegree - 1));

	//如果是第一个度
	if (nDegree == 1)
	{
		nLeft = (int)(pRc->left + pRc->right)/2;
	}
	//中间的度
	else if (nDegree != nMaxDegree)
	{
		int nColR1 = (nCol + 1) * (1 << (nMaxDegree - nDegree)) - 1;
		int nColR2 = (nCol + 0) * (1 << (nMaxDegree - nDegree)) + 0;

		int nLeft1 = (int)(pRc->left + nColR1 * fInternalX);
		int nLeft2 = (int)(pRc->left + nColR2 * fInternalX);

		nLeft = (nLeft1 + nLeft2)/2;
	}
	//如果是最后的度
	else
	{
		nLeft = (int)(pRc->left + nCol * fInternalX);
	}

	//计算横向坐标值
	if (pnLeft != NULL)
	{
		*pnLeft = nLeft;
	}

	//计算纵向坐标值
	if (pnTop != NULL)
	{
		*pnTop  = nTop;
	}
}


//遍历筛选函数
CTreeNode* DisplayFc(CTree* pTree, CTreeNode* pTreeNode, void* pUser, int nDegree, int nCol)
{

	RECT* pRc =  (RECT *)pUser;

	CWindowDC  mDC(AfxGetApp()->m_pMainWnd);//->GetSafeHwnd());
	mDC.MoveTo(pRc->left, pRc->top);
	mDC.LineTo(pRc->right, pRc->top);
	mDC.LineTo(pRc->right, pRc->bottom);
	mDC.LineTo(pRc->left, pRc->bottom);
	mDC.LineTo(pRc->left, pRc->top);


	int nMaxDegree= pTree->GetMaxDegree();

	int nLeft1, nLeft2, nTop1, nTop2, nLeft3, nTop3;
	CBinaryTree::CalcCoordinate(pRc, nMaxDegree, 
		nDegree, nCol, &nLeft1, &nTop1);



	if (nDegree > 1)
	{
		if ((nCol % 2) == 0) 
		{
			CBinaryTree::CalcCoordinate(pRc, nMaxDegree, 
				nDegree, nCol + 1, &nLeft2, &nTop2);
		}
		else
		{
			CBinaryTree::CalcCoordinate(pRc, nMaxDegree, 
				nDegree, nCol - 1, &nLeft2, &nTop2);
		}
		
		CBinaryTree::CalcCoordinate(pRc, nMaxDegree, 
			nDegree - 1, nCol/2, &nLeft3, &nTop3);

		mDC.MoveTo(nLeft1, nTop1);
		mDC.LineTo(nLeft3, nTop3);
	}
	
	//画圆
	mDC.Ellipse(nLeft1-3, nTop1-3, nLeft1+3, nTop1+3);

	int nNodes = 1 << (nMaxDegree - 1);	
	float fInternalX = 0.0f;
	if (nMaxDegree > 1)
	{
		fInternalX = 1.0f * (pRc->right - pRc->left) / (nNodes - 1);
	}
	float fInternalY = 0.0f;
	if (nMaxDegree > 1)
	{
		fInternalY = 1.0f * (pRc->bottom - pRc->top) / (nMaxDegree - 1);
	}

	for (int i = 0; i < nNodes; i++)
	{
		for (int j = 0; j < nMaxDegree; j++)
		{
			int nLeft5 = (int)(pRc->left + i * fInternalX);
			int nTop5 = (int)(pRc->bottom - j * fInternalY);
			mDC.Ellipse(nLeft5-1, nTop5-1, nLeft5+1, nTop5+1);
		}

	}


	return(NULL);
}

char bysCh[100] = {
	'-', 
	'+',
	'/',
	'a',
	'*',
	'e',
	'f',
	'b',
	'-',
	'c',
	'd'};
void CDisplayDlg::OnOK() 
{
	// TODO: Add extra validation here

	CTreeNode msTreeNode[100];
	for (int i = 0; i < 100; i++)
	{
		memset(&msTreeNode[i], 0 ,sizeof(CTreeNode));
		msTreeNode[i].pUser = (void *)(DWORD)bysCh[i];
	}

	CBinaryTree mBinaryTree;
	mBinaryTree.InitiateTree();
	mBinaryTree.BuildBinaryTree(&msTreeNode[0], &msTreeNode[1], &msTreeNode[2]);

	mBinaryTree.InsertChildL(&msTreeNode[1], &msTreeNode[3]);
	mBinaryTree.InsertChildR(&msTreeNode[1], &msTreeNode[4]);

	mBinaryTree.InsertChildL(&msTreeNode[2], &msTreeNode[5]);
	mBinaryTree.InsertChildR(&msTreeNode[2], &msTreeNode[6]);

	mBinaryTree.InsertChildL(&msTreeNode[4], &msTreeNode[7]);
	mBinaryTree.InsertChildR(&msTreeNode[4], &msTreeNode[8]);

	mBinaryTree.InsertChildL(&msTreeNode[8], &msTreeNode[9]);
	mBinaryTree.InsertChildR(&msTreeNode[8], &msTreeNode[10]);

	//计算树的度
	mBinaryTree.CalcMaxDegree();

	//显示
	RECT rc;
	rc.left  = 200;
	rc.top = 200;
	rc.right = 500;
	rc.bottom = 500;
	mBinaryTree.TraverseBinaryTree(DisplayFc, &rc, TRAVERSING_BINARYTREE_LAST);



	//CDialog::OnOK();
}

#define COMM_BASE_COM1 0x3F8
#define COMM_BASE_COM2 0x2F8
#define COMM_BASE_COM3 0x3E8
#define COMM_BASE_COM4 0x2E8

struct WORDREGS 
{
	unsigned int ax, bx, cx, dx, si, di,cflag, flags;
}; 

struct BYTEREGS 
{
	unsigned char al, ah, bl, bh, cl, ch, dl, dh;
};

union REGS 
{
	struct WORDREGS x;
	struct BYTEREGS h;
};


int  int86(int intno, union REGS *inregs, union REGS *outregs);

BOOL InitCommEx_Rs232Bios(int nBase, WORD wBaud, int nDataBit, 
	int nStopBit, int nEvenOdd, WORD* pwState)
{
	//AH = 0 初始化端口
	//AH = 1 向串口写字符
	//AH = 2 从串口读字符
	//AH = 3 取通讯口状态
	//AH = 4 扩充的初始化状态
	//AH = 5 扩充的通信口控制
	
	//dx = 0 COMM1
	//dx = 1 COMM2
	//dx = 2 COMM3
	//dx = 3 COMM4

	int nComm = 0;
	if (nBase == COMM_BASE_COM1) nComm = 0;
	if (nBase == COMM_BASE_COM2) nComm = 1;
	if (nBase == COMM_BASE_COM3) nComm = 2;
	if (nBase == COMM_BASE_COM4) nComm = 3;

	REGS mRegsIn, mRegsOut;

	do
	{
		//奇偶性
		mRegsIn.h.bh = nEvenOdd;
		//停止位
		mRegsIn.h.bl = nStopBit & 0x1;
		//数据位
		mRegsIn.h.ch = (BYTE)(nDataBit - 5);
		//设置波特率
		mRegsIn.h.cl = (BYTE)(0.5 + log((BYTE)(wBaud / 150)) / log(2));
		
		//扩展初始化端口，中断14H功能--AH=4子功能调用
		mRegsIn.h.ah = 4; 
		mRegsIn.x.dx = (BYTE)nComm;
		::int86(0x14, &mRegsIn, &mRegsOut);
	}while(mRegsOut.h.ah >= (BYTE)0x80u);
	
	if (NULL != pwState)
	{
		*pwState = mRegsOut.x.ax;
	}

	return(TRUE);
}

BOOL ReadState_Rs232Bios(int nBase, WORD* pwState)
{
	//AH = 0 初始化端口
	//AH = 1 向串口写字符
	//AH = 2 从串口读字符
	//AH = 3 取通讯口状态
	//AH = 4 扩充的初始化状态
	//AH = 5 扩充的通信口控制
	
	//dx = 0 COMM1
	//dx = 1 COMM2
	//dx = 2 COMM3
	//dx = 3 COMM4

	int nComm = 0;
	if (nBase == COMM_BASE_COM1) nComm = 0;
	if (nBase == COMM_BASE_COM2) nComm = 1;
	if (nBase == COMM_BASE_COM3) nComm = 2;
	if (nBase == COMM_BASE_COM4) nComm = 3;

	REGS mRegsIn, mRegsOut;

	//取通讯口状态，中断14H功能--AH=3子功能调用
	mRegsIn.h.ah = 3; //取通讯口状态
	mRegsIn.x.dx = nComm;
	::int86(0x14, &mRegsIn, &mRegsOut);

	if (NULL != pwState)
	{
		*pwState = mRegsOut.x.ax;
	}

	return (TRUE);
}

BOOL WriteData_Rs232Bios(int nBase, BYTE * pBuffer, int nLength)
{
	//AH = 0 初始化端口
	//AH = 1 向串口写字符
	//AH = 2 从串口读字符
	//AH = 3 取通讯口状态
	//AH = 4 扩充的初始化状态
	//AH = 5 扩充的通信口控制
	
	//dx = 0 COMM1
	//dx = 1 COMM2
	//dx = 2 COMM3
	//dx = 3 COMM4

	int nComm = 0;
	if (nBase == COMM_BASE_COM1) nComm = 0;
	if (nBase == COMM_BASE_COM2) nComm = 1;
	if (nBase == COMM_BASE_COM3) nComm = 2;
	if (nBase == COMM_BASE_COM4) nComm = 3;

	REGS mRegsIn, mRegsOut;
	for (int i = 1; i < nLength; i++)
	{
		do
		{
			//向串口写字符，中断14H功能--AH = 1子功能调用
			mRegsIn.h.ah = 1;
			mRegsIn.h.al = *pBuffer;
			mRegsIn.x.dx = nComm;
			::int86(0x14, &mRegsIn, &mRegsOut);
		}while(mRegsOut.h.ah >= (BYTE)0x80u);
		
		pBuffer++;
	}
	
	return (TRUE);
}


BOOL ReadData_Rs232Bios(int  nBase, BYTE* pBuffer, int nLength)
{
	//AH = 0 初始化端口
	//AH = 1 向串口写字符
	//AH = 2 从串口读字符
	//AH = 3 取通讯口状态
	//AH = 4 扩充的初始化状态
	//AH = 5 扩充的通信口控制
	
	//dx = 0 COMM1
	//dx = 1 COMM2
	//dx = 2 COMM3
	//dx = 3 COMM4

	int nComm = 0;
	if (nBase == COMM_BASE_COM1) nComm = 0;
	if (nBase == COMM_BASE_COM2) nComm = 1;
	if (nBase == COMM_BASE_COM3) nComm = 2;
	if (nBase == COMM_BASE_COM4) nComm = 3;

	REGS mRegsIn, mRegsOut;

	for (int i = 0; i < nLength; i++)
	{
		do
		{
			//从串口读字符，中断14H功能--AH = 2子功能调用
			mRegsIn.h.ah = 2; 
			mRegsIn.x.dx = nComm;
			::int86(0x14, &mRegsIn, &mRegsOut);
		}while(mRegsOut.h.ah >= (BYTE)0x80u);
	}

	return(TRUE);
}

#define  COMM_BUFFER_SIZE 1024




#define  interrupt
#define  inportb(a) _inp(a)
#define  outportb(a, b) _outp(a, b)
void setvect (int interruptno, void interrupt (*isr) ());


char bufAfxComm1[COMM_BUFFER_SIZE];
int  nAfxCommPoint1 = 0;

void interrupt _Comm1Int()
{
	int nState;
	do
	{
		nState  = inportb(COMM_BASE_COM1 + 5);
		if (nState &1)
		{
			bufAfxComm1[nAfxCommPoint1++] = inportb(COMM_BASE_COM1);
			if (nAfxCommPoint1 >= COMM_BUFFER_SIZE)
			{
				nAfxCommPoint1 = 0;
			}
		}
	}while (nState &1);
	outportb(0x20, 0x20);
}


void InitComm_Rs232Interrupt(int nBase)
{
	/* PORT 1 - Communication Settings */
	outportb(nBase + 3, 0x80); /* SET DLAB ON */
	outportb(nBase + 0, 0x0C); /* Set Baud rate - Divisor Latch Low Byte */
	/* Default 0x03 = 38,400 BPS */
	/* 0x01 = 115,200 BPS */
	/* 0x02 = 57,600 BPS */
	/* 0x06 = 19,200 BPS */
	/* 0x0C = 9,600 BPS */
	/* 0x18 = 4,800 BPS */
	/* 0x30 = 2,400 BPS */

	outportb(nBase + 1, 0x00); /* Set Baud rate - Divisor Latch High Byte */
	outportb(nBase + 3, 0x03); /* 8 Bits, No Parity, 1 Stop Bit */
	outportb(nBase + 2, 0xC7); /* FIFO Control Register */
	outportb(nBase + 4, 0x0B); /* Turn on DTR, RTS, and OUT2 */

	outportb(0x21, (inportb(0x21) &0xEF)); /* Set Programmable Interrupt Controller */
	/* COM1 (IRQ4) - 0xEF */
	/* COM2 (IRQ3) - 0xF7 */
	/* COM3 (IRQ4) - 0xEF */
	/* COM4 (IRQ3) - 0xF7 */

	outportb(nBase + 1, 0x01); /* Interrupt when data received */
}

void CloseComm_Re232Interrupt(int nBase)
{
	outportb(nBase + 1, 0);
	
	// 关闭COM口的IRQ
	if (nBase == COMM_BASE_COM1 || nBase == COMM_BASE_COM3)
	{
		outportb(0x21, (inportb(0x21) | 0x10)); /* MASK IRQ using PIC */
	}
	else
	{
		outportb(0x21, (inportb(0x21) | 0x10)); /* MASK IRQ using PIC */
	}
}

//CProgress