📄 可辨识矩阵生成器dlg.cpp
字号:
// 可辨识矩阵生成器Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "可辨识矩阵生成器.h"
#include "可辨识矩阵生成器Dlg.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()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog
CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyDlg)
m_sFilePath = _T("");
m_strOutputInEdit = _T("");
m_strComment = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_sOutFileName = _T("");
}
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDlg)
DDX_Text(pDX, IDC_EDIT_PATH, m_sFilePath);
DDX_Text(pDX, IDC_OUTPUT, m_strOutputInEdit);
DDX_Text(pDX, IDC_COMMENT, m_strComment);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
//{{AFX_MSG_MAP(CMyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers
BOOL CMyDlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
void CMyDlg::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 CMyDlg::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 CMyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
// construct the unclear matrix
/*
* assume the matrix like that:
*
* U a1 a2 a3 ... aN
* x1
* x2
* x3
* .
* .
* xM
*/
void CMyDlg::OnOK()
{
UpdateData(TRUE);
CStdioFile inFile;
if( !inFile.Open(m_sFilePath,CFile::modeRead | CFile::typeText)){
AfxMessageBox("Open file failure!");
return;
}else{
CStdioFile outFile;
CString sTemp;
CStringArray *pstrAttr;
int iCounterLine = 0;
int iCounterCharOfLine = 0;
int iCounterName = 0;
int iCharBeginPoint = 0;
// * name the out file(add "_out" in the end of the source file name)
m_sOutFileName = m_sFilePath;
int iPoint = m_sOutFileName.Find(".");
m_sOutFileName.Insert(iPoint,"_out");
// * create the out file in the same directory of the source file
outFile.Open(m_sOutFileName,CFile::modeCreate | CFile::modeWrite | CFile::typeText);
// * get the total line counter in the source file
while(inFile.ReadString(sTemp)){
iCounterLine ++;
}
// * read the data from the source file,
// * store the attributes in a string array,
// * get the count of the attributes' names
pstrAttr = new CStringArray[iCounterLine];
iCounterLine = 0;
inFile.SeekToBegin();
while(inFile.ReadString(sTemp)){
BOOL bCharBegin = FALSE;
iCounterCharOfLine = sTemp.GetLength();
for (int i = 0; i < iCounterCharOfLine; i ++){
if ((sTemp.GetAt(i) != ' ') && (sTemp.GetAt(i) != '\t')){
if (bCharBegin == FALSE) {
bCharBegin = TRUE;
iCharBeginPoint = i;// record the start point
}
}else{
if (bCharBegin == TRUE) {
bCharBegin = FALSE;
iCounterName ++;// record the count of names
pstrAttr[iCounterLine].Add(sTemp.Mid(iCharBeginPoint,i - iCharBeginPoint));
}
}
}// for(iCounterCharOfLine)
if (bCharBegin == TRUE) { // check the last name has been included
iCounterName ++;
pstrAttr[iCounterLine].Add(sTemp.Mid(iCharBeginPoint,i - iCharBeginPoint));
}
iCounterLine ++;
}// while(ReadString)
// * format header of the out file
CString strOut,strOut2; // strOut to output matrix, strOut2 to output a class of unclear members
strOut = "\t*****************信息系统不可分明矩阵*****************\n\n\n\t* ";
strOut += pstrAttr[0].GetAt(0); // domain name
strOut += " *\t";
for(int i = 1; i < iCounterLine; i ++){
strOut += pstrAttr[i].GetAt(0);
strOut += "\t\t\t";
}
strOut += "\n\t";
for(i = 0; i < 20 * iCounterLine; i ++){
strOut += "_";
}
strOut += "\n";
outFile.WriteString(strOut);
//m_strOutputInEdit += strOut; // for display
strOut2 = "\n\n\n不分明类( U|IND(C) ):\n\n\t";
// * come out the unclear matrix
int j,k,iCounterAttr;
CStringArray aClassMem;
CString strTemp2 = ""; // record the clear attribute names
CString strTemp3 = ""; // record the unclear class members' names
iCounterAttr = pstrAttr[0].GetSize();
for(i = 1; i < iCounterLine; i ++){ // the line 0 contains the attributes' names, so begin with line 1
strOut = "\t";
strOut += pstrAttr[i].GetAt(0);
strOut += "|\t";
for(j = 1; j < iCounterLine; j ++){
for(k = 1; k < iCounterAttr; k ++){
CString s1,s2,s3;
s1 = pstrAttr[i].GetAt(k);
s2 = pstrAttr[j].GetAt(k);
s3 = pstrAttr[0].GetAt(k);
if ( s1 != s2) {
if (strTemp2 != "") {
strTemp2 += ",";
}
strTemp2 += s3;
}
}//for(k)
if (strTemp2 == "") {
strTemp2 = "NULL";
// * record unclear class members
if (strTemp3 != "") {
strTemp3 += ",";
}
strTemp3 += pstrAttr[j].GetAt(0);
}
strOut += strTemp2;
// * caculate the number of '\t'
int len = strTemp2.GetLength()/8;
for( ; len < 3; len ++){
strOut += "\t";
}
strTemp2 = "";
}// for(j)
strOut += "\n";
//m_strOutputInEdit += strOut; // for display
outFile.WriteString(strOut); // write a line
bool bFind = false;
for(int t = 0; t < aClassMem.GetSize(); t++){
if (aClassMem.GetAt(t) == strTemp3) {
bFind = true;
}
}
if (!bFind) {
aClassMem.Add(strTemp3);
}
strTemp3 = "";
}//for(i)
for(i = 0; i < aClassMem.GetSize(); i ++){
strOut2 += aClassMem.GetAt(i);
strOut2 += "\n\t";
}
//m_strOutputInEdit += strOut2; // for display
outFile.WriteString(strOut2);
for(i = 0; i < iCounterLine; i ++){
pstrAttr[i].RemoveAll();
}
//delete [] pstrAttr;
outFile.Close();
}// if(open file)
inFile.Close();
// * display matrix in edit control
DisplayInEdit();
// * display comments
m_strComment = "不可辨识矩阵保存在文件:\r\n";
m_strComment += m_sOutFileName;
UpdateData(FALSE);
}
void CMyDlg::OnBrowse()
{
// Select the file contains the data source matrix
CFileDialog dlg(TRUE,NULL,"d:\\*.txt",0,"TXT Files (*.txt)|*.txt||",this); // set the first parameter to TRUE to consturct a "OPEN" dialog, or "FALSE" to construct a "SAVE AS" dialog
if(dlg.DoModal() == IDOK)
{
// display in the edit control
m_sFilePath = dlg.GetPathName();
UpdateData(false);
}
// display the source text in edit control
DisplayInEdit2();
}
void CMyDlg::DisplayInEdit()
{
CStdioFile pFile;
CString sText = "";
CString s;
if( !pFile.Open(m_sOutFileName,CFile::modeRead | CFile::typeText)){
AfxMessageBox("Open file failure!");
return;
}else{
while (pFile.ReadString(s)) {
sText += s;
sText += "\r\n";
}
}
m_strOutputInEdit += "\r\n\r\n";
m_strOutputInEdit += sText;
UpdateData(FALSE);
pFile.Close();
}
void CMyDlg::DisplayInEdit2()
{
CStdioFile inFile;
CString sText; //= "\t*****************原始矩阵*****************\r\n\r\n\r\n";
CString s;
char c;
if( !inFile.Open(m_sFilePath,CFile::modeRead | CFile::typeText)){
AfxMessageBox("Open file failure!");
return;
}else{
while (inFile.ReadString(s)) {
for(int i = 0; i < s.GetLength();i ++) {
c = s.GetAt(i);
}
sText += "\t";
sText += s;
sText += "\r\n";
}
}
m_strOutputInEdit = sText;
UpdateData(FALSE);
inFile.Close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -