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

📄 mergesortshowdlg.cpp

📁 数据结构的代码演示程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MergeSortShowDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MergeSortShow.h"
#include "MergeSortShowDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMergeSortShowDlg dialog

CMergeSortShowDlg::CMergeSortShowDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMergeSortShowDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMergeSortShowDlg)
	m_MultiNum = 0;
	m_ValueShow1 = _T("");
	m_ValueShow2 = _T("");
	m_ValueShow3 = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMergeSortShowDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMergeSortShowDlg)
	DDX_Text(pDX, IDC_MULTI_NUM, m_MultiNum);
	DDX_Text(pDX, IDC_VALUE_SHOW1, m_ValueShow1);
	DDX_Text(pDX, IDC_VALUE_SHOW2, m_ValueShow2);
	DDX_Text(pDX, IDC_VALUE_SHOW3, m_ValueShow3);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMergeSortShowDlg, CDialog)
	//{{AFX_MSG_MAP(CMergeSortShowDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_START_SHOW, OnStartShow)
	ON_BN_CLICKED(IDC_SINGLE_STEP, OnSingleStep)
	ON_BN_CLICKED(IDC_MULTI_STEP, OnMultiStep)
	ON_BN_CLICKED(IDC_END_SHOW, OnEndShow)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMergeSortShowDlg message handlers

CMergeSortShowDlg::~CMergeSortShowDlg()
{
	delete m_pCodeDisplayWnd;
	delete m_pStackDisplayWnd;
	delete m_pMoveDisplayWnd;
	if(m_pShowThread)
	{
		delete m_pShowThread;
		m_pShowThread = NULL;
	}
}

CDialog * CMergeSortShowDlg::GetDlg()
{
	CMergeSortShowApp *pApp = (CMergeSortShowApp *)AfxGetApp();
	CDialog *pDialog = (CDialog *)pApp->m_pMainWnd;
	return pDialog;
}

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

	// 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
	RECT rect;
	m_pCodeDisplayWnd= new CTextWnd(IDC_CODE_SHOW);
	m_pCodeDisplayWnd->Create(NULL,NULL,WS_CHILD|WS_VISIBLE,rect,this,0,NULL);
	m_pStackDisplayWnd= new CTextWnd(IDC_STACK_SHOW);
	m_pStackDisplayWnd->Create(NULL,NULL,WS_CHILD|WS_VISIBLE,rect,this,0,NULL);
	m_pMoveDisplayWnd= new CMoveWnd(IDC_MOVE_SHOW);
	m_pMoveDisplayWnd->Create(NULL,NULL,WS_CHILD|WS_VISIBLE,rect,this,0,NULL);
	
	SetCodeSentence();
	m_pStackDisplayWnd->SetTextWnd();
	m_count = 8;
	m_A[0] = 36;
	m_A[1] = 20;
	m_A[2] = 17;
	m_A[3] = 13;
	m_A[4] = 28;
	m_A[5] = 14;
	m_A[6] = 23;
	m_A[7] = 15;
	memset(m_temp, 0, sizeof(int)*m_count);
	m_pMoveDisplayWnd->SetMoveWnd(m_A, m_temp, m_count);
	m_Stack_Top = -1;
	m_step = -1;
	mExist = FALSE;
	m_pShowThread = NULL;
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CMergeSortShowDlg::SetCode(int pos)////////////////////////////////////////
{
	for(int i=0; i<m_pCodeDisplayWnd->m_Sentence_num; i++)
		if(m_pCodeDisplayWnd->m_Sentence_Flag[i] == 'L')
			m_pCodeDisplayWnd->m_Sentence_Flag[i] ='A';
	m_pCodeDisplayWnd->m_Sentence_Flag[pos] = 'L';
	m_pCodeDisplayWnd->Invalidate();

	m_step--;
}

void CMergeSortShowDlg::PushStack(CString CallStr)
{
	m_Stack_Top++;
	m_pStackDisplayWnd->m_Sentence_num++;
	m_pStackDisplayWnd->m_Sentence[m_pStackDisplayWnd->m_Sentence_num-1].Empty();
	m_pStackDisplayWnd->m_Sentence[m_pStackDisplayWnd->m_Sentence_num-1] += CallStr;
	m_pStackDisplayWnd->m_Sentence_Flag[m_pStackDisplayWnd->m_Sentence_num-1] = 'L';
	if(m_pStackDisplayWnd->m_Sentence_num>1)
		m_pStackDisplayWnd->m_Sentence_Flag[m_pStackDisplayWnd->m_Sentence_num-2] = 'A';
	m_pStackDisplayWnd->Invalidate();
}

void CMergeSortShowDlg::PopStack()
{
	m_Stack_Top--;
	m_pStackDisplayWnd->m_Sentence_num--;
	m_pStackDisplayWnd->m_Sentence[m_pStackDisplayWnd->m_Sentence_num].Empty();
	if(m_pStackDisplayWnd->m_Sentence_num>0)
		m_pStackDisplayWnd->m_Sentence_Flag[m_pStackDisplayWnd->m_Sentence_num-1] = 'L';
	m_pStackDisplayWnd->Invalidate();
}

