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

📄 demodlg.cpp

📁 这是一个排序算法的应用
💻 CPP
字号:
// DEMODlg.cpp : implementation file
//

#include "stdafx.h"
#include "DEMO.h"
#include "DEMODlg.h"
#include "stdio.h"
#include "qhncxf_paixu.h"
#define  length  100000
#define L 100

#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()

/////////////////////////////////////////////////////////////////////////////
// CDEMODlg dialog

CDEMODlg::CDEMODlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDEMODlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDEMODlg)
	m_1 = _T("");
	m_2 = _T("");
	m_3 = 0;
	m_4 = 0;
	m_5 = _T("");
	m_6 = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON2);
}

void CDEMODlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDEMODlg)
	DDX_Text(pDX, IDC_EDIT1, m_1);
	DDX_Text(pDX, IDC_jieguo, m_2);
	DDX_Text(pDX, IDC_yidong, m_3);
	DDX_Text(pDX, IDC_bijiao, m_4);
	DDX_Text(pDX, IDC_zhengti_0, m_5);
	DDX_Text(pDX, IDC_EDIT2, m_6);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDEMODlg, CDialog)
	//{{AFX_MSG_MAP(CDEMODlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_Exit, OnExit)
	ON_BN_CLICKED(IDC_Enter, OnEnter)
	ON_BN_CLICKED(IDC_re_input, Onreinput)
	ON_BN_CLICKED(IDC_Suiji, OnSuiji)
	ON_BN_CLICKED(IDC_zhengti_1, Onzhengti1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDEMODlg message handlers

BOOL CDEMODlg::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
	
   	CheckRadioButton(IDC_0,IDC_7,IDC_0);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDEMODlg::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 CDEMODlg::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 CDEMODlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
void CDEMODlg::OnExit() 
{
	// TODO: Add your control notification handler code here
	//退出
     MessageBox("感谢您对本软件的使用!谢谢","温馨提示",0);
	   exit(0);

	
}
int CDEMODlg::treamform(char *s1, int *s2)
{
/*将字符数组S1[]的字符转换成整数存到整形数组S2[]中*/
 	int i=0,j=0,d;
    char ch;
 	ch=s1[i]; //ch保存s1串中扫描到的字符,初值为第一个字符
 	while(ch!='#')
	{
        switch (ch)
		{
		case ' ':
			 i++;
			 ch=s1[i];
			 break;	
       default:
			 d=0;
			 while(ch>='0'&&ch<='9')
			 {
			    d=10*d+ch-'0';
                 i++;
			    ch=s1[i];
			 }
		     s2[j]=d;
             j++;	
             break;
		}
	}
  return j;
}
char s1[length];//存放用户输入的排序码(整型数据和空格)
int s2[length]; //将s1中的整型数据转换到S2中
void CDEMODlg::OnEnter() 
{
	// TODO: Add your control notification handler code here
        //开始排序
	UpdateData(TRUE);
	int i,j=0,h=0,k,c=0,m=0;
     SortObject st;   
	   if(kaishi())
	   {
	     st.n=treamform(s1,s2);
	        for(i=0;i<st.n;i++)
  	          st.record[i].key=s2[i];
		if(st.n==1)
			MessageBox("排序码只有一个整数","注意",0);
		else
		{
			h=GetCheckedRadioButton(IDC_0,IDC_7);
			if(h==1000)
			   MessageBox("现是默认第一个排序算法!","说明",0);  
            switch(h)
			{ //MessageBox("请选择排序算法!","选择",0);
	         case 1000:// Shell插入排序
                 shellSort(st,c,m);break;
	         case 1001://冒泡排序
		         bubbleSort( st,c,m );break;
             case 1002://快速排序
		         quickSort(st,0 ,st.n-1,c,m);break;
	         case 1003://堆排序
		          heapSort( st,c,m);break;
	         case 1004://二分法插入排序
                  binSort ( st,c,m);break;
	         case 1005://直接插入排序
                  insertSort(st,c,m);break;
             case 1006:// 直接选择排序
				 SelectSort(st,c,m);break;
	         case 1007:
		     break;
	         default:break;
		    }
		  if(h>=1000&&h<=1007)
		  {
				m_3=m;m_4=c;
                m_2="";
	            int i, s;
              	char temp[length];
            	for(i=0;i<st.n;i++)
				{  
	               s=st.record[i].key;
		            itoa(s,temp,10);
		             m_2=m_2+temp+" ";
				}
		  }
		}
	   }
	  UpdateData(FALSE);
}
void CDEMODlg::Onreinput() 
{
	// TODO: Add your control notification handler code here
	//重新输入排序码
   UpdateData(TRUE);
   m_1=m_2=m_5="";
   m_3=m_4=m_6=0;
   ON_IDC_EDIT1:SetFocus();
   UpdateData(FALSE);
}
void CDEMODlg::OnSuiji() 
{
	// TODO: Add your control notification handler code here
	//随机输入
	UpdateData(TRUE);
	int s,i=m_1.GetLength();
		if(m_6==0)
		{ ON_IDC_EDIT6:SetFocus();
			MessageBox("请输入随机输入的元素个数!","注意",0);}
	time_t t;
    srand((unsigned)time(&t));
	char temp[length];
	m_1="";
   //m_6 random numbers from 0 to 1000\n\n");
   	for(i=0;i<m_6;i++)
	{
		s=rand()%1000;
	    itoa(s,temp,10);
		m_1=m_1+temp+" ";
	}
	UpdateData(FALSE);
}

int CDEMODlg::kaishi()
{
	//令字符串的数字和空格转移到数组S1中保存
	UpdateData(TRUE);
	int i,j=0,k;
	char ch;
	k=m_1.GetLength();
	if(k<=0)
	{MessageBox("请输入排序码!","注意现排序码为空",0);ON_IDC_EDIT1:SetFocus();return false;}
	else
	{
		for(i=0;i<k;i++)
		{
			ch=m_1.GetAt(i);
			if(ch=='.')
			{MessageBox("您输入的数据不符合,请输入整型数据","注意",0);return false;}
			if(ch==' '||(ch>='0'&&ch<='9'))
			{s1[j]=ch;j++;}
		}
        s1[j]='#';
		return true;
	}
		UpdateData(FALSE);
}
void CDEMODlg::Onzhengti1() 
{
	// TODO: Add your control notification handler code here
	//整体比较
	 UpdateData(TRUE);
	 SortObject st; 
	 int i,j=1;
	 if(kaishi())
	 {
		 st.n=treamform(s1,s2);
	     if(st.n==1)
			MessageBox("排序码只有一个整数","注意",0);
	     else
		 {	 
			m_5=m_2="";
			while(j<8)
			{
			  for(i=0;i<st.n;i++)
  	           st.record[i].key=s2[i];
			  int c=0,m=0;
			  switch(j)
			  {
			   case 1:
			     shellSort(st,c,m); 
				 m_5=m_5+"希尔排序        ";
				int i, s;
              	char temp[length];
            	for(i=0;i<st.n;i++)
				{  
	               s=st.record[i].key;
		            itoa(s,temp,10);
		             m_2=m_2+temp+" ";
				}
				 break;
			   case 2:
		         bubbleSort( st ,c,m);
				 m_5=m_5+"冒泡排序        ";
				 break;
			   case 3:
                 quickSort(st,0 ,st.n-1,c,m);
				 m_5=m_5+"快速排序        ";
				 break;
			   case 4:
		         heapSort( st,c,m);
				 m_5=m_5+"堆排序          ";
				 break;
			   case 5:
                  binSort ( st,c,m);
				  m_5=m_5+"二分法插入排序  ";break;
			   case 6:
                  insertSort(st,c,m);
				  m_5=m_5+"直接插入排序    ";break;
			   case 7:
				   SelectSort(st,c,m);
                   m_5=m_5+"直接选择排序    ";break;
				   break;
			   default:
				   break;
			  }
			  j++;
         	 char temp_1[L]; 
   		       itoa(m,temp_1,10);
		        m_5=m_5+temp_1+"                ";
			    itoa(c,temp_1,10);
		        m_5=m_5+temp_1+"       ";
			
			}
		 }
	 }
	UpdateData(FALSE);
}

⌨️ 快捷键说明

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