Смекни!
smekni.com

Программа решения задачи о графах (стр. 3 из 3)

#include <math.h>

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

/////////////////////////////////////////////////////////////////////////////

// CKursovikMinView

IMPLEMENT_DYNCREATE(CKursovikMinView, CScrollView)

BEGIN_MESSAGE_MAP(CKursovikMinView, CScrollView)

//{{AFX_MSG_MAP(CKursovikMinView)

ON_WM_LBUTTONUP()

ON_COMMAND(ID_FILE_SAVE, OnFileSave)

ON_COMMAND(ID_FILE_OPEN, OnFileOpen)

ON_WM_MOUSEMOVE()

ON_WM_RBUTTONUP()

ON_WM_KEYUP()

ON_COMMAND(ID_EDIT_DIALOG, OnEditDialog)

ON_COMMAND(ID_EDIT_MAKEFULLGRAPH, OnEditMakefullgraph)

ON_COMMAND(ID_EDIT_TEST_ON_FULL, OnEditTestOnFull)

ON_COMMAND(ID_FILE_NEW, OnFileNew)

ON_COMMAND(ID_SHOW_GRAPH, OnShowGraph)

ON_COMMAND(ID_SHOW_GRAPHS, OnShowGraphs)

ON_COMMAND(ID_SHOW_NGRAPH, OnShowNgraph)

//}}AFX_MSG_MAP

// Standard printing commands

ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)

ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)

ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

// CKursovikMinView construction/destruction

CKursovikMinView::CKursovikMinView()

{

// TODO: add construction code here

const int COUNT = 8;

mode = 0;

const CString fname = "Graph.dat";

CString str;

CFile f;

if(f.Open(fname,CFile::modeRead))

{

f.Close();

m_graph.Load(fname);

}

else{

MessageBox("Ôàéë "+fname+" íå íàéäåí&bsol;nÃðàô áóäåò ñîçäàí ñëó÷àéíûì îáðàçîì","Íåò ôàéëà",MB_OK);

m_graph.Create(COUNT);

m_graph.SetRand(30,150,25,0.5);

}

/*

srand(time(NULL));

for(int i=0;i<COUNT;i++ )

{

str.Format("Point-%i",i);

m_graph.SetV(i,CPoint(20+rand()%100,20+rand()%100),str);

}

for(i=0;i<COUNT*COUNT;i++)

if(rand() % 5 == 0) m_graph.SetE(i / COUNT, i % COUNT, 5.0+(1.0+rand())/RAND_MAX);

*/

}

CKursovikMinView::~CKursovikMinView()

{

}

BOOL CKursovikMinView::PreCreateWindow(CREATESTRUCT& cs)

{

// TODO: Modify the Window class or styles here by modifying

// the CREATESTRUCT cs

return CScrollView::PreCreateWindow(cs);

}

/////////////////////////////////////////////////////////////////////////////

// CKursovikMinView drawing

void CKursovikMinView::OnDraw(CDC* pDC)

{

CKursovikMinDoc* pDoc = GetDocument();

ASSERT_VALID(pDoc);

switch(mode){

case 0 :m_graph.Show(pDC,RGB(0,0,0)); break;

case 1 :m_Ngraph.Show(pDC,RGB(0,255,0)); break;

case 2 :m_graph.Show(pDC,RGB(0,0,0)); break;

default : m_graph.Show(pDC); break;

}

// TODO: add draw code for native data here

}

void CKursovikMinView::OnInitialUpdate()

{

CScrollView::OnInitialUpdate();

CSize sizeTotal;

// TODO: calculate the total size of this view

sizeTotal.cx = sizeTotal.cy = 100;

SetScrollSizes(MM_TEXT, sizeTotal);

}

/////////////////////////////////////////////////////////////////////////////

// CKursovikMinView printing

BOOL CKursovikMinView::OnPreparePrinting(CPrintInfo* pInfo)

{

// default preparation

return DoPreparePrinting(pInfo);

}

void CKursovikMinView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)

{

// TODO: add extra initialization before printing

}

void CKursovikMinView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)

{

// TODO: add cleanup after printing

}

/////////////////////////////////////////////////////////////////////////////

// CKursovikMinView diagnostics

#ifdef _DEBUG

void CKursovikMinView::AssertValid() const

{

CScrollView::AssertValid();

}

void CKursovikMinView::Dump(CDumpContext& dc) const

{

CScrollView::Dump(dc);

}

CKursovikMinDoc* CKursovikMinView::GetDocument() // non-debug version is inline

{

ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CKursovikMinDoc)));

return (CKursovikMinDoc*)m_pDocument;

}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////

// CKursovikMinView message handlers

void CKursovikMinView::OnFileSave()

{

// TODO: Add your command handler code here

CString fname;

CFileDialog dlg(FALSE,"dat","*.dat");

if(dlg.DoModal()==IDOK)

m_graph.Save(dlg.GetFileName());

}

void CKursovikMinView::OnFileOpen()

{

// TODO: Add your command handler code here

CString fname;

CFileDialog dlg(TRUE,"dat","*.dat");

if(dlg.DoModal()==IDOK)

m_graph.Load(dlg.GetFileName());

Invalidate();

}

void CKursovikMinView::OnLButtonUp(UINT nFlags, CPoint point)

{

// TODO: Add your message handler code here and/or call default

//m_graph.SetRand(30,250,25,0.8);

CScrollView::OnLButtonUp(nFlags, point);

}

void CKursovikMinView::OnMouseMove(UINT nFlags, CPoint point)

{

// TODO: Add your message handler code here and/or call default

if(nFlags & MK_RBUTTON) {

m_graph.MoveV(point.x,point.y);

Invalidate();

}

CScrollView::OnMouseMove(nFlags, point);

}

void CKursovikMinView::OnRButtonUp(UINT nFlags, CPoint point)

{

// TODO: Add your message handler code here and/or call default

mode = 0;

m_graph.SetCurV(0);

m_Ngraph.Destroy();

for(int i=0;i<m_graph.V_count;i++)

if((fabs(m_graph.V[i].pt.x-point.x) < 10) && (fabs(m_graph.V[i].pt.y-point.y) < 10))

{ m_graph.SetCurV(i); break; }

Invalidate();

CScrollView::OnRButtonUp(nFlags, point);

}

void CKursovikMinView::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)

{

// TODO: Add your message handler code here and/or call default

switch(nChar)

{

case '0':

m_graph.SetRand(30,250,25,0.8);

Invalidate();

break;

//default: MessageBox("key = "+nChar); break;

}

CScrollView::OnKeyUp(nChar, nRepCnt, nFlags);

}

void CKursovikMinView::OnEditDialog()

{

// TODO: Add your command handler code here

CGraphSettinngs dlg;

dlg.Init(&m_graph);

dlg.DoModal();

Invalidate();

}

void CKursovikMinView::OnEditMakefullgraph()

{

// TODO: Add your command handler code here

int i=0,j=0,Vs = m_graph.V_count*m_graph.V_count;

BOOL IsFull = FALSE;

for(i=0;i<Vs;i++)

m_graph.E[i].state = TRUE;

Invalidate();

}

void CKursovikMinView::OnEditTestOnFull()

{

int i=0,j=0,Vs = m_graph.V_count;

BOOL IsFull = FALSE;

CString str;

for(i=0;i<Vs;i++)

{

for(j=0;j<Vs-i;j++)

if(i != j && (!m_graph.E[i*Vs+j].state || !m_graph.E[j*Vs+i].state)) {IsFull = TRUE; break;}

if(IsFull) break;

}

if(!IsFull) MessageBox("Äàííûé ãðàô - ïîëíûé","Test results",MB_OK);

else{

str.Format("Äàííûé ãðàô - íå ÿâëÿåòñÿ ïîëíûì&bsol;nÍåò ñîåäèíåíèÿ âåðøèí (%i,%i)",i,j);

MessageBox(str,"Test results",MB_OK);

}

}

void CKursovikMinView::OnFileNew()

{

// TODO: Add your command handler code here

m_graph.SetRand(30,250,25,0.8);

Invalidate();

}

void CKursovikMinView::OnShowGraph()

{

// TODO: Add your command handler code here

mode = 0;

Invalidate();

}

void CKursovikMinView::OnShowGraphs()

{

// TODO: Add your command handler code here

mode = 0;

/*

m_Ngraph.Destroy();

m_Ngraph = m_graph;

!m_Ngraph;

*/

m_graph.MakeFull();

Invalidate();

}

void CKursovikMinView::OnShowNgraph()

{

// TODO: Add your command handler code here

mode = 1;

m_Ngraph = m_graph;

!m_Ngraph;

Invalidate();

}