⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lietestdlg.cpp

📁 某个实验事编写粗糙集智能信息处理的程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:

#include "stdafx.h"
#include "RSet.h"
#include "LieTestDlg.h"
#include "fstream.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define MAX 20
/////////////////////////////////////////////////////////////////////////////
// CLieTestDlg dialog


CLieTestDlg::CLieTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLieTestDlg::IDD, pParent)
{
	pAttName=NULL;
	pDataType=NULL;
	pRuleTable=NULL;
	pBlockTable=NULL;
	pTestData=NULL;
	m_bRuleLoaded=false;
	m_nPerformType=0;
	str="";

	//{{AFX_DATA_INIT(CLieTestDlg)
	m_strResult = _T("");
	m_strInputFilePath = _T("");
	m_iInputByHand = 0;
	m_iFew = 0;
	m_strRule = _T("");
	m_table = _T("");
	//}}AFX_DATA_INIT
}

CLieTestDlg::~CLieTestDlg()
{
	int i;
	if(pTestData)
	{
		for(i=0;i<iAttNum;i++)
			delete[] pTestData[i];
		delete[] pTestData;
	}

	if(pAttName)
	{
		for(i=0;i<iAttNum+1;i++)
		{
			if(pAttName[i]) delete[]pAttName[i];
			
		}
		delete[] pAttName;
	}
	if(pDataType)
	{
		for(i=0;i<iAttNum+1;i++)
		{
			
			if(pDataType[i]) delete[]pDataType[i];
		}
		delete[] pDataType;
	}
	
		//释放pRuleTable
	if(pRuleTable != NULL)
	{
		for(int i = 0;i < iRuleNum;i++)
		{
			for(int j = 0;j < iAttNum+4;j++)
				delete[] pRuleTable[i][j];
		}//end for
		for(i = 0;i < iRuleNum;i++)
			delete[] pRuleTable[i];
		delete[] pRuleTable;
	}//end if

	//释放pBlockTable
	if(pBlockTable != NULL)
	{
		for(int i = 0;i < iBlockNum;i++)
		{
			for(int j = 0;j < iAttNum+1;j++)
				delete[] pBlockTable[i][j];
		}//end for
		for(i = 0;i < iBlockNum;i++)
			delete[] pBlockTable[i];
		delete[] pBlockTable;
	}//end if


}

void CLieTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLieTestDlg)
	DDX_Control(pDX, IDC_GRID, m_grid);
	DDX_Text(pDX, IDC_RESULT, m_strResult);
	DDX_Text(pDX, IDC_INPUT_FILEPATH, m_strInputFilePath);
	DDX_Radio(pDX, IDC_INPUTBYHAND, m_iInputByHand);
	DDX_Radio(pDX, IDC_FEW, m_iFew);
	DDX_Text(pDX, IDC_RULE_USED, m_strRule);
	DDX_Text(pDX, IDC_INPUT_FILEPATH2, m_table);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CLieTestDlg, CDialog)
	//{{AFX_MSG_MAP(CLieTestDlg)
	ON_BN_CLICKED(IDC_INPUTBYFILE, OnInputbyfile)
	ON_BN_CLICKED(IDC_TEST_DB, OnInputDBfile)
	ON_BN_CLICKED(IDC_INPUTBYHAND, OnInputbyhand)
	ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
	ON_BN_CLICKED(IDC_CONNECT_DB, OnConnectDb)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLieTestDlg message handlers

BOOL CLieTestDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

	if(m_bTest)
	{
		m_grid.SetCols(m_iAttrNum+1);
		SetWindowText("样本测试");
	}
	else
	{
		m_grid.SetCols(m_iAttrNum);
		SetWindowText("样本识别");
	}

	m_grid.SetRows(2);
	m_grid.SetRow(0);
	int k=0;
	for(int i=0;i<m_grid.GetCols();i++)
	{
		m_grid.SetCol(i);
		if(i==pReductedAttr[k])
		{
			m_grid.SetText("*");
			k++;
		}
		else
		m_grid.SetText(m_pstrAttrName[i-k]);
	}
	GetDlgItem(IDC_INPUT_FILEPATH)->EnableWindow(FALSE);
	GetDlgItem(IDC_BROWSE)->EnableWindow(FALSE);
	GetDlgItem(IDC_INPUT_FILEPATH2)->EnableWindow(FALSE);
	GetDlgItem(IDC_CONNECT_DB)->EnableWindow(FALSE);	
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CLieTestDlg::OnInputbyfile() 
{
	m_iInputByHand=1;
	GetDlgItem(IDC_GRID)->EnableWindow(FALSE);
	GetDlgItem(IDC_INPUT_FILEPATH)->EnableWindow(TRUE);
	GetDlgItem(IDC_BROWSE)->EnableWindow(TRUE);
	GetDlgItem(IDC_INPUT_FILEPATH2)->EnableWindow(FALSE);
	GetDlgItem(IDC_CONNECT_DB)->EnableWindow(FALSE);
}

void CLieTestDlg::OnInputDBfile() 
{
	m_iInputByHand=2;
	GetDlgItem(IDC_GRID)->EnableWindow(FALSE);
	GetDlgItem(IDC_INPUT_FILEPATH)->EnableWindow(FALSE);
	GetDlgItem(IDC_BROWSE)->EnableWindow(FALSE);
	GetDlgItem(IDC_INPUT_FILEPATH2)->EnableWindow(TRUE);
	GetDlgItem(IDC_CONNECT_DB)->EnableWindow(TRUE);
	UpdateData(true);
}

void CLieTestDlg::OnInputbyhand() 
{

	GetDlgItem(IDC_INPUT_FILEPATH)->EnableWindow(FALSE);
	GetDlgItem(IDC_BROWSE)->EnableWindow(FALSE);
	GetDlgItem(IDC_GRID)->EnableWindow(TRUE);
	GetDlgItem(IDC_INPUT_FILEPATH2)->EnableWindow(FALSE);
	GetDlgItem(IDC_CONNECT_DB)->EnableWindow(FALSE);	
}

void CLieTestDlg::OnBrowse()
{
	CFileDialog dlg(true,".txt",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
			"RS files(*.txt)|*.txt|all files(*.*)|*.*||",NULL);
	UpdateData(true);
	if(dlg.DoModal()==IDOK)
	{
		m_strInputFilePath=dlg.GetPathName();
		UpdateData(false);
	}
	m_strFilePath=m_strInputFilePath;
}

void CLieTestDlg::OnOK() 
{
	UpdateData(true);
	m_grid.OnUpdateGrid();
	UpdateData(TRUE);
	if(m_iInputByHand == 0)		//	手工输入。
	{    
		if(!m_bRuleLoaded)
			LoadRule(m_strRuleFile);
		if(!pTestData)	//若没分配内存,则分配
		{
			pTestData = new char*[iAttNum];
			if(!pTestData)	
			{
				MessageBox("out of memoey!\n");
				return ;
			}
			for(int i = 0;i < iAttNum;i++)
			{
				pTestData[i] = new char[MAX];
				if(!pTestData[i])
				{
					MessageBox("out of memoey!\n");
					return ;
				}
			}
		}
		//  fill  test data
		for(int i=0;i<iAttNum;i++)
		{
			strcpy(pTestData[i],m_grid.GetTextMatrix(1,i));
			if(strcmp(pAttName[i],"*")!=0)
				if(strcmp(m_grid.GetTextMatrix(1,i),"")==0)
				{
					m_strResult = " ";
		            m_strRule = " ";
					UpdateData(FALSE);
					AfxMessageBox("请完整输入要求的属性值");
				    return;
				} 
		}
		if(m_iFew == 0)
			SampleRecogniseFew();//少数优先
		else
			SampleRecogniseMany();//多数优先
		UpdateData(FALSE);		
	}
	else if (m_iInputByHand == 1)	//处理文本文件
	{
		if(! CheckInputFile(m_strInputFilePath))
		{ 
		  m_strInputFilePath.Empty();
		  UpdateData(false);
		  return;
		}
		CDialog::OnOK();
	}
	else //处理数据库文件
	{
		if(m_strFilePath=="")
		{
			return;
		}
		CDialog::OnOK();
	}
}

BOOL CLieTestDlg::LoadRule(LPCTSTR lpszRuleFile)
{//得到规则文件,存储
	if(m_bRuleLoaded) return true;
	//---------load rule-----------
	FILE* fp;
	if((fp = fopen(lpszRuleFile,"r")) == NULL)
	{
		::MessageBeep(MB_ICONHAND);
		AfxMessageBox("Couldn't open the file",MB_OK|MB_ICONSTOP);
		return FALSE;
	}//end if
	fscanf(fp,"Style:%s\n",cStyle);
	fscanf(fp,"Stage:%d\n",&iStage);
	fscanf(fp,"Condition attributes number:%d\n",&iAttNum);
    fscanf(fp,"The Number of Condition attributes deleted: %d\n",&iAttNumDeleted); 
	pAttDeleted=new int[iAttNumDeleted];
	if(pAttDeleted==0)
	{
		MessageBox("Out of Memory when read rule file");
		return false;
	}
	fscanf(fp,"The position of Condition attributes deleted:");
//    char p;
//	fscanf(fp,"%c",&p);
//	while(p!=':')
//		fscanf(fp," %c",&p);
	for(int x=0;x<iAttNumDeleted;x++)
		fscanf(fp," %d",&pAttDeleted[x]);
   fscanf(fp," Rules number:%d\n",&iRuleNum);
	fscanf(fp," Blocks number:%d\n",&iBlockNum);
	iAttNum=iAttNum+iAttNumDeleted;//属性数目为约简前的属性数目
	if(!SetAttName(fp,iAttNum+1))
		return FALSE;
	if(!SetDataType(fp,iAttNum+1))
		return FALSE;		
	SetRuleTable(fp,iAttNum+4,iRuleNum);//读取规则表 iAttNum+4为实际列数,包括信任度,覆盖样本数..
	char temp[MAX];
	if(fscanf(fp,"%s",temp) == -1)
		return TRUE;
	if(strcmp(temp,"[Blocks]") != 0)
		return TRUE;//有封锁事实
	if(iBlockNum != 0)
	{
		bBlock = TRUE;
		SetBlockTable(fp,iAttNum+1,iBlockNum);//读取封锁事实
	}
	iStringCol = iAttNum + 1;//列数为属性数加一

	m_bRuleLoaded=true;

	fclose(fp);
	return true;
}

BOOL CLieTestDlg::SetAttName(FILE* fp, int count)
{
	int i;
	if(pAttName == NULL)
	{
		try
		{
			pAttName = new char*[count];
		}
		catch(CMemoryException* e)
		{
			::MessageBeep(MB_ICONHAND);
			AfxMessageBox("Out of the memory!",MB_OK|MB_ICONSTOP);
			e->Delete();
			return FALSE;
		}

		for(i=0;i < count;i++)
		{
			try
			{
				pAttName[i]=new char[MAX];
			}
			catch(CMemoryException* e)
			{
				::MessageBeep(MB_ICONHAND);
				AfxMessageBox("Out of the memory!",MB_OK|MB_ICONSTOP);
				e->Delete();
				return FALSE;
			}
		}//end for
	}//end if

	int k=0;
	for(i=0;i < count;i++)
	{
		if(i==pReductedAttr[k])
		{
			strcpy(pAttName[i],"*");
			k++;
		}
		else
		fscanf(fp,"%s",pAttName[i]);
	}
	return TRUE;
}

BOOL CLieTestDlg::SetDataType(FILE *fp, int count)
{
	int i;
	if(pDataType == NULL)
	{
		try
		{
			pDataType = new char*[count];//count为列数
		}
		catch(CMemoryException* e)
		{
			::MessageBeep(MB_ICONHAND);
			AfxMessageBox("Out of the memory!",MB_OK|MB_ICONSTOP);
			e->Delete();
			return FALSE;
		}
		for(i = 0;i < count;i++)
		{
			try
			{
				pDataType[i]=new char[MAX];
			}
			catch(CMemoryException* e)
			{
	   			::MessageBeep(MB_ICONHAND);
	   			AfxMessageBox("Out of the memory!",MB_OK|MB_ICONSTOP);
				e->Delete();
	   			return FALSE;
			}
		}//end for
	}//end if

	int k=0;
	for(i=0;i < count;i++)
	{
		if(i==pReductedAttr[k])
		{
			strcpy(pDataType[i],"*");
			k++;
		}
		else
		fscanf(fp,"%s",pDataType[i]);
	}
	return TRUE;
}

BOOL CLieTestDlg::SetRuleTable(FILE *fp, int column, int row)
{
	int i,j;
	if(pRuleTable == NULL)
	{
		try
		{
			pRuleTable = new char**[row];
		}
		catch(CMemoryException* e)
		{
			::MessageBeep(MB_ICONHAND);
			AfxMessageBox("Out of memory!",MB_OK|MB_ICONSTOP);
			e->Delete();
			return FALSE;
		}
		for(i = 0;i < row;i++)
		{
			try
			{
				pRuleTable[i] = new char*[column];
			}
			catch(CMemoryException* e)
			{
				::MessageBeep(MB_ICONHAND);
				AfxMessageBox("Out of memory!",MB_OK|MB_ICONSTOP);
				e->Delete();
				return FALSE;
			}
			for(j = 0;j < column;j++)
			{
				try
				{
					pRuleTable[i][j] = new char[MAX];
				}
				catch(CMemoryException* e)
				{
					::MessageBeep(MB_ICONHAND);
					AfxMessageBox("Out of memory!",MB_OK|MB_ICONSTOP);
					e->Delete();
					return FALSE;
				}
				strcpy(pRuleTable[i][j]," ");
			}//end for
		}//end for
	}//end if
	
	for(i = 0;i < row;i++)
	{	
		int k=0;
		for(j=0;j < column;j++)
		{
			if(j==pReductedAttr[k])

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -