?? multisortdlg.cpp
字號:
// MultiSortDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MultiSort.h"
#include "MultiSortDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CString file;
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CMultiSortDlg dialog
CMultiSortDlg::CMultiSortDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMultiSortDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMultiSortDlg)
m_combo = -1;
m_filepath = _T("");
m_maintext = _T("");
m_num = 0;
m_keynum = 0;
m_a = 1;
m_b = 2;
m_c = 3;
m_d = 4;
m_e = 5;
m_radio1 = 0;
m_minortext = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMultiSortDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMultiSortDlg)
DDX_Text(pDX, IDC_EDIT11, m_filepath);
DDX_Text(pDX, IDC_EDIT1, m_maintext);
DDX_Text(pDX, IDC_number, m_num);
DDX_Text(pDX, IDC_EDIT5, m_keynum);
DDX_Text(pDX, IDC_a, m_a);
DDX_Text(pDX, IDC_b, m_b);
DDX_Text(pDX, IDC_c, m_c);
DDX_Text(pDX, IDC_d, m_d);
DDX_Text(pDX, IDC_e, m_e);
DDX_Radio(pDX, IDC_RADIO1, m_radio1);
DDX_Text(pDX, IDC_EDIT2, m_minortext);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMultiSortDlg, CDialog)
//{{AFX_MSG_MAP(CMultiSortDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_FILE, OnFile)
ON_BN_CLICKED(IDC_SORT, OnSort)
ON_BN_CLICKED(IDC_RANDOM, OnRandom)
ON_BN_CLICKED(IDC_BUTTON1, OnExplain)
ON_BN_CLICKED(IDC_Handin, OnHandin)
ON_BN_CLICKED(IDC_SAVE, OnSave)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMultiSortDlg message handlers
BOOL CMultiSortDlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
void CMultiSortDlg::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 CMultiSortDlg::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 CMultiSortDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMultiSortDlg::OnSelchangeCombo1()
{
// TODO: Add your control notification handler code here
}
void CMultiSortDlg::OnFile() //“打開文件”
{
static char BASED_CODE szFilter[]="Txt Files (*.txt)|*.txt||";
CFileDialog dlg(true,NULL,NULL,OFN_FILEMUSTEXIST|OFN_HIDEREADONLY,szFilter,NULL);
if(dlg.DoModal()!=IDOK)return;
m_filepath=dlg.GetFileName();
UpdateData(false);
CFile f(m_filepath,CFile::modeRead);
int nLen=f.GetLength();
file.Empty();
f.ReadHuge(file.GetBuffer(nLen+1),nLen) ;
file.ReleaseBuffer();
m_maintext=file; //將文件內容在編輯框顯示,便于修改查看結果
UpdateData(false);
}
void CMultiSortDlg::OnSort() //“開始排序”
{
UpdateData(true);
CString a,b,c;
CString con;
int lsda=0;
int lsdb=0;
int msd=0;
int head=0;
int time=0;
Sort s;
s.Init();
s.keynum=m_keynum;
s.size=m_num;
for(int i=0;i<5;i++)s.priority[i]=0;
if(m_keynum>0)
{
if(m_a>=1&&m_a<=m_keynum&&!s.priority[m_a-1])s.priority[m_a-1]=1;
else { MessageBox("優先次序輸入有誤!"); return; }
}
if(m_keynum>1)
{
if(m_b>=1&&m_b<=m_keynum&&!s.priority[m_b-1])s.priority[m_b-1]=2;
else { MessageBox("優先次序輸入有誤!"); return; }
}
if(m_keynum>2)
{
if(m_c>=1&&m_c<=m_keynum&&!s.priority[m_c-1])s.priority[m_c-1]=3;
else { MessageBox("優先次序輸入有誤!"); return; }
}
if(m_keynum>3)
{
if(m_d>=1&&m_d<=m_keynum&&!s.priority[m_d-1])s.priority[m_d-1]=4;
else { MessageBox("優先次序輸入有誤!"); return; }
}
if(m_keynum>4)
{
if(m_e>=1&&m_e<=m_keynum&&!s.priority[m_e-1])s.priority[m_e-1]=5;
else { MessageBox("優先次序輸入有誤!"); return; }
}
s.ChangetoInt(m_maintext);
a.Format("%d",m_num);
b.Format("%d",m_keynum);
if(!m_radio1) //選中了LSD-歸并
{
time=s.LSDsort_a();
m_maintext=s.Result();
con="LSD-歸并:\r\n";
}
else if(m_radio1==1) //選中了LSD-分配收集
{
time=s.LSDsort_b(head);
m_maintext=s.Result(head);
con="LSD-分配收集:\r\n";
}
else if(m_radio1==2) //選中了MSD-歸并
{
time=s.MSDsort();
m_maintext=s.Result();
con="MSD-歸并:\r\n";
}
c.Format("%d",time);
con+=a+"個記錄,"+b+"個關鍵字,共"+c+"次。\r\n"; //顯示分析結果
m_minortext+=con;
UpdateData(false);
}
void CMultiSortDlg::OnRandom() //“隨機產生”
{
UpdateData(true);
m_maintext.Empty();
CString ran;
CString temp;
srand((unsigned)time(NULL));
int i,j;
for(i=0;i<m_num;i++)
{
for(j=0;j<m_keynum;j++)
{
ran.Format("%3d",rand()%101);
temp+=ran+" "; //原為result+=...耗費了大量時間
}
m_maintext+=temp+"\r\n";
temp.Empty();
}
UpdateData(false);
}
void CMultiSortDlg::OnExplain() //“說明”
{
MessageBox("待排序記錄個數輸入范圍為1-10000,關鍵字個數范圍為1-5。\r\n優先次序不同關鍵字不能輸入相同的數字,按優先次序填入1,2,3....\r\n若選取“打開文件”的方式輸入數據,記錄個數和關鍵字個數的輸入需與情況相符。\r\n");
}
void CMultiSortDlg::OnHandin() //“確認”
{
UpdateData(true);
if(m_keynum<1||m_keynum>5||m_num<1||m_num>10000)
{
MessageBox("非法輸入!請參照“說明”。");
return;
}
switch(m_keynum)
{
case 1:
{
GetDlgItem(IDC_a)->EnableWindow(true);
GetDlgItem(IDC_b)->EnableWindow(false);
GetDlgItem(IDC_c)->EnableWindow(false);
GetDlgItem(IDC_d)->EnableWindow(false);
GetDlgItem(IDC_e)->EnableWindow(false);
break;
}
case 2:
{
GetDlgItem(IDC_a)->EnableWindow(true);
GetDlgItem(IDC_b)->EnableWindow(true);
GetDlgItem(IDC_c)->EnableWindow(false);
GetDlgItem(IDC_d)->EnableWindow(false);
GetDlgItem(IDC_e)->EnableWindow(false);
break;
}
case 3:
{
GetDlgItem(IDC_a)->EnableWindow(true);
GetDlgItem(IDC_b)->EnableWindow(true);
GetDlgItem(IDC_c)->EnableWindow(true);
GetDlgItem(IDC_d)->EnableWindow(false);
GetDlgItem(IDC_e)->EnableWindow(false);
break;
}
case 4:
{
GetDlgItem(IDC_a)->EnableWindow(true);
GetDlgItem(IDC_b)->EnableWindow(true);
GetDlgItem(IDC_c)->EnableWindow(true);
GetDlgItem(IDC_d)->EnableWindow(true);
GetDlgItem(IDC_e)->EnableWindow(false);
break;
}
case 5:
{
GetDlgItem(IDC_a)->EnableWindow(true);
GetDlgItem(IDC_b)->EnableWindow(true);
GetDlgItem(IDC_c)->EnableWindow(true);
GetDlgItem(IDC_d)->EnableWindow(true);
GetDlgItem(IDC_e)->EnableWindow(true);
break;
}
}
GetDlgItem(IDC_RANDOM)->EnableWindow(true);
GetDlgItem(IDC_FILE)->EnableWindow(true);
GetDlgItem(IDC_SORT)->EnableWindow(true);
GetDlgItem(IDC_SAVE)->EnableWindow(true);
GetDlgItem(IDC_EDIT11)->EnableWindow(true);
GetDlgItem(IDC_RADIO1)->EnableWindow(true);
GetDlgItem(IDC_RADIO2)->EnableWindow(true);
GetDlgItem(IDC_RADIO3)->EnableWindow(true);
}
void CMultiSortDlg::OnSave() //“保存”
{
UpdateData(true);
if(m_maintext.IsEmpty()&&m_minortext.IsEmpty())
{
MessageBox("沒有記錄可以保存!");
return;
}
CFileDialog dlg(false, "txt", "Result",OFN_HIDEREADONLY| OFN_OVERWRITEPROMPT,"txt Files(*.txt)|*.txt",NULL);
if(dlg.DoModal()!=IDOK)return;
CString sFileName=dlg.GetPathName();
CStdioFile f;
CFileException e;
char* pFileName = sFileName.GetBuffer(0);
if(!f.Open(pFileName,CFile::modeCreate|CFile::modeWrite,&e))
{
#ifdef _DEBUG
afxDump << "File could not be opened "<< e.m_cause << "\n";
#endif
}
f.WriteString(m_maintext+"\r\n====================\r\n"+m_minortext);
f.Close();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -