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

📄 new01背包问题dlg.cpp

📁 Knapsack problemnew01背包问题(动态规划) 01背包实验报告
💻 CPP
字号:
// new01背包问题Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "new01背包问题.h"
#include "new01背包问题Dlg.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()

/////////////////////////////////////////////////////////////////////////////
// CNew01Dlg dialog

CNew01Dlg::CNew01Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNew01Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CNew01Dlg)
	m_amount = _T("");
	m_volumn = _T("");
	m_max_value = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CNew01Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNew01Dlg)
	DDX_Text(pDX, IDC_EDIT_AMOUNT, m_amount);
	DDX_Text(pDX, IDC_EDIT_VOLUMN, m_volumn);
	DDX_Text(pDX, IDC_EDIT_MAX_VALUE, m_max_value);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CNew01Dlg, CDialog)
	//{{AFX_MSG_MAP(CNew01Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_AMOUNT, OnBtnAmount)
	ON_BN_CLICKED(IDC_BTN_START, OnBtnStart)
	ON_BN_CLICKED(IDC_BTN_CLEAR, OnBtnClear)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNew01Dlg message handlers

BOOL CNew01Dlg::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
//	a=new int[20];
	a[0]=IDC_EDIT2;
	a[1]=IDC_EDIT3;
	a[2]=IDC_EDIT4;
	a[3]=IDC_EDIT5;

	a[4]=IDC_EDIT6;
	a[5]=IDC_EDIT7;
	a[6]=IDC_EDIT8;
	a[7]=IDC_EDIT9;

	a[8]=IDC_EDIT10;
	a[9]=IDC_EDIT11;
	a[10]=IDC_EDIT12;
	a[11]=IDC_EDIT13;

	a[12]=IDC_EDIT14;
	a[13]=IDC_EDIT15;
	a[14]=IDC_EDIT16;
	a[15]=IDC_EDIT17;

	a[16]=IDC_EDIT18;
	a[17]=IDC_EDIT19;
	a[18]=IDC_EDIT20;
	a[19]=IDC_EDIT21;

	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CNew01Dlg::OnBtnAmount() 
{
	// TODO: Add your control notification handler code here
	GetDlgItemText(IDC_EDIT_AMOUNT,m_amount);
	n=atoi(m_amount);
	for(i=0;i<10-n;i++)
	{
		GetDlgItem(a[9-i])->EnableWindow(false);
		GetDlgItem(a[19-i])->EnableWindow(false);
	}
   GetDlgItemText(IDC_EDIT_VOLUMN,m_volumn);
   c=atoi(m_volumn);

   s=new int[n+1];
	    v=new int[n+1];
	
	m=new int*[n+1];
   for (i=0;i<n+1;i++)
    m[i]=new int[c+1];  
}


void CNew01Dlg::OnBtnStart() 
{

		
	// TODO: Add your control notification handler code here
	for ( i=1;i<n+1;i++) 
	{
        GetDlgItemText(a[i-1],str);
	      s[i]=atoi(str);
		  GetDlgItemText(a[i+9],str);
	      v[i]=atoi(str);
	//	  SetDlgItemText(IDC_EDIT_MAX_VALUE,str);
    // SetDlgItemText(IDC_EDIT_MAX_VALUE,str);
	//	  str="";
		  }



	for (i=0;i<n+1;i++)
  m[i][0]=0;
 for (j=0;j<c+1;j++)
  m[0][j]=0;
 for (i=1;i<n+1;i++)
  for (j=1;j<c+1;j++)
  {if (j<s[i]) m[i][j]=m[i-1][j]; 
   if ((i>0)&&(j>=s[i]))
    m[i][j]=(m[i-1][j]>(m[i-1][j-s[i]]+v[i]) ? m[i-1][j] : m[i-1][j-s[i]]+v[i]);};
   
   m_max_value.Format("%d",m[n][c]);
   SetDlgItemText(IDC_EDIT_MAX_VALUE,m_max_value);
}

void CNew01Dlg::OnBtnClear() 
{
	// TODO: Add your control notification handler code here
	GetDlgItemText(IDC_EDIT_MAX_VALUE,m_max_value);
	if(m_max_value!="")
	{
	
	delete[]s;
	delete[]v;
	}

	else
		AfxMessageBox("已经清空了^_^");
 //   for(i=0;i<n+1;i++)
//		delete[]m;
	for(i=0;i<20;i++)
	{
		SetDlgItemText(a[i],"");
	}
	SetDlgItemText(IDC_EDIT_AMOUNT,"");
	SetDlgItemText(IDC_EDIT_VOLUMN,"");
	SetDlgItemText(IDC_EDIT_MAX_VALUE,"");
	for(i=0;i<20;i++)
		GetDlgItem(a[i])->EnableWindow(true);
}

⌨️ 快捷键说明

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