📄 kdlg.cpp
字号:
// kDlg.cpp : implementation file
//
#include "stdafx.h"
#include "k.h"
#include "kDlg.h"
#include"fstream"
#include "math.h"
#include "dlgshow.h"
#include ".\kdlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
using namespace std;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CKDlg dialog
CKDlg::CKDlg(CWnd* pParent /*=NULL*/)
: CDialog(CKDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CKDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CKDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CKDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CKDlg, CDialog)
//{{AFX_MSG_MAP(CKDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_IN, OnButtonIn)
ON_BN_CLICKED(IDC_BUTTON_OUT, OnButtonOut)
ON_BN_CLICKED(IDC_BUTTON_RUN, OnButtonRun)
ON_BN_CLICKED(IDC_BUTTON_RESET, OnButtonReset)
//}}AFX_MSG_MAP
ON_EN_CHANGE(IDC_EDIT_CLSNUM, OnEnChangeEditClsnum)
ON_EN_CHANGE(IDC_EDIT_INPUT, OnEnChangeEditInput)
ON_EN_CHANGE(IDC_EDIT_OUTPUT, OnEnChangeEditOutput)
ON_BN_CLICKED(IDC_BUTTON_DRAW, OnBnClickedButtonDraw)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CKDlg message handlers
BOOL CKDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
InFileStr="";
OutFileStr="";
clsnum=0;
RunFlag1=false;
RunFlag2=false;
RunFlag3=true;
CButton *pBut=(CButton *)GetDlgItem(IDC_BUTTON_RUN);
pBut->EnableWindow(FALSE);
CEdit *pEdit=(CEdit *)GetDlgItem(IDC_EDIT_CLSNUM);
pEdit->SetWindowText("2");
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CKDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CKDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CKDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CKDlg::OnButtonIn()
{
// TODO: Add your control notification handler code here
CString FilterStr="文本文件(*.txt)|*.txt|数据文件(*.dat)|*.dat|所有文件(*.*)|*.*||";
CFileDialog FileDlg(TRUE,"*.txt","data.txt",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,FilterStr,NULL);
if(FileDlg.DoModal()!=IDOK)
return;
InFileStr=FileDlg.GetFileName();
CString InFilePath=FileDlg.GetPathName();
CEdit *pEdit=(CEdit *)GetDlgItem(IDC_EDIT_INPUT);
pEdit->SetWindowText(InFilePath);
}
void CKDlg::OnButtonOut()
{
// TODO: Add your control notification handler code here
CString FilterStr="文本文件(*.txt)|*.txt|数据文件(*.dat)|*.dat|所有文件(*.*)|*.*||";
CFileDialog FileDlg(FALSE,"*.txt","result.txt",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,FilterStr,NULL);
if(FileDlg.DoModal()!=IDOK)
return;
OutFileStr=FileDlg.GetFileName();
CString InFilePath=FileDlg.GetPathName();
CEdit *pEdit=(CEdit *)GetDlgItem(IDC_EDIT_OUTPUT);
pEdit->SetWindowText(InFilePath);
}
void CKDlg::OnButtonRun()
{
// TODO: Add your control notification handler code here
BOOL bResult=ReadData(InFileStr,SmpData,Smp,Dim);
if(bResult==FALSE)
return;
bResult=kAveClustering(SmpData,Ans,clsnum,Dim,Smp);
if(bResult==FALSE)
return;
WriteData(OutFileStr,Ans,Smp,Dim);
CButton *pBut=(CButton *)GetDlgItem(IDC_BUTTON_DRAW);
pBut->EnableWindow(TRUE);
}
void CKDlg::OnButtonReset()
{
// TODO: Add your control notification handler code here
CButton *pBut=(CButton *)GetDlgItem(IDC_BUTTON_RUN);
pBut->EnableWindow(FALSE);
CEdit *pEdit=(CEdit *)GetDlgItem(IDC_EDIT_CLSNUM);
pEdit->SetWindowText("2");
pEdit=(CEdit *)GetDlgItem(IDC_EDIT_INPUT);
pEdit->SetWindowText("");
pEdit=(CEdit *)GetDlgItem(IDC_EDIT_OUTPUT);
pEdit->SetWindowText("");
InFileStr="";
OutFileStr="";
clsnum=0;
}
void CKDlg::OnEnChangeEditClsnum()
{
// TODO: 如果该控件是 RICHEDIT 控件,则它将不会
// 发送该通知,除非重写 CDialog::OnInitDialog()
// 函数并调用 CRichEditCtrl().SetEventMask(),
// 同时将 ENM_CHANGE 标志“或”运算到掩码中。
// TODO: 在此添加控件通知处理程序代码
CString str;
CEdit *pEdit=(CEdit *)GetDlgItem(IDC_EDIT_CLSNUM);
pEdit->GetWindowText(str);
clsnum=atoi(str);
if(clsnum>0)
RunFlag3=true;
else
RunFlag3=false;
CButton *pBut=(CButton *)GetDlgItem(IDC_BUTTON_RUN);
if(RunFlag1&&RunFlag2&&RunFlag3)
pBut->EnableWindow(TRUE);
else
pBut->EnableWindow(FALSE);
}
void CKDlg::OnEnChangeEditInput()
{
// TODO: 如果该控件是 RICHEDIT 控件,则它将不会
// 发送该通知,除非重写 CDialog::OnInitDialog()
// 函数并调用 CRichEditCtrl().SetEventMask(),
// 同时将 ENM_CHANGE 标志“或”运算到掩码中。
// TODO: 在此添加控件通知处理程序代码
CString str;
CEdit *pEdit=(CEdit *)GetDlgItem(IDC_EDIT_INPUT);
pEdit->GetWindowText(str);
if(str!="")
RunFlag1=true;
else
RunFlag1=false;
CButton *pBut=(CButton *)GetDlgItem(IDC_BUTTON_RUN);
if(RunFlag1&&RunFlag2&&RunFlag3)
pBut->EnableWindow(TRUE);
else
pBut->EnableWindow(FALSE);
}
void CKDlg::OnEnChangeEditOutput()
{
// TODO: 如果该控件是 RICHEDIT 控件,则它将不会
// 发送该通知,除非重写 CDialog::OnInitDialog()
// 函数并调用 CRichEditCtrl().SetEventMask(),
// 同时将 ENM_CHANGE 标志“或”运算到掩码中。
// TODO: 在此添加控件通知处理程序代码
// TODO: 在此添加控件通知处理程序代码
CString str;
CEdit *pEdit=(CEdit *)GetDlgItem(IDC_EDIT_OUTPUT);
pEdit->GetWindowText(str);
if(str!="")
RunFlag2=true;
else
RunFlag2=false;
CButton *pBut=(CButton *)GetDlgItem(IDC_BUTTON_RUN);
if(RunFlag1&&RunFlag2&&RunFlag3)
pBut->EnableWindow(TRUE);
else
pBut->EnableWindow(FALSE);
}
BOOL CKDlg::kAveClustering(float * data,int *&Result,int ClsNum,int DimNum,int SmpNum)
{
Result=new int[SmpNum];
float *ClsCenter=new float[ClsNum*DimNum]; //用来存放聚类中心的数据
float *ClsCenterOld=new float[ClsNum*DimNum]; //用来存放上一次的聚类中心
float *DstCenter=new float[ClsNum]; //用来存放两次聚类中心的距离
float *tmp1=new float[ClsNum*DimNum]; //临时变量
if(Result==NULL||ClsCenter==NULL||DstCenter==NULL||tmp1==NULL)
return FALSE;
int i,j,Cls;
float DstOld,Dst;
for(i=0;i<ClsNum*DimNum;i++)
{
ClsCenter[i]=data[i]; //选最初几个样本作为初始聚类中心
tmp1[i]=0; //初始化
}
for(i=0;i<ClsNum;i++) //初始化
DstCenter[i]=0;
do
{
for(i=0;i<SmpNum;i++)
{
Dst=Distance(&data[i*DimNum],ClsCenter,DimNum);
DstOld=Dst+1;
//--------计算每个样本到各个聚类中的距离,并把样本归为最小距离对应的类别---------//
for(j=0;j<ClsNum;j++)
{
Dst=Distance(&data[i*DimNum],&ClsCenter[j*DimNum],DimNum);
if(Dst<DstOld)
{
DstOld=Dst;
Result[i]=j;
DstCenter[j]++;
}
}
Cls=Result[i];
for(j=0;j<DimNum;j++)
tmp1[Cls*DimNum+j]=tmp1[Cls*DimNum+j]+data[i*DimNum+j];
}
//----------------保留旧的聚类中心,并计算新聚类中心----------------//
for(i=0;i<ClsNum;i++)
{
for(j=0;j<DimNum;j++)
{
ClsCenterOld[i*DimNum+j]=ClsCenter[i*DimNum+j];
ClsCenter[i*DimNum+j]=tmp1[i*DimNum+j]/DstCenter[i];
}
}
DstOld=0;
for(i=0;i<ClsNum;i++)
{
Dst=Distance(&ClsCenterOld[i*DimNum],&ClsCenter[i*DimNum],DimNum);
if(Dst>DstOld)
{
DstOld=Dst;
}
}
}while(DstOld>0.1);
delete []ClsCenter;
delete []ClsCenterOld;
delete []DstCenter;
delete []tmp1;
return TRUE;
}
float CKDlg::Distance(float *data1,float *data2,int num)
{
float dis=0;
int i;
for(i=0;i<num;i++)
dis=dis+(data1[i]-data2[i])*(data1[i]-data2[i]);
dis=sqrt(dis);
return dis;
}
BOOL CKDlg::ReadData(CString FileName,float *&data,int &SmpNum,int &DimNum)
{
ifstream InFile;
int m,n;
InFile.open(FileName,ios::in);
InFile>>m; //读入样本个数
InFile>>n; //读入样本维数
data=new float [m*n];
if(data==NULL)
return false;
int i;
for(i=0;i<m*n;i++)
InFile>>data[i];
SmpNum=m;
DimNum=n;
InFile.close();
return TRUE;
}
BOOL CKDlg::WriteData(CString FileName,int *data,int SmpNum,int DimNum)
{
ofstream OutFile;
OutFile.open(FileName,ios::out);
int i;
for(i=0;i<SmpNum;i++)
OutFile<<data[i]<<endl;
OutFile.close();
return TRUE;
}
void CKDlg::OnBnClickedButtonDraw()
{
// TODO: 在此添加控件通知处理程序代码
DLGSHOW DlgDraw;
DlgDraw.Pos=SmpData;
DlgDraw.Cls=Ans;
DlgDraw.ClsNum=clsnum;
DlgDraw.SmpNum=Smp;
DlgDraw.DoModal();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -