Смекни!
smekni.com

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

}

};

#endif // !defined(AFX_GRAPH_H__8C8860CB_4D3F_4F0B_9B81_66289DCC2354__INCLUDED_)

Файл Graph.cpp

// Graph.cpp: implementation of the CGraph class.

//

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

#include "stdafx.h"

#include "KursovikMin.h"

#include "Graph.h"

#ifdef _DEBUG

#undef THIS_FILE

static char THIS_FILE[]=__FILE__;

#define new DEBUG_NEW

#endif

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

// Construction/Destruction

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

CGraph::CGraph()

{

V_count = 0;

}

CGraph::~CGraph()

{

Destroy();

}

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

// Methods

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

void CGraph::Create(int mV_count)

{

Destroy();

if(mV_count <= 0) return;

curV = -1;

V_count = mV_count;

V = new CGraphV[V_count];

E = new CGraphE[V_count*V_count];

Null(-1,0.0);

}

BOOL CGraph::IsExist()

{

return V_count > 0;

}

void CGraph::Null(int m_V=-1, double m_len = -1)

{

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

for(int j=0;j<V_count;j++)

{

E[i*V_count+j].state = FALSE;

E[i*V_count+j].len = m_len;

}

}

void CGraph::Destroy()

{

if(IsExist()) {

delete [] V;

delete [] E;

V = NULL;

E = NULL;

V_count = 0;

}

}

void CGraph::SetV(int m_Vpos, CPoint m_pt, CString m_Title)

{

if(m_Vpos < V_count){

V[m_Vpos].pt = m_pt;

V[m_Vpos].Title = m_Title;

}

}

void CGraph::SetE(int i, int j, double m_len)

{

int pos = i*V_count+j;

if(pos < V_count*V_count)

{E[pos].state = TRUE;E[pos].len = m_len;}

}

void CGraph::SetRand(int A_space, int B_space, int m_Len, double m_p)

{

int COUNT = V_count;

CString str;

srand(time(NULL));

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

{

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

SetV(i,CPoint(A_space+rand()%(B_space-A_space+1),A_space+rand()%(B_space-A_space+1)),str);

}

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

if((rand()+0.0)/RAND_MAX >= m_p) SetE(i / COUNT, i % COUNT, m_Len*rand()/RAND_MAX);

else {E[i].state = FALSE;E[i].len = 0.0;}

}

void CGraph::Show(CDC *pDC, COLORREF c)

{

int i=0, j=0, fn = V_count*V_count;

const int sz = 4;

CPoint pt1, pt2;

CPen p(PS_SOLID,1,c), *old_p;

CString str;

old_p = pDC->SelectObject(&p);

pDC->SetBkMode(TRANSPARENT);

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

{

pDC->Ellipse(V[i].pt.x-sz,V[i].pt.y-sz,V[i].pt.x+sz,V[i].pt.y+sz);

pDC->TextOut(V[i].pt.x,V[i].pt.y,V[i].Title);

}

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

if(E[i].state)

{

pt1 = V[i/V_count].pt; pt2 = V[i%V_count].pt;

//str.Format("%7.3f",E[i].len);

pDC->MoveTo(pt1);

pDC->LineTo(pt2);

//pDC->TextOut((pt1.x+pt2.x)/2,(pt1.y+pt2.y)/2,str);

}

pDC->SelectObject(old_p);

}

void CGraph::Save(CString fname)

{

int i=0,j=0,len = 0;

const UINT Separator = 0xffff;

char buf[81];

FILE *file = NULL;

file = fopen(fname,"wb");

if(file != NULL){

fwrite(&V_count,sizeof(V_count),1,file);

for(i=0;i<V_count;i++){

fwrite(&V[i].pt.x,sizeof(V[i].pt.x),1,file);

fwrite(&V[i].pt.y,sizeof(V[i].pt.y),1,file); len = V[i].Title.GetLength();

fwrite(&len,sizeof(int),1,file);

//fwrite(&V[i].Title,len,1,file);

for(j=0;j<len;j++)

buf[j] = V[i].Title[j];

buf[len]=0;

fwrite(&buf[0],len,1,file);

}

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

{

fwrite(&E[i].state,sizeof(E[i].state),1,file);

fwrite(&E[i].len,sizeof(E[i].len),1,file);

}

fclose(file);

}

}

void CGraph::Load(CString fname)

{

int i=0, len = 0;

const UINT Separator = 0xffff;

char buf[81];

FILE *file = NULL;

file = fopen(fname,"rb");

if(file != NULL){

Destroy();

fread(&i,sizeof(i),1,file);

Create(i);

for(i=0;i<V_count;i++){

fread(&V[i].pt.x,sizeof(V[i].pt.x),1,file);

fread(&V[i].pt.y,sizeof(V[i].pt.y),1,file);

fread(&len,sizeof(int),1,file);

fread(&buf[0],len,1,file); buf[len] = 0;

V[i].Title = buf;

}

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

{

fread(&E[i].state,sizeof(E[i].state),1,file);

fread(&E[i].len,sizeof(E[i].len),1,file);

}

fclose(file);

}

}

void CGraph::MoveV(int m_x, int m_y)

{

if(curV >=0 && curV < V_count)

V[curV].pt = CPoint(m_x,m_y);

}

void CGraph::DeleteV(int indexV)

{

int i=0, j=0;

if(!IsExist()) return;

if(indexV>=0 && indexV<V_count)

{

CGraph g;

int i1 = 0, j1 = 0;

g.Create(V_count-1);

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

if(i != indexV) g.V[i1++] = V[i];

i1 = 0; j1 = 0;

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

{

if(i != indexV){

j1 = 0;

for(j=0;j<V_count;j++)

if(j != indexV) {g.E[i1*(V_count-1)+j1] = E[i*V_count+j];j1++;}

i1++;

}

}

Destroy();

*this = g;

}

}

void CGraph::AddV(CPoint pt, CString m_Title)

{

int i=0;

CGraphV *V1 = new CGraphV[V_count+1];

CGraphE *E1 = new CGraphE[(V_count+1)*(V_count+1)];

for(i=0;i<V_count;i++) {V1[i].pt = V[i].pt; V1[i].Title = V[i].Title;}

V1[i].pt = pt; V1[i].Title = m_Title;

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

{

E1[i].state = E[i].state;

E1[i].len = E[i].len;

}

for(i=0;i<V_count*V_count;i++) {

E1[(V_count-1)*V_count+i].state = FALSE;

E1[(V_count-1)*V_count+i].len = 0.0;

}

Create(V_count+1);

for(i=0;i<V_count;i++) {V[i].pt = V1[i].pt; V[i].Title = V1[i].Title;}

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

{

E[i].state = E1[i].state;

E[i].len = E1[i].len;

}

delete [] V1;

delete [] E1;

}

void CGraph::MakeFull()

{

for(int i=0;i<V_count*V_count;i++)

E[i].state = TRUE;

}

Файл KursovikMinView.h

// KursovikMinView.h : interface of the CKursovikMinView class

//

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

#if !defined(AFX_KURSOVIKMINVIEW_H__8A69BF1A_16AF_4508_9EB6_7960CF0CBACD__INCLUDED_)

#define AFX_KURSOVIKMINVIEW_H__8A69BF1A_16AF_4508_9EB6_7960CF0CBACD__INCLUDED_

#include "Graph.h"

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

class CGrpah;

class CKursovikMinView : public CScrollView

{

protected: // create from serialization only

CKursovikMinView();

DECLARE_DYNCREATE(CKursovikMinView)

// Attributes

public:

CKursovikMinDoc* GetDocument();

int mode;

CGraph m_graph;

CGraph m_Ngraph;

// Operations

public:

// Overrides

// ClassWizard generated virtual function overrides

//{{AFX_VIRTUAL(CKursovikMinView)

public:

virtual void OnDraw(CDC* pDC); // overridden to draw this view

virtual BOOL PreCreateWindow(CREATESTRUCT& cs);

protected:

virtual void OnInitialUpdate(); // called first time after construct

virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);

virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);

virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);

//}}AFX_VIRTUAL

// Implementation

public:

virtual ~CKursovikMinView();

#ifdef _DEBUG

virtual void AssertValid() const;

virtual void Dump(CDumpContext& dc) const;

#endif

protected:

// Generated message map functions

protected:

//{{AFX_MSG(CKursovikMinView)

afx_msg void OnLButtonUp(UINT nFlags, CPoint point);

afx_msg void OnFileSave();

afx_msg void OnFileOpen();

afx_msg void OnMouseMove(UINT nFlags, CPoint point);

afx_msg void OnRButtonUp(UINT nFlags, CPoint point);

afx_msg void OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags);

afx_msg void OnEditDialog();

afx_msg void OnEditMakefullgraph();

afx_msg void OnEditTestOnFull();

afx_msg void OnFileNew();

afx_msg void OnShowGraph();

afx_msg void OnShowGraphs();

afx_msg void OnShowNgraph();

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

#ifndef _DEBUG // debug version in KursovikMinView.cpp

inline CKursovikMinDoc* CKursovikMinView::GetDocument()

{ return (CKursovikMinDoc*)m_pDocument; }

#endif

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

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_KURSOVIKMINVIEW_H__8A69BF1A_16AF_4508_9EB6_7960CF0CBACD__INCLUDED_)

Файл KursovikMinView.cpp

// KursovikMinView.cpp : implementation of the CKursovikMinView class

//

#include "stdafx.h"

#include "KursovikMin.h"

#include "KursovikMinDoc.h"

#include "KursovikMinView.h"

#include "GraphSettinngs.h"