?? cmyview.cpp
字號:
// CMyView.cpp : implementation of the CMyView class
//
#include "stdafx.h"
#include "最小生成樹.h"
#include <math.h>
#include "CMyDoc.h"
#include "CMyView.h"
#include "HelpDlg.h"
#include "AddDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyView
IMPLEMENT_DYNCREATE(CMyView, CView)
BEGIN_MESSAGE_MAP(CMyView, CView)
//{{AFX_MSG_MAP(CMyView)
ON_WM_LBUTTONUP()
ON_COMMAND(ID_MENU_HANDLE, OnHandle)
ON_COMMAND(ID_MENU_AUTO, OnAuto)
ON_COMMAND(W_HELP, OnHelp)
ON_WM_TIMER()
ON_WM_CHAR()
ON_WM_LBUTTONDBLCLK()
ON_WM_CANCELMODE()
ON_WM_CAPTURECHANGED()
ON_COMMAND(ID_ENDATA, OnEnData)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyView construction/destruction
CMyView::CMyView()
{
// TODO: add construction code here
w_isstop = true;
w_ishandle = true;
}
CMyView::~CMyView()
{
}
BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMyView drawing
void CMyView::OnDraw(CDC* pDC)
{
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//當是第一次讀數(shù)據(jù)時,要初始化w_closedge這個輔助計算數(shù)組
if(pDoc->w_isbegindraw){
W_InitClosedge(pDoc);
pDoc->w_isbegindraw = false;
}
int x1,y1,x2,y2,k;
CString str;
map<int,CPoint>::iterator pos;
CPen pen[4], *oldpen;
pen[0].CreatePen(PS_DASHDOT,1,RGB(0,0,0));//畫邊
pen[1].CreatePen(PS_SOLID,2,RGB(0,0,0));//畫頂點圓
pen[2].CreatePen(PS_SOLID,4, RGB(0,255,0));//在圖中畫最小生成樹的線
pen[3].CreatePen(PS_SOLID,4,RGB(255,0,155));//畫輸出結果的邊框
//畫邊、邊的值
oldpen = pDC->SelectObject(&pen[0]);
for(int i=0; i<pDoc->w_vexnum; ++i)
for(int j=0; j<pDoc->w_vexnum; ++j){
if(pDoc->w_arcs[i][j]!=0 && pDoc->w_arcs[i][j]<MAXINT){
pos = pDoc->w_vecmap.find(i);
x1 = pos->second.x;
y1 = pos->second.y;
pDC->MoveTo(x1, y1);
pos = pDoc->w_vecmap.find(j);
x2 = pos->second.x;
y2 = pos->second.y;
pDC->LineTo(x2, y2);
str.Format("%d", pDoc->w_arcs[i][j]);
pDC->TextOut( (x1+x2)/2-2,(y1+y2)/2-2,str);
}
}
//畫頂點、頂點標簽
pDC->SelectObject(&pen[1]);
pos = pDoc->w_vecmap.begin();
while(pos!=pDoc->w_vecmap.end()){
pDC->Ellipse(pos->second.x-R0,pos->second.y-R0,pos->second.x+R0,pos->second.y+R0);
pDC->TextOut(pos->second.x-3,pos->second.y-3,pDoc->w_vecname[pos->first]);
++pos;
}
int x0 = 100,
y0 = 580;
//畫輸出結果的邊框
pDC->SelectObject(&pen[3]);
pDC->Rectangle(0,560,1000,630);
pDC->Rectangle(0,0,XX-R1-20,560);
//畫出頂點之間的關系
k = 0;
for(i=0; i<pDoc->w_vexnum; ++i)
for(int j=0; j<pDoc->w_vexnum; ++j)
if(pDoc->w_arcs[i][j]!=0 && pDoc->w_arcs[i][j]<MAXINT){
str = "";
str.Format("%s——%s %d",pDoc->w_vecname[i],pDoc->w_vecname[j],pDoc->w_arcs[i][j]);
pDC->TextOut(10,50+20*k,str);
++k;
}
//畫已搜索到的最小生成樹
pDC->SetTextColor(RGB(255,0,155));
for(i=0; i<pDoc->w_vexnum; ++i){
if(i!=w_start && w_closedge[i].value==0){
//在圖中畫最小生成樹的線
pDC->SelectObject(&pen[2]);
pDC->MoveTo(pDoc->w_vecmap[i]);
pDC->LineTo(pDoc->w_vecmap[w_closedge[i].vex]);
//同時顯示此線的值
pos = pDoc->w_vecmap.find(i);
x1 = pos->second.x;
y1 = pos->second.y;
pDC->MoveTo(x1, y1);
pos = pDoc->w_vecmap.find(w_closedge[i].vex);
x2 = pos->second.x;
y2 = pos->second.y;
pDC->LineTo(x2, y2);
str.Format("%d", pDoc->w_arcs[i][w_closedge[i].vex]);
pDC->TextOut( (x1+x2)/2-2,(y1+y2)/2-2,str);
//輸出找到的最小生成樹
str = "——";
pDC->TextOut(x0+i*100,y0, pDoc->w_vecname[i]);
pDC->TextOut(x0+i*100+20,y0, str);
pDC->TextOut(x0+i*100+50,y0, pDoc->w_vecname[w_closedge[i].vex]);
}
}
//畫數(shù)組
pDC->SelectObject(oldpen);
}
/////////////////////////////////////////////////////////////////////////////
// CMyView diagnostics
#ifdef _DEBUG
void CMyView::AssertValid() const
{
CView::AssertValid();
}
void CMyView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMyDoc* CMyView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc)));
return (CMyDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMyView message handlers
void CMyView::W_Nextspan() //尋找下一個點
{
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
static int i=0;
int max;
int j,k=0;
if(i<pDoc->w_vexnum){
max=MAXINT;
for(j=0; j<pDoc->w_vexnum; ++j) {//求出下一個結點
if(w_closedge[j].value>0 && max>w_closedge[j].value)
{
k=j;
max=w_closedge[j].value;
}
}
w_closedge[k].value=0;
for(j=0;j<pDoc->w_vexnum; ++j){
if(pDoc->w_arcs[k][j]<w_closedge[j].value){
w_closedge[j].vex=k;
w_closedge[j].value=pDoc->w_arcs[k][j];
}//if
}//for
++i;
}//end_if
else if(i == pDoc->w_vexnum) {//是又一次開始的話
i = 0;
pDoc->w_isbegindraw = true;
}
else
++i;
}
void CMyView::W_InitClosedge(CMyDoc* pDoc)//初始化w_closedge
{
//初始化w_closedge
w_start = 0;
for(int i=0; i<pDoc->w_vexnum; ++i){
w_closedge[i].vex = w_start;
w_closedge[i].value = pDoc->w_arcs[w_start][i];
}
}
void CMyView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(w_ishandle){
W_Nextspan(); //尋找下一個結點
Invalidate();
CView::OnLButtonUp(nFlags, point);
}
}
void CMyView::OnHandle() //手動演示
{
// TODO: Add your command handler code here
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
w_ishandle = true;
pDoc->w_isbegindraw = true;
KillTimer(1);
}
void CMyView::OnAuto() //自動演示
{
// TODO: Add your command handler code here
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if(!w_ishandle){
w_isstop = !w_isstop;
}
else if(w_ishandle){
w_ishandle = false;
w_isstop = false;
pDoc->w_isbegindraw = true;
SetTimer(1,1000,NULL);
}
}
void CMyView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(!w_ishandle && !w_isstop){
W_Nextspan(); //尋找下一個結點
Invalidate();
}
CView::OnTimer(nIDEvent);
}
void CMyView::OnHelp() //操作幫助
{
// TODO: Add your command handler code here
HelpDlg help;
help.DoModal();
}
void CMyView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
CView::OnChar(nChar, nRepCnt, nFlags);
}
void CMyView::OnEnData() //輸入數(shù)據(jù)
{
// TODO: Add your command handler code here
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
char token[10];
CString str;
int i = 0,
j=0,
k=0;
int arcnum,
vexnum;
vector<CString> vecname;
int arcs[10][10];
AddDlg adddlg;
if(adddlg.DoModal())
{
//修改圖的值
arcnum = adddlg.m_arcnum;
vexnum = adddlg.m_vexnum;
if(arcnum<=0)
{
MessageBox("你輸入的頂點數(shù)錯誤!");
return ;
}
//改變vecname
for(i=0; i<vexnum; ++i)
{
k=0;
if(adddlg.m_vnamestr.GetLength()<=j)
{
MessageBox("你輸入的頂點和頂點數(shù)有沖突!");
return;
}
while(adddlg.m_vnamestr.GetLength()>j && adddlg.m_vnamestr[j]==' ') ++j;
while(adddlg.m_vnamestr.GetLength()>j && adddlg.m_vnamestr[j]!=',' && adddlg.m_vnamestr[j]!=' ')
{
token[k++] = adddlg.m_vnamestr[j];
++j;
}
token[k] = '\0';
++j;
if(k>0){
++i;
str = token;
vecname.push_back(str);
}
}
/*
//改變arc值
j = 0;
k = 0;
while(j<adddlg.m_arcstr.GetLength()){
while()
}
*/
//從新畫圖形
pDoc->w_arcnum = arcnum;
pDoc->w_vexnum = vexnum;
pDoc->w_vecname.clear();
pDoc->w_vecname.resize(vecname.size());
copy(pDoc->w_vecname.begin(), pDoc->w_vecname.begin(), vecname.begin());
for(i=0; i<10; ++i)
for(j=0; j<10; ++j)
pDoc->w_arcs[i][j] = arcs[i][j];
int x,y;
double pi = 3.1415926535;
double a = 2*pi/vexnum;
for(int i=0; i<vexnum; ++i){
x = int(XX+R1*sin(i*a));
y = int(YY-R1*cos(i*a));
pDoc->w_vecmap.insert(pair<int,CPoint>(i,CPoint(x,y)));
}
w_isstop = true;
w_ishandle = true;
pDoc->w_isbegindraw = true;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -