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

📄 dlgt2dlg.cpp

📁 精确计算各种排序算法时间效率的程序
💻 CPP
字号:
// DlgT2Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "DlgT2.h"
#include "DlgT2Dlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDlgT2Dlg dialog

CDlgT2Dlg::CDlgT2Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgT2Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgT2Dlg)
		// 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);
	// length = 1000;
}

void CDlgT2Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgT2Dlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDlgT2Dlg, CDialog)
	//{{AFX_MSG_MAP(CDlgT2Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgT2Dlg message handlers

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

void CDlgT2Dlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	//*
	CStdioFile cfgFile;
	CString strTemp;

	
	if ( !cfgFile.Open("F:\\work\\新项目_0801\\sample\\wgl_32\\dat\\新建 文本文档.ex8", CFile::modeRead | CFile::typeText) )
	{
		AfxMessageBox(_T("Can NOT Open Test File !!!"), MB_OK | MB_ICONEXCLAMATION);
		return;
	}
	
	if ( !cfgFile.ReadString(strTemp) )
	{
		AfxMessageBox(_T("ReadString Error !!!"), MB_OK | MB_ICONEXCLAMATION);
		cfgFile.Close();
		
		return;
	}
	else
	{
		AfxMessageBox(strTemp);
	}
}

void CDlgT2Dlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	unsigned int icode = 1;
	unsigned char ccode = 1;
	unsigned short scode = 1;
	unsigned long pcode = 1, tcode;
	// float fcode;
	double dcode;

	tcode = pcode << 8;

	int iLen = sizeof(dcode);

	CString str;
	str.Format("%d", iLen);

	MessageBox(str);
}

void CDlgT2Dlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	int length = 100000;
	int bSearchTable[100000];
	int sSearchTable[100000];
	int i;

	LARGE_INTEGER	litmp;   
	LONGLONG		QPart1,QPart2;   
	double			dfMinus, dfFreq, dfTim;

	CString str;

	srand(time(0));
	
	for (i = 0;i < length;i++)
	{
		bSearchTable[i] = int(rand() % 100000);
		sSearchTable[i] = bSearchTable[i];
	}
    
	QueryPerformanceFrequency(&litmp);
	//   获得计数器的时钟频率
	dfFreq   =   (double)litmp.QuadPart;
    
	QueryPerformanceCounter(&litmp); 
	//   获得初始值   
	QPart1   =   litmp.QuadPart;   

	// InsertSort(bSearchTable, length);
	ShellSort(bSearchTable, (int)(length/2), length);
    
	QueryPerformanceCounter(&litmp);
	//   获得中止值   
	QPart2 = litmp.QuadPart;   
    
	dfMinus = (double)(QPart2 - QPart1);
	dfTim = dfMinus / dfFreq;

	str.Format("%f", dfTim);
	
	MessageBox(str);

	////////////////////////////////////////////////////////////////

	QueryPerformanceCounter(&litmp); 
	//   获得初始值   
	QPart1   =   litmp.QuadPart;   
	
	// InsertSort(sSearchTable, length);
	QuickSort(sSearchTable, 0, length-1, length);
    
	QueryPerformanceCounter(&litmp);
	//   获得中止值   
	QPart2 = litmp.QuadPart;   
    
	dfMinus = (double)(QPart2 - QPart1);
	dfTim = dfMinus / dfFreq;       

	str.Format("%f", dfTim);
	
	MessageBox(str);
}

void CDlgT2Dlg::BubbleSort(int searchTable[], const int length)
{
	int i, j;
	int temp;
	bool exchange;
	
	for (i = 1;i < length;i++)
	{
		exchange = false;
		
		for (j = length - 1;j >= i;j--)
			if (searchTable[j] < searchTable[j - 1])
			{
				temp = searchTable[j];
				searchTable[j] = searchTable[j - 1];
				searchTable[j - 1] = temp;
				
				exchange = true;
			}
			
		if (exchange == false)
			return ;
	}
}

void CDlgT2Dlg::SelectSort(int searchTable[], const int length)
{
	int i, j, k;
	int temp;
	
	for (i = 0;i < length;i++)
	{
		k = i;
		
		for (j = i + 1;j < length;j++)
			if (searchTable[j] < searchTable[k])
				k = j;

		if (k != i)
		{
			temp = searchTable[i];
			searchTable[i] = searchTable[k];
			searchTable[k] = temp;
		}
	}
}

void CDlgT2Dlg::InsertSort(int searchTable[], const int length)
{
	int i, j;
	int temp;

	for ( i = 1; i < length; i++ )
	{
		temp = searchTable[i];

		for ( j = i - 1; temp < searchTable[j] && j >= 0; j-- )
			searchTable[j + 1] = searchTable[j];

		if ( j != i - 1 )
			searchTable[j + 1] = temp;
	}
}

void CDlgT2Dlg::BinInsertSort(int searchTable[], const int length)
{
	int i, j, left, mid, right;
	int temp;
	
	for ( i = 1; i < length; i++ )
	{
		temp = searchTable[i];
		left = 0;
		right = i - 1;

		while (left <= right)
		{
			mid = (left + right) / 2;

			if (temp < searchTable[mid])
				right = mid - 1;
			else
				left = mid + 1;
		}

		for (j = i - 1; j >= left; j--)
			searchTable[j + 1] = searchTable[j];

		if (left != i)
			searchTable[left] = temp;
	}
}

void CDlgT2Dlg::ShellSort(int searchTable[], int d, const int length)
{
	int i, j, inc;
	int temp;
	
	for (inc = d; inc > 0; inc /= 2)
	{
		for (i = inc; i < length; i++)
		{
			temp = searchTable[i];

			for (j = i - inc; j >= 0 && temp < searchTable[j]; j -= inc)
				searchTable[j + inc] = searchTable[j];

			searchTable[j + inc] = temp;
		}
	}
}

void CDlgT2Dlg::QuickSort(int searchTable[], int l, int r, const int length)
{
	int i, j;
	int temp;
	
	if (l >= r)
		return ;
	
	i = l;
	j = r;
	temp = searchTable[i];
	
	while (i != j)
	{
		while ( searchTable[j] >= temp && j > i )
			j--;
		
		if (i < j)
			searchTable[i++] = searchTable[j];
		
		while ( searchTable[i] <= temp && j > i )
			i++;
		
		if (i < j)
			searchTable[j--] = searchTable[i];
	}
	
	searchTable[i] = temp;
	
	QuickSort(searchTable, l, i - 1, length);
	QuickSort(searchTable, i + 1, r, length);
}



void CDlgT2Dlg::OnButton4() 
{
	// TODO: Add your control notification handler code here
	LARGE_INTEGER	litmp;   
	LONGLONG		QPart1,QPart2;   
	double			dfMinus, dfFreq, dfTim;

	// int i;
	float f;

	CString str;

	QueryPerformanceFrequency(&litmp);
	//   获得计数器的时钟频率
	dfFreq   =   (double)litmp.QuadPart;
    
	QueryPerformanceCounter(&litmp); 
	//   获得初始值   
	QPart1   =   litmp.QuadPart;

	for (int j = 0; j < 1000000; j++)
	{
		f = sqrtf((float)j);
	}

	QueryPerformanceCounter(&litmp);
	//   获得中止值   
	QPart2 = litmp.QuadPart;   
    
	dfMinus = (double)(QPart2 - QPart1);
	dfTim = dfMinus / dfFreq;
	
	str.Format("%f", dfTim);
	
	MessageBox(str);
}

⌨️ 快捷键说明

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