📄 gradecluserview.cpp
字号:
// GradeCluserView.cpp : implementation of the CGradeCluserView class
//
#include "stdafx.h"
#include "GradeCluser.h"
#include "GradeCluserDoc.h"
#include "GradeCluserView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CGradeCluserView
IMPLEMENT_DYNCREATE(CGradeCluserView, CView)
BEGIN_MESSAGE_MAP(CGradeCluserView, CView)
//{{AFX_MSG_MAP(CGradeCluserView)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_WM_LBUTTONDOWN()
ON_COMMAND(IDM_CLEAR, OnClear)
ON_COMMAND(IDM_GO, OnGo)
ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGradeCluserView construction/destruction
CGradeCluserView::CGradeCluserView()
{
// TODO: add construction code here
}
CGradeCluserView::~CGradeCluserView()
{
}
BOOL CGradeCluserView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CGradeCluserView drawing
void CGradeCluserView::OnDraw(CDC* pDC)
{
CGradeCluserDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CRect rect;
GetClientRect(rect);
pDC->TextOut(rect.right/2+4,rect.bottom/2+4,"O");
pDC->Ellipse(rect.right/2+4,rect.bottom/2+4,rect.right/2-4,rect.bottom/2-4);
for (int i=rect.right/2;i<=rect.right;i+=40)
{
pDC->MoveTo(i,rect.bottom/2);
pDC->LineTo(i,rect.bottom/2-15);
CPen mypen(PS_DOT,1,RGB(255,0,0));
CPen* oldpen=pDC->SelectObject(&mypen);
pDC->MoveTo(i+40,rect.bottom);
pDC->LineTo(i+40,0);
pDC->SelectObject(oldpen);
pDC->MoveTo(i-20,rect.bottom/2);
pDC->LineTo(i-20,rect.bottom/2-7);
}
for (i=rect.right/2;i>=0;i-=40)
{
pDC->MoveTo(i,rect.bottom/2);
pDC->LineTo(i,rect.bottom/2-15);
CPen mypen(PS_DOT,1,RGB(255,0,0));
CPen* oldpen=pDC->SelectObject(&mypen);
pDC->MoveTo(i-40,rect.bottom);
pDC->LineTo(i-40,0);
pDC->SelectObject(oldpen);
pDC->MoveTo(i-20,rect.bottom/2);
pDC->LineTo(i-20,rect.bottom/2-7);
}
for (i=rect.bottom/2;i<=rect.bottom;i+=40)
{
pDC->MoveTo(rect.right/2,i);
pDC->LineTo(rect.right/2+15,i);
CPen mypen(PS_DOT,1,RGB(255,0,0));
CPen* oldpen=pDC->SelectObject(&mypen);
pDC->MoveTo(0,i);
pDC->LineTo(rect.right,i);
pDC->SelectObject(oldpen);
pDC->MoveTo(rect.right/2,i-20);
pDC->LineTo(rect.right/2+7,i-20);
}
for (i=rect.bottom/2;i>=0;i-=40)
{
pDC->MoveTo(0,i);
pDC->LineTo(rect.right/2+15,i);
CPen mypen(PS_DOT,1,RGB(255,0,0));
CPen* oldpen=pDC->SelectObject(&mypen);
pDC->MoveTo(0,i);
pDC->LineTo(rect.right,i);
pDC->SelectObject(oldpen);
pDC->MoveTo(rect.right/2,i-20);
pDC->LineTo(rect.right/2+7,i-20);
}
pDC->MoveTo(0,rect.bottom/2);//bottom://客户区最下面的数值
pDC->LineTo(rect.right,rect.bottom/2);//right://客户区右边的数值
pDC->MoveTo(rect.right/2,0);
pDC->LineTo(rect.right/2,rect.bottom);
}
/////////////////////////////////////////////////////////////////////////////
// CGradeCluserView diagnostics
#ifdef _DEBUG
void CGradeCluserView::AssertValid() const
{
CView::AssertValid();
}
void CGradeCluserView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CGradeCluserDoc* CGradeCluserView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGradeCluserDoc)));
return (CGradeCluserDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CGradeCluserView message handlers
void CGradeCluserView::OnFileOpen()
{
// TODO: Add your command handler code here
CFileDialog dlg(true,NULL,NULL,NULL,_T("本例程数据文件 (*.txt)|*.txt|所有文件 (*.*)|*.*||"),NULL);
if (dlg.DoModal()==IDCANCEL)
{
return;
}
FILE *file_swatch; //实验数据文件文件
file_swatch=fopen(dlg.GetPathName(),"rt"); //以读的方式打开样本文件
int num_swatch/*,num_class*/; //实验数据数目
CDC* pdc=this->GetDC();
CRect re;
CPoint mypoint(0,0);
GetClientRect(re);
CPoint Oxis;
Oxis.x=re.right/2;
Oxis.y=re.bottom/2;
if (file_swatch==NULL)
{
AfxMessageBox("文件不能正常打开!",MB_OKCANCEL,NULL);
}
else
{
fscanf(file_swatch,"%d",&num_swatch/*,&num_class*/);
for (int i=0;i<num_swatch;i++)
{
fscanf(file_swatch,"%d %d",&mypoint.x,&mypoint.y);
mypoint=Trans(Oxis,mypoint);
PNTVECTOR v;
v.push_back(mypoint);
m_pnts.push_back(v);
int x1=mypoint.x+5;
int y1=mypoint.y+5;
int x2=mypoint.x-5;
int y2=mypoint.y-5;
pdc->Ellipse(x1,y1,x2,y2);
// CString df;
// df.Format("%d-%d",mypoint.x,mypoint.y);
// pdc->TextOut(mypoint.x,mypoint.y,df);
//
}
fclose(file_swatch);
}
this->ReleaseDC(pdc);
}
void CGradeCluserView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
PNTVECTOR v;
v.push_back(point);
m_pnts.push_back(v);
CDC *pdc = this->GetDC();
int x1=point.x+5;
int y1=point.y+5;
int x2=point.x-5;
int y2=point.y-5;
pdc->Ellipse(x1,y1,x2,y2);
//
// CString df;
// df.Format("%d-%d",point.x,point.y);
// pdc->TextOut(point.x,point.y,df);
this->ReleaseDC(pdc);
CView::OnLButtonDown(nFlags, point);
}
void CGradeCluserView::OnClear()
{
// TODO: Add your command handler code here
m_pnts.clear();
CDC *pdc = this->GetDC();
CRect rect;
GetClientRect(rect);
pdc->FillRect(rect,NULL);
OnDraw(pdc);
this->ReleaseDC(pdc);
}
void CGradeCluserView::OnGo()
{
// TODO: Add your command handler code here
CInputDlg dlg;
int i;
CDC *pdc = this->GetDC();
dlg.DoModal();
i=dlg.m_num;
if (m_pnts.size()<i)
{
MessageBox("分类数目错误","提示:",MB_OK);
}
else
{
if(GradeCluser(m_pnts, i))
{
int i=0;
int j=0;
for(VECTORLIST::iterator vit=m_pnts.begin(); vit!=m_pnts.end(); vit++)
{
i++;
for(PNTVECTOR::iterator pit=vit->begin(); pit!=vit->end(); pit++)
{
if(vit->size()==1 || pit!=vit->begin())
{
j++;
CString temp;
// temp.Format("%d-%d ",pit->x, pit->y);
// pdc->TextOut(100+80*i,100+20*j,temp);
temp.Format("%d", i);
pdc->TextOut(pit->x, pit->y, temp);
}
}
}
}
this->ReleaseDC(pdc);
}
}
CPoint CGradeCluserView::Trans(CPoint Oxis, CPoint Input)
{
Input.x=Oxis.x+Input.x*40;
Input.y=Oxis.y-Input.y*40;
return Input;
}
CPoint CGradeCluserView::Trans(int x1, int y1, int x2, int y2)
{
CPoint mypoint(x2,y2);
mypoint.x=x1+x2*40;
mypoint.y=y1-y2*40;
return mypoint;
}
void CGradeCluserView::OnFileSaveAs()
{
// TODO: Add your command handler code here
CFileDialog save_result_filedlg(false,NULL,NULL,NULL,_T("本例程存档文件 (*.txt)|*.txt|所有文件 (*.*)|*.*||"),NULL);
CString sav_filname;
LPCTSTR lpszPathName;
FILE *sav_result_file;
if (save_result_filedlg.DoModal()==IDOK)
{
sav_filname=save_result_filedlg.GetPathName();
lpszPathName=(LPCTSTR)sav_filname;
sav_result_file=fopen(lpszPathName,"wt");
int i=0;
int j=0;
for(VECTORLIST::iterator vit=m_pnts.begin(); vit!=m_pnts.end(); vit++)
{
i++;
fprintf(sav_result_file,"第%d个分类的样本具体如下\n",i);
for(PNTVECTOR::iterator pit=vit->begin(); pit!=vit->end(); pit++)
{
if(vit->size()==1 || pit!=vit->begin())
{
j++;
CString temp;
fprintf(sav_result_file,"%d %d\n",pit->x, pit->y);
}
}
}
fclose(sav_result_file);
return;
}
else if (save_result_filedlg.DoModal()==IDCANCEL)
{
return;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -