📄 树dlg.cpp
字号:
// 树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 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()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog
CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyDlg)
m_ncost = 0;
m_bnewgraph=TRUE;
m_stroutput="";
m_str1 = _T("");
//}}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)
DDX_Control(pDX, IDC_COMBO_POINT2, m_cmb2);
DDX_Control(pDX, IDC_COMBO_POINT1, m_cmb1);
DDX_Text(pDX, IDC_COST, m_ncost);
DDV_MinMaxInt(pDX, m_ncost, 0, 10000);
DDX_Text(pDX, IDC_OUTPUT, m_str1);
//}}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_INSERT, OnButtonInsert)
ON_BN_CLICKED(IDC_ENTER, OnEnter)
ON_BN_CLICKED(IDC_CLEAN, OnClean)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers
BOOL 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 CreateMGraph(MGraph &G)
{
int i,j;
G.vexnum=10;
G.vexs[0]='A';
G.vexs[1]='B';
G.vexs[2]='C';
G.vexs[3]='D';
G.vexs[4]='E';
G.vexs[5]='F';
G.vexs[6]='G';
G.vexs[7]='H';
G.vexs[8]='I';
G.vexs[9]='J';
for (i=0;i<G.vexnum;i++)
for (j=0;j<G.vexnum;j++)
G.arcs[i][j]=0;
G.arcnum=0;
return;
}
int LocateVex(MGraph G,char u)
{
int i;
for (i=0;i<G.vexnum;i++)
{
if (u == G.vexs[i])
return i;
}
if (i== G.vexnum)
{
printf("Error u!\n");
exit(1);
}
return 0;
}
typedef int VRType;
struct CLOSEDGE{
char adjvex;
VRType lowcost;
}closedge[MAX_VERTEX_NUM];
void CMyDlg::OnButtonInsert()
{
// TODO: Add your control notification handler code here
UpdateData(1);
if(m_bnewgraph==TRUE)
{
CreateMGraph(G);
m_bnewgraph=FALSE;
}
m_nv1=m_cmb1.GetCurSel();
m_nv2=m_cmb2.GetCurSel();
cost=m_ncost;
if(m_nv1!=m_nv2)
{
InsertEdge(m_nv1,m_nv2,cost);
ShowMGraph(G);
}
else MessageBox("输入错误,请重新输入");
UpdateData(0);
}
void CMyDlg::InsertEdge(int v1, int v2, int cost)
{
G.arcs[v1][v2]=cost;
G.arcs[v2][v1]=cost;
G.arcnum++;
}
void CMyDlg::OnEnter()
{
// TODO: Add your control notification handler code here
if(m_bnewgraph==false)
{
MiniSpanTree_PRIM(G,'A');
MessageBox(m_stroutput,NULL,MB_OK);
}
else MessageBox(_T("未生成布线"),NULL,MB_OK);
}
void CMyDlg::MiniSpanTree_PRIM(MGraph G, char u)
{
m_stroutput="";
CString str;
int a,allcost=0;
int k,j,i,minCost;
k=LocateVex(G,u);
for (j=0;j<G.vexnum;++j)
{
if (j!=k)
{
closedge[j].adjvex=u;
closedge[j].lowcost=G.arcs[k][j];
}
}
closedge[k].lowcost=-1;
for (i=1;i<G.vexnum;++i)
{
if(a==k)a=-1;
minCost=1000;
for (j=0;j<G.vexnum;++j)
{
if (closedge[j].lowcost<minCost&&closedge[j].lowcost>0)
{
minCost=closedge[j].lowcost;
k=j;
a=k;
}
}
if(a!=-1)
{
str.Format("%c%c ",closedge[k].adjvex,G.vexs[k]);
m_stroutput=m_stroutput+str;
allcost=allcost+closedge[k].lowcost;
closedge[k].lowcost=-1;
for (j=0;j<G.vexnum;++j)
{
if ((G.arcs[k][j]<closedge[j].lowcost&&G.arcs[k][j]!=0&&closedge[j].lowcost!=-1)
||closedge[j].lowcost==0)
{
closedge[j].adjvex=G.vexs[k];
closedge[j].lowcost=G.arcs[k][j];
}
}
}
else
{
m_stroutput=_T("这十个办公点不联通,不能建立局域网");
return;
}
}
if(allcost>0)
{
str.Format("\n总成本为%d",allcost);
m_stroutput=m_stroutput+str;
}
else
m_stroutput=_T("这十个办公点不联通,不能建立局域网");
}
void CMyDlg::ShowMGraph(MGraph G)
{
CString str,all;
for(int i=0;i<G.vexnum ;i++)
{
str.Format("\t%c",G.vexs[i]);
all=all+str;
}
for(i=0;i<G.vexnum;i++)
{
str.Format("\n%c\t",G.vexs[i]);
all=all+str;
for(int j=0;j<G.vexnum;j++)
{
str.Format("%d\t",G.arcs[i][j]);
all=all+str;
}
}
m_str1=all;
}
void CMyDlg::OnClean()
{
// TODO: Add your control notification handler code here
UpdateData(1);
m_str1="";
m_bnewgraph=TRUE;
UpdateData(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -