📄 beibaodlg.cpp
字号:
// beibaoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "beibao.h"
#include "beibaoDlg.h"
#include "GreedyBeibao1.h"
#include "Shuju.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()
/////////////////////////////////////////////////////////////////////////////
// CBeibaoDlg dialog
CBeibaoDlg::CBeibaoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CBeibaoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CBeibaoDlg)
m_CoutResult = _T("");
m_GeShuN = 0;
m_GeShuK = 0;
m_MaxWei = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CBeibaoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBeibaoDlg)
DDX_Text(pDX, IDC_CoutResult, m_CoutResult);
DDX_Text(pDX, IDC_EDIT2, m_GeShuN);
DDX_Text(pDX, IDC_EDIT3, m_GeShuK);
DDX_Text(pDX, IDC_EDIT1, m_MaxWei);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBeibaoDlg, CDialog)
//{{AFX_MSG_MAP(CBeibaoDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_RADIO1, OnShuiji)
ON_BN_CLICKED(IDC_RADIO2, OnShougong)
ON_BN_CLICKED(IDOK, OnBiebao)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBeibaoDlg message handlers
BOOL CBeibaoDlg::OnInitDialog()
{
CDialog::OnInitDialog();
//m_nctlPro=0;
//m_Time.SetRange(0,100);
// 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 CBeibaoDlg::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 CBeibaoDlg::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 CBeibaoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CBeibaoDlg::OnShuiji()
{
// TODO: Add your control notification handler code here
xuanze='1';
}
void CBeibaoDlg::OnShougong()
{
// TODO: Add your control notification handler code here
xuanze='2';
}
void CBeibaoDlg::OnCancel()
{
// TODO: Add extra cleanup here
CDialog::OnCancel();
}
void CBeibaoDlg::OnBiebao()
{
// TODO: Add your control notification handler code here
Shuju ShuJ;
int DlgResult;
UpdateData(true);
CString temp;
GreedyBeibao G;
int result=0;
int n=m_GeShuN;
int k=m_GeShuK;
int m=m_MaxWei;
int f1[101];
int f2[101];
int x[101];
int nu=0;//用来查看是否有可以选到的物品
int total=0;
bool flag[101];
int i,j;
DataType *A=new DataType[n+1];
//-------------------------------------
if(n>=k&&n!=0)
{
CString temp;
switch(xuanze)
{
case '1':
G.RandA(A,n);
temp.Format("\r\n原始数据为:\r\n");
m_CoutResult=m_CoutResult+temp;
temp.Format("标号:\t价值:\t重量:\t\r\n");
m_CoutResult=m_CoutResult+temp;
for( j=1;j<=n;j++)
{
temp.Format("%3d\t",j);
m_CoutResult=m_CoutResult+temp;
temp.Format("%3d\t",A[j].Val);
m_CoutResult=m_CoutResult+temp;
temp.Format("%3d\r\n",A[j].Wei);
m_CoutResult=m_CoutResult+temp;
}
G.Sort(A,n);
break;
case '2':
temp.Format("标号:\t价值:\t重量:\t\r\n");
m_CoutResult=m_CoutResult+temp;
for(i=1;i<=n;i++)
{
DlgResult = ShuJ.DoModal( );
A[i].num1=i;
temp.Format("%3d\t",i);
m_CoutResult=m_CoutResult+temp;
A[i].Val=ShuJ.m_TheVal;
temp.Format("%3d\t",ShuJ.m_TheVal);
m_CoutResult=m_CoutResult+temp;
A[i].Wei=ShuJ.m_TheWei;
temp.Format("%3d\t\r\n",ShuJ.m_TheWei);
m_CoutResult=m_CoutResult+temp;
if ( DlgResult == IDCANCEL )
{
return;
}
}
G.Sort(A,n);
break;
default:
G.RandA(A,n);
G.Sort(A,n);
break;
}
LARGE_INTEGER litmp;
LONGLONG StartT,EndT;
double dfMinus, dfFreq, Tresult;
QueryPerformanceFrequency(&litmp);
dfFreq=(double)litmp.QuadPart;
QueryPerformanceCounter(&litmp);
StartT=litmp.QuadPart;
G.Greedyresult(f1,f2,x,A,flag,nu,n,m,k,result, total);
QueryPerformanceCounter(&litmp);
EndT=litmp.QuadPart;
dfMinus=(double)(EndT-StartT);
Tresult=(dfMinus/dfFreq)*1000;
if(nu!=0)
{
temp.Format("\r\n选取的最优数据为:\r\n序号:\t价值:\t重量:\t初始标号:\t\r\n");
m_CoutResult=m_CoutResult+temp;
//m_nctlPro +=10;
for(i=0;i<=n;i++)
{
CString temp;
if(f1[i]!=0)
{
temp.Format("%d\t",i);
m_CoutResult=m_CoutResult+temp;
temp.Format("%d\t",A[f1[i]].Val);
m_CoutResult=m_CoutResult+temp;
temp.Format("%d\t",A[f1[i]].Wei);
m_CoutResult=m_CoutResult+temp;
temp.Format("%d\t\r\n",A[f1[i]].num1);
m_CoutResult=m_CoutResult+temp;
}
else break;
///m_Time.SetPos(m_nctlPro);
}
temp.Format("\r\n总的组合数:%3d\r\n\r\n",total);//输出总的计算次数
m_CoutResult=m_CoutResult+temp;
temp.Format("花费用的时间为:%3f秒\r\n\r\n",Tresult/1000);
m_CoutResult=m_CoutResult+temp;
temp.Format("最优值是:%3d\r\n\r\n",result);
m_CoutResult=m_CoutResult+temp;
}
else
AfxMessageBox("没有合适的物品可选!");
}
else
{
AfxMessageBox("不可行方法!\n请重新输入!");
}
UpdateData(false);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -