📄 mygaview.cpp
字号:
// MyGAView.cpp : implementation of the CMyGAView class
//
#include "stdafx.h"
#include "MyGA.h"
#include "MyGADoc.h"
#include "MyGAView.h"
#include "GACtlDlg.h"
#include "GA.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyGAView
IMPLEMENT_DYNCREATE(CMyGAView, CView)
BEGIN_MESSAGE_MAP(CMyGAView, CView)
//{{AFX_MSG_MAP(CMyGAView)
ON_COMMAND(ID_GADLG, OnGadlg)
ON_COMMAND(ID_SAVE, OnSave)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyGAView construction/destruction
CMyGAView::CMyGAView()
{
// TODO: add construction code here
}
CMyGAView::~CMyGAView()
{
}
BOOL CMyGAView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMyGAView drawing
void CMyGAView::OnDraw(CDC* pDC)
{
CMyGADoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CMyGAView printing
BOOL CMyGAView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMyGAView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMyGAView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMyGAView diagnostics
#ifdef _DEBUG
void CMyGAView::AssertValid() const
{
CView::AssertValid();
}
void CMyGAView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMyGADoc* CMyGAView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyGADoc)));
return (CMyGADoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMyGAView message handlers
void CMyGAView::OnGadlg()
{
// TODO: Add your command handler code here
CGACtlDlg dlg;
CGA *pGA=new CGA();
CClientDC *pDC=new CClientDC(this);
CString str[15];
if(dlg.DoModal()==IDOK)
{
pGA->popSize=dlg.m_popsize;
pGA->lchrom=dlg.m_lchorm;
genMax=pGA->genMax=dlg.m_maxgen;
pGA->pcross=dlg.m_pcross;
pGA->pmutation=dlg.m_pmutation;
pGA->Kmax=dlg.m_CalNum;
pGA->CalculateGA(pDC,str);
OutFitness=new double[genMax];
for(int i=0;i<genMax;i++)
{
OutFitness[i]=pGA->Outfitness[i]/(1.05*pGA->Outfitness[genMax-1]);
}
}
pDC->DeleteDC();
}
void CMyGAView::OnSave()
{
// TODO: Add your command handler code here
FileWriteTxt1("",genMax,OutFitness);
}
void CMyGAView::FileWriteTxt1(CString FileName, int n, double x[])
{
int i;
CString Str;
FileName=FileName+".txt";
CFileDialog dlg(FALSE,_T("txt"),NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
_T("文本文件 (*.txt)|*.txt"));
if(IDOK==dlg.DoModal())
{
FileName=dlg.GetFileName();
CStdioFile file;//CStdioFile类是CFile类的派生类,它是以流方式操作文本
if(file.Open(FileName,CFile::modeCreate|CFile::modeWrite|CFile::typeText)==0)
{
Str="创建文件"+FileName+"失败";
AfxMessageBox(Str); //创建文件失败报告并返回
return;
}
Str.Format("%5d\n",n); //写入文件主题
file.WriteString(Str);
for(i=0;i<=n-1;i++)
{
Str.Format("%f\n",x[i]); //写入文件数据
file.WriteString(Str);
}
file.SetLength(file.GetPosition()); //设置文件长度
file.Close(); //关闭文件
Str="notepad "+FileName;
WinExec(Str,SW_SHOW); //利用记事本打开生成的文本文件
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -