我是一个MFC新手所以提出的问题很菜,还请各位耐心教教我谢谢大家了
// 贪心算法最短路径Dlg.cpp : implementation file
//#include "stdafx.h"
#include "贪心算法最短路径.h"
#include "贪心算法最短路径Dlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App Aboutclass 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()/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialogCMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDlg)
//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
//{{AFX_MSG_MAP(CMyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart)
ON_BN_CLICKED(IDC_STATIC_JIEGUO, OnStaticJieguo)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlersBOOL CMyDlg::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
return TRUE; // return TRUE unless you set the focus to a control
}void CMyDlg::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 CMyDlg::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 CMyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}void CMyDlg::OnButtonStart()
{ UpdateData();
CString m_result;
// TODO: Add your control notification handler code here
int search(int*,int,int,int*,int); /*生成单源点最短路径的贪心算法*/
int COST[7][7]={{10,20,50,30,200,200,200},{200,10,25,200,200,70,200},{200,200,10,40,25,50,200},{200,200,200,10,55,200,200},{200,200,200,200,10,10,70},{200,200,200,200,200,0,50},{200,200,200,200,200,200,10}};
/*COST[i][j]表示成本邻接矩阵,200表示无穷大*/
int front_point[7]={0,0,0,0,0,0,0};/*用来存放各结点的最短路径上的前一结点*/
int DIST[7];/*DIST[i]表示第i个结点到源结点的路径长度*/
int S[7];/*S中表示对其已经生成了最短路径的那些结点,S[i]=1表示第i个结点在S中,否则不在*/
int u,num,i,w,j,k;
for(k=0;k<7;k++){ /*j对应到其他所有点的最短距离*/
for(i=0;i<7;i++){
front_point[i]=k;
}
for(i=0;i<7;i++){
S[i]=0;/*初始状态时,所有结点均不在S中*/
DIST[i]=COST[k][i];/*各结点到源结点的最短路径的初始长度为它们的直接距离*/
}
S[k]=1;/*将源结点放入S中*/
DIST[k]=0;/*自身到自身的路径为0*/
for(num=2;num<=6;num++){ /*加入五个结点到S中,最后一个不需要*/
int min=32767;
for(w=0;w<=6;w++){ /*找出不在S的结点中到源结点直接距离最短的结点*/
if(!S[w]){
if(DIST[w]<min){
min=DIST[w];
u=w; /*将所找结点位置赋值给u*/
}
}
}
S[u]=1; /*将第u结点放入S中*/
for(w=0;w<=6;w++){ /*加入u结点后,重新计算非S中结点的DIST[i]*/
if(!S[w]&&(DIST[u]+COST[u][w])<DIST[w]){
DIST[w]=DIST[u]+COST[u][w];/*若新的路径短则更新原路径*/
front_point[w]=u;
}
}
}
getchar();
printf("value 源节点到每个节点的最短路径\n");
for(i=0;i<=6;i++){ /*输出结果*/
printf("v%d ",search(front_point,i,i,DIST,k)+1);
printf("v%d\n",i+1);
}
getchar();
}
}
int search(int t[],int n,int m,int D[],int p){
int q=t[n];
if(q==p){
printf(" %d ",D[m]);
return(p);
}
else{
printf("v%d ",search(t,q,m,D,p)+1);
return(t[n]);
}
m_result.format("v%d");
GetDlgItem(IDC_STATIC_JIEGUO)->SetWindowText(m_result);
UpdateData(false);
}用VC6.0运行了出现如下错误
Compiling...
贪心算法最短路径Dlg.cpp
E:\学习及课件\VC++\MSDev98\MyProjects\xsxxglxtzq最终\xsxxglxt\贪心算法最短路径\贪心算法最短路径Dlg.cpp(183) : warning C4101: 'j' : unreferenced local variable
E:\学习及课件\VC++\MSDev98\MyProjects\xsxxglxtzq最终\xsxxglxt\贪心算法最短路径\贪心算法最短路径Dlg.cpp(236) : error C2065: 'm_result' : undeclared identifier
E:\学习及课件\VC++\MSDev98\MyProjects\xsxxglxtzq最终\xsxxglxt\贪心算法最短路径\贪心算法最短路径Dlg.cpp(236) : error C2228: left of '.format' must have class/struct/union type
E:\学习及课件\VC++\MSDev98\MyProjects\xsxxglxtzq最终\xsxxglxt\贪心算法最短路径\贪心算法最短路径Dlg.cpp(237) : error C2660: 'GetDlgItem' : function does not take 1 parameters
E:\学习及课件\VC++\MSDev98\MyProjects\xsxxglxtzq最终\xsxxglxt\贪心算法最短路径\贪心算法最短路径Dlg.cpp(237) : error C2227: left of '->SetWindowTextA' must point to class/struct/union
E:\学习及课件\VC++\MSDev98\MyProjects\xsxxglxtzq最终\xsxxglxt\贪心算法最短路径\贪心算法最短路径Dlg.cpp(238) : error C2065: 'UpdateData' : undeclared identifier
执行 cl.exe 时出错.贪心算法最短路径Dlg.obj - 1 error(s), 0 warning(s)
恳求各位帮忙
// 贪心算法最短路径Dlg.cpp : implementation file
//#include "stdafx.h"
#include "贪心算法最短路径.h"
#include "贪心算法最短路径Dlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App Aboutclass 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()/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialogCMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDlg)
//}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
//{{AFX_MSG_MAP(CMyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart)
ON_BN_CLICKED(IDC_STATIC_JIEGUO, OnStaticJieguo)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlersBOOL CMyDlg::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
return TRUE; // return TRUE unless you set the focus to a control
}void CMyDlg::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 CMyDlg::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 CMyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}void CMyDlg::OnButtonStart()
{ UpdateData();
CString m_result;
// TODO: Add your control notification handler code here
int search(int*,int,int,int*,int); /*生成单源点最短路径的贪心算法*/
int COST[7][7]={{10,20,50,30,200,200,200},{200,10,25,200,200,70,200},{200,200,10,40,25,50,200},{200,200,200,10,55,200,200},{200,200,200,200,10,10,70},{200,200,200,200,200,0,50},{200,200,200,200,200,200,10}};
/*COST[i][j]表示成本邻接矩阵,200表示无穷大*/
int front_point[7]={0,0,0,0,0,0,0};/*用来存放各结点的最短路径上的前一结点*/
int DIST[7];/*DIST[i]表示第i个结点到源结点的路径长度*/
int S[7];/*S中表示对其已经生成了最短路径的那些结点,S[i]=1表示第i个结点在S中,否则不在*/
int u,num,i,w,j,k;
for(k=0;k<7;k++){ /*j对应到其他所有点的最短距离*/
for(i=0;i<7;i++){
front_point[i]=k;
}
for(i=0;i<7;i++){
S[i]=0;/*初始状态时,所有结点均不在S中*/
DIST[i]=COST[k][i];/*各结点到源结点的最短路径的初始长度为它们的直接距离*/
}
S[k]=1;/*将源结点放入S中*/
DIST[k]=0;/*自身到自身的路径为0*/
for(num=2;num<=6;num++){ /*加入五个结点到S中,最后一个不需要*/
int min=32767;
for(w=0;w<=6;w++){ /*找出不在S的结点中到源结点直接距离最短的结点*/
if(!S[w]){
if(DIST[w]<min){
min=DIST[w];
u=w; /*将所找结点位置赋值给u*/
}
}
}
S[u]=1; /*将第u结点放入S中*/
for(w=0;w<=6;w++){ /*加入u结点后,重新计算非S中结点的DIST[i]*/
if(!S[w]&&(DIST[u]+COST[u][w])<DIST[w]){
DIST[w]=DIST[u]+COST[u][w];/*若新的路径短则更新原路径*/
front_point[w]=u;
}
}
}
getchar();
printf("value 源节点到每个节点的最短路径\n");
for(i=0;i<=6;i++){ /*输出结果*/
printf("v%d ",search(front_point,i,i,DIST,k)+1);
printf("v%d\n",i+1);
}
getchar();
}
}
int search(int t[],int n,int m,int D[],int p){
int q=t[n];
if(q==p){
printf(" %d ",D[m]);
return(p);
}
else{
printf("v%d ",search(t,q,m,D,p)+1);
return(t[n]);
}
m_result.format("v%d");
GetDlgItem(IDC_STATIC_JIEGUO)->SetWindowText(m_result);
UpdateData(false);
}用VC6.0运行了出现如下错误
Compiling...
贪心算法最短路径Dlg.cpp
E:\学习及课件\VC++\MSDev98\MyProjects\xsxxglxtzq最终\xsxxglxt\贪心算法最短路径\贪心算法最短路径Dlg.cpp(183) : warning C4101: 'j' : unreferenced local variable
E:\学习及课件\VC++\MSDev98\MyProjects\xsxxglxtzq最终\xsxxglxt\贪心算法最短路径\贪心算法最短路径Dlg.cpp(236) : error C2065: 'm_result' : undeclared identifier
E:\学习及课件\VC++\MSDev98\MyProjects\xsxxglxtzq最终\xsxxglxt\贪心算法最短路径\贪心算法最短路径Dlg.cpp(236) : error C2228: left of '.format' must have class/struct/union type
E:\学习及课件\VC++\MSDev98\MyProjects\xsxxglxtzq最终\xsxxglxt\贪心算法最短路径\贪心算法最短路径Dlg.cpp(237) : error C2660: 'GetDlgItem' : function does not take 1 parameters
E:\学习及课件\VC++\MSDev98\MyProjects\xsxxglxtzq最终\xsxxglxt\贪心算法最短路径\贪心算法最短路径Dlg.cpp(237) : error C2227: left of '->SetWindowTextA' must point to class/struct/union
E:\学习及课件\VC++\MSDev98\MyProjects\xsxxglxtzq最终\xsxxglxt\贪心算法最短路径\贪心算法最短路径Dlg.cpp(238) : error C2065: 'UpdateData' : undeclared identifier
执行 cl.exe 时出错.贪心算法最短路径Dlg.obj - 1 error(s), 0 warning(s)
恳求各位帮忙
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货