void CMergeSortShowDlg::ResetCodeSentence()
{
	m_pCodeDisplayWnd->m_Sentence_Flag[0] = 'L';
	memcpy(m_pCodeDisplayWnd->m_Pos_Flag, " ● ", 4);
	for(int i=1; i<m_pCodeDisplayWnd->m_Sentence_num; i++)
		if(m_pCodeDisplayWnd->m_Sentence_Flag[i] == 'L')
			m_pCodeDisplayWnd->m_Sentence_Flag[i] = 'A';
	m_ValueShow1.Empty();
	m_ValueShow2.Empty();
	m_ValueShow3.Empty();
	m_pCodeDisplayWnd->Invalidate();
}
void CMergeSortShowDlg::ResetStackSentence()
{
	memcpy(m_pStackDisplayWnd->m_Pos_Flag, "===>", 4);
	m_pStackDisplayWnd->m_ResetWnd = TRUE;
	m_Stack_Top = -1;
	for(int i=0; i<m_pStackDisplayWnd->m_Sentence_num; i++)
		m_pStackDisplayWnd->m_Sentence[i].Empty();
	m_pStackDisplayWnd->m_Sentence_num = 0;
	m_pCodeDisplayWnd->Invalidate();
}


BOOL CMergeSortShowDlg::SetValue(int n, int s)
{
	CString str;
	if(m_step < 0)
		return TRUE;

	m_ValueShow1.Empty();
	m_ValueShow1.Format("n=%d", n);
	if(s!=0)
	{
		m_ValueShow2.Empty();
		m_ValueShow2.Format("fact(n-1)=%d", s);
		m_ValueShow3.Empty();
		m_ValueShow3.Format("n*fact(n-1)=%d   !!!", s*n);
	}
	if(m_step == 0)
	{
		WaitForSingleObject(mStepEvent, INFINITE);
		mStepEvent.ResetEvent();
	}
	return FALSE;
}
void CMergeSortShowDlg::SetMoveValue(BOOL MoveFlag, BOOL FromA_Flag, BOOL ToA_Flag, int from, int to, int A[], int temp[])
{
	int i;
	m_pMoveDisplayWnd->m_Move_Flag = MoveFlag;
	m_pMoveDisplayWnd->m_FromA_Flag = FromA_Flag;
	m_pMoveDisplayWnd->m_ToA_Flag = ToA_Flag;
	m_pMoveDisplayWnd->m_from = from;
	m_pMoveDisplayWnd->m_to = to;
	for(i = 0; i<m_pMoveDisplayWnd->m_Array_Num; i++)
	{
		m_pMoveDisplayWnd->m_pA[i] = A[i];
		m_pMoveDisplayWnd->m_ptemp[i] = temp[i];
	}
	m_pMoveDisplayWnd->Invalidate();
}

void CMergeSortShowDlg::SetCodeSentence()
{
	m_pCodeDisplayWnd->m_Sentence[0]  = "public static int fact(int n)                        ";
	m_pCodeDisplayWnd->m_Sentence[1]  = "{                                                    ";
	m_pCodeDisplayWnd->m_Sentence[2]  = "//...................................................";
	m_pCodeDisplayWnd->m_Sentence[3]  = "// Computes the factorial of a nonnegative integer.  ";
    m_pCodeDisplayWnd->m_Sentence[4]  = "// Precondition: n must be greater than or equal to O";
	m_pCodeDisplayWnd->m_Sentence[5]  = "// Postcondition: Returns the factorial of n.        ";
	m_pCodeDisplayWnd->m_Sentence[6]  = "//...................................................";
	m_pCodeDisplayWnd->m_Sentence[7]  = "      if (n == 0)                                    ";
	m_pCodeDisplayWnd->m_Sentence[8]  = "      {                                              ";
	m_pCodeDisplayWnd->m_Sentence[9]  = "            return 1;                                ";
	m_pCodeDisplayWnd->m_Sentence[10] = "      }                                              ";
	m_pCodeDisplayWnd->m_Sentence[11] = "      else                                           ";
	m_pCodeDisplayWnd->m_Sentence[12] = "      {                                              ";
	m_pCodeDisplayWnd->m_Sentence[13] = "           return n * fact (n-1);                    ";
	m_pCodeDisplayWnd->m_Sentence[14] = "      }// end if                                     ";
	m_pCodeDisplayWnd->m_Sentence[15] = "}// end fact                                         ";

	m_pCodeDisplayWnd->m_Sentence_num = 16;
	m_pCodeDisplayWnd->SetTextWnd();
	m_ValueShow1.Empty();
	m_ValueShow2.Empty();
	m_ValueShow3.Empty();
}


void CMergeSortShowDlg::inssort(int A[], int n)

⌨️ 快捷键说明

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