📄 work5dlg.cpp
字号:
// work5Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "work5.h"
#include "work5Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CWork5Dlg dialog
CWork5Dlg::CWork5Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CWork5Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CWork5Dlg)
m_k = 0.5f;
m_Index = 1;
m_IntialTranslationFlag=0;
m_LineCount=0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CWork5Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWork5Dlg)
DDX_Text(pDX, IDC_EDIT1, m_k);
DDX_Text(pDX, IDC_EDIT2, m_Index);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CWork5Dlg, CDialog)
//{{AFX_MSG_MAP(CWork5Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
ON_EN_CHANGE(IDC_EDIT2, OnChangeEdit2)
ON_BN_CLICKED(IDC_COMPUTE, OnCompute)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWork5Dlg message handlers
BOOL CWork5Dlg::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);
}
}
// 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
if(m_IntialTranslationFlag == 0)
{
CheckRadioButton(IDC_RADIO1,IDC_RADIO2,IDC_RADIO1);
}
else
{
CheckRadioButton(IDC_RADIO1,IDC_RADIO2,IDC_RADIO2);
}
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CWork5Dlg::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 CWork5Dlg::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 CWork5Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CWork5Dlg::OnOK()
{
// TODO: Add extra validation here
CDialog::OnOK();
}
int CWork5Dlg::GetTheData(CString filename)
{
FILE *fp;
float * temvalue;
char c;
int m=0;
fp=fopen(filename,"r");
if (!fp) return -1;
do
{
temvalue=new float;
fscanf(fp,"%f",temvalue);
c=fgetc(fp);
m_Info.Add((CObject *)temvalue);
if (c=='\n'||c=='#') m++;
}while(c!='#');
fclose(fp);
return m;
}
void CWork5Dlg::InitialTheData(float * pData,int flag)
{
int i,j;
float n;
switch(flag) {
case 0:
for (i=0;i<m_LineCount;i++)
{
n=*(pData+i*m_RowCount+0);
for (j=0;j<m_RowCount;j++)
*(pData+i*m_RowCount+j)=*(pData+i*m_RowCount+j)/n;
}
break;
case 1:
for (i=0;i<m_LineCount;i++)
{
n=0;
for (j=0;j<m_RowCount;j++)
n=n+ *(pData+i*m_RowCount+j);
for (j=0;j<m_RowCount;j++)
*(pData+i*m_RowCount+j)=*(pData+i*m_RowCount+j)/n;
}
break;
default:
break;
}
}
void CWork5Dlg::OnRadio1()
{
// TODO: Add your control notification handler code here
m_IntialTranslationFlag=0;
}
void CWork5Dlg::OnRadio2()
{
// TODO: Add your control notification handler code here
m_IntialTranslationFlag=1;
}
void CWork5Dlg::OutPutData(float *pD,int flag)
{
FILE * fp;
int i,j;
CString s="";
s.Format("%d",m_Index);
CString filename="RDofNO"+s+".txt";
fp=fopen(filename,"a");
switch(flag) {
case 0:
fprintf(fp,"原始数据为:\n");
break;
case 1:
if (!m_IntialTranslationFlag)
fprintf(fp,"经过初值变换后数据为:\n");
else
fprintf(fp,"经过均值变换后数据为:\n");
break;
case 2:
fprintf(fp,"Derta数据为:\n");
break;
case 3:
fprintf(fp,"KeSai数据为:\n");
break;
default:
break;
}
for (i=0;i<m_LineCount;i++)
{
for (j=0;j<m_RowCount;j++)
fprintf(fp,"%.3f ",*(pD+i*m_RowCount+j));
fprintf(fp,"\n");
}
fclose(fp);
}
float * CWork5Dlg::PutDataToPData()
{
float * p=new float[m_Info.GetSize()];
for (int i=0;i<m_Info.GetSize();i++)
*(p+i)=*((float *)(m_Info.GetAt(i)));
return p;
}
void CWork5Dlg::DertaTrans(int index)
{
float * p=PutDataToPData();
InitialTheData(p,m_IntialTranslationFlag);
for (int i=0;i<m_LineCount;i++)
for (int j=0;j<m_RowCount;j++)
*(pData+i*m_RowCount+j)=*(pData+i*m_RowCount+j)-(*(p+index*m_RowCount+j));
AbsData(pData);
}
void CWork5Dlg::AbsData(float *p)
{
for (int i=0;i<m_Info.GetSize();i++)
if (*(p+i)<0) *(p+i)=-*(p+i);
}
float CWork5Dlg::FindMax(float *p)
{
float max;
for (int i=0;i<m_Info.GetSize();i++)
{
if (0==i)
{
max=*(p+i);
}
else
{
if (max<*(p+i))
{
max=*(p+i);
}
}
}
return max;
}
float CWork5Dlg::FindMin(float *p)
{
float min;
for (int i=0;i<m_Info.GetSize();i++)
{
if (0==i)
{
min=*(p+i);
}
else
{
if (min>*(p+i))
{
min=*(p+i);
}
}
}
return min;
}
void CWork5Dlg::KeSaiTrans(float *p,float dmax,float dmin,float k)
{
for (int i=0;i<m_Info.GetSize();i++)
{
*(p+i)=(dmin+k*dmax)/(*(p+i)+k*dmax);
}
}
float * CWork5Dlg::GetTheRelatedDegree(float *p)
{
float * r;
r=new float[m_LineCount];
float sum=0;
for (int i=0;i<m_LineCount;i++)
{
sum=0;
for (int j=0;j<m_RowCount;j++)
{
sum+=*(p+i*m_RowCount+j);
}
*(r+i)=sum/m_RowCount;
}
return r;
}
void CWork5Dlg::OutputResult(float *r, int count)
{
FILE * fp;
CString s="";
s.Format("%d",m_Index);
CString filename="RDofNO"+s+".txt";
fp=fopen(filename,"a");
if (m_IntialTranslationFlag)
fprintf(fp,"采用均值变换,灰度值=%.3f,本次计算第%d个要素的相关度为:\n",m_k,m_Index);
else
fprintf(fp,"采用初值变换,灰度值=%.3f,本次计算第%d个要素的相关度为:\n",m_k,m_Index);
for (int i=0;i<count;i++)
{
fprintf(fp,"%.3f ",*(r+i));
}
fprintf(fp,"\n");
fclose(fp);
}
void CWork5Dlg::OnChangeEdit1()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
UpdateData();
// TODO: Add your control notification handler code here
}
void CWork5Dlg::OnChangeEdit2()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
UpdateData();
// TODO: Add your control notification handler code here
}
void CWork5Dlg::OnCompute()
{
// TODO: Add your control notification handler code here
MessageBox("准备开始计算,请您确认数据无误。","灰色关联度计算");
m_Info.RemoveAll();
m_LineCount=GetTheData("dat.txt");
if (-1==m_LineCount)
{
MessageBox("读取dat文件有错误,关闭程序!","灰色关联度计算");
this->OnOK();
}
else
{
m_RowCount=m_Info.GetSize()/m_LineCount;
if (m_Info.GetSize()!=m_RowCount*m_LineCount)
{
MessageBox("数据输入有空缺!","灰色关联度计算");
}
else
{
if (m_Index-1<m_LineCount&&m_Index>0)
{
pData=PutDataToPData();
OutPutData(pData,0);
InitialTheData(pData,m_IntialTranslationFlag);
OutPutData(pData,1);
DertaTrans(m_Index-1);
OutPutData(pData,2);
float Dmax=FindMax(pData);
float Dmin=FindMin(pData);
KeSaiTrans(pData,Dmax,Dmin,m_k);
OutPutData(pData,3);
float * r=GetTheRelatedDegree(pData);
OutputResult(r,m_LineCount);
pData=PutDataToPData();
MessageBox("计算结束请查看输出文档。","灰色关联度计算");
}
else
{
MessageBox("输入的要素序数越界!","灰色关联度计算");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -