www.pudn.com > CmpTool.zip > CmpToolDlg.cpp, change:2016-09-08,size:7188b


// CmpToolDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "CmpTool.h" 
#include "CmpToolDlg.h" 
#include <time.h> 
#include <algorithm> 
using std::sort; 
 
#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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CCmpToolDlg dialog 
 
CCmpToolDlg::CCmpToolDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CCmpToolDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CCmpToolDlg) 
	m_dBigPercent = 0.05; 
	m_nBigValue = 0; 
	m_nFirstValue = 0; 
	m_nNumb = 10; 
	m_dRandPersent = 0.15; 
	m_strResult = _T(""); 
	m_nSmallValue = 30; 
	m_nBs = 10; 
	m_nTotal = 15000; 
	m_bCheck = FALSE; 
	m_nSortType = 0; 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CCmpToolDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CCmpToolDlg) 
	DDX_Text(pDX, IDC_EDIT_BIGEST, m_dBigPercent); 
	DDX_Text(pDX, IDC_EDIT_BVALUE, m_nBigValue); 
	DDX_Text(pDX, IDC_EDIT_FRESULT, m_nFirstValue); 
	DDX_Text(pDX, IDC_EDIT_NUMB, m_nNumb); 
	DDX_Text(pDX, IDC_EDIT_RANDPER, m_dRandPersent); 
	DDX_Text(pDX, IDC_EDIT_RESULT, m_strResult); 
	DDX_Text(pDX, IDC_EDIT_SMALLST, m_nSmallValue); 
	DDX_Text(pDX, IDC_EDIT_TMS, m_nBs); 
	DDX_Text(pDX, IDC_EDIT_TOTAL, m_nTotal); 
	DDX_Check(pDX, IDC_CHECK1, m_bCheck); 
	DDX_Radio(pDX, IDC_RADIO1, m_nSortType); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CCmpToolDlg, CDialog) 
	//{{AFX_MSG_MAP(CCmpToolDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CCmpToolDlg message handlers 
 
BOOL CCmpToolDlg::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 
	srand(time(NULL)); 
 
// 	SYSTEMTIME st; 
// 	GetLocalTime(&st); 
// 	CString strDate; 
// 	strDate.Format("%4d%02d%02d",st.wYear,st.wMonth,st.wDay);  
// 	//MessageBox("此版本为测试版,有以下限制:\r\n1.样本1中的计算最多20行\r\n2.使用次数有限制\r\n正式版无上述限制", "注意", MB_ICONWARNING | MB_OK); 
// 	if (strDate != "20160908") 
// 	{ 
// 		exit(0); 
//  	} 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CCmpToolDlg::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 CCmpToolDlg::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 CCmpToolDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
bool cmp(int i,int j)   
{   
    return (i>j);   
}   
 
void CCmpToolDlg::OnButtonStart()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(TRUE); 
	double temp = (double)m_nTotal * m_dRandPersent; 
	m_nFirstValue = temp; 
	int tempN = m_nFirstValue % 10; 
	if (tempN >= 5) 
	{ 
		m_nFirstValue -= tempN; 
		m_nFirstValue += 10; 
	} 
	else 
		m_nFirstValue -= tempN; 
 
	m_nBigValue = (double)m_nTotal * m_dBigPercent; 
 
	int nTemp = m_nFirstValue; 
	int bV = m_nBigValue; 
	int gamp = 10; 
	int* sz = new int[m_nNumb]; 
	while (TRUE) 
	{ 
		bV -= gamp; 
		if (bV <= 0) 
		{ 
			gamp += 10; 
			bV = m_nBigValue; 
			continue; 
		} 
		m_strResult = ""; 
		int nTotal = 0; 
		int leftN = m_nFirstValue; 
		BOOL bFalse = TRUE; 
 
		for (int i = 0; (i < m_nNumb - 1) && (nTotal <= (m_nFirstValue - m_nSmallValue)); i++) 
		{ 
			int tempLeft = leftN - m_nSmallValue; 
			if (tempLeft <= 0) 
			{ 
				bFalse = FALSE; 
				break; 
			} 
			int randN = tempLeft > bV ? bV : tempLeft; 
			int n = rand() % randN + m_nSmallValue; 
			if (m_bCheck == TRUE) 
			{ 
				int a = n % m_nBs; 
				n -= a; 
			} 
			sz[i] = n; 
			nTotal += n; 
			leftN -= n; 
// 			CString tempStr; 
// 			tempStr.Format("%-6d", n); 
// 			m_strResult += tempStr; 
// 			if ((i + 1) % 10 == 0) 
// 			{ 
// 				m_strResult += "\r\n"; 
// 			} 
			 
		} 
		if (nTotal <= m_nFirstValue - m_nSmallValue && bFalse == TRUE) 
		{ 
			int N = m_nFirstValue - nTotal; 
			sz[m_nNumb - 1] = N; 
			if (N > m_nBigValue) 
				continue; 
// 			CString tempStr; 
// 			tempStr.Format("%d", N); 
// 			m_strResult += tempStr; 
			break; 
		} 
	} 
	if (m_nSortType == 1) 
	{ 
		sort(sz, sz + m_nNumb, cmp); 
	} 
	else if (m_nSortType == 2) 
	{ 
		sort(sz, sz + m_nNumb); 
	} 
 
	for (int i = 0; i < m_nNumb; i++) 
	{ 
		CString tempStr; 
		tempStr.Format("%-6d", sz[i]); 
		m_strResult += tempStr; 
		if ((i + 1) % 10 == 0) 
		{ 
			m_strResult += "\r\n"; 
		} 
	} 
 
	delete []sz; 
	UpdateData(FALSE); 
}