📄 skowron.cpp
字号:
// Skowron.cpp: implementation of the CSkowron class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "../RSet.h"
#include "Skowron.h"
#include <map>
#include "..\Alpha.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define MAX 100
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSkowron::CSkowron()
{
fAlpha = 0.0;
iClassNum = 0;
pSheet = NULL;
piConflict = NULL;
iClassNum = 0;
iMatrixLine = 0;
callnum = 0;
rulenum = 0;
m_pRules = NULL;
serial = 0;
block = 0;//*
pAttName=NULL;
pStringTable=NULL;
pDataType=NULL;
pIntTable=NULL;
abt=NULL;
bCuts = FALSE;
struCutRecord=NULL;
}
CSkowron::~CSkowron()
{
if(abt)
delete[] abt;
/*Object* pTemp;
while(pSheet != NULL)
{
delete[] pSheet->m_pRec;
pTemp = pSheet->p;
delete pSheet;
pSheet = pTemp;
}
delete[] piConflict;
for(int i = 0;i < iMatrixLine;i++)
{
for(int j = 0;j < iMatrixLine;j++)
delete[] pMatrix[i][j].m_p;
delete[] pMatrix[i];
}
delete[] pMatrix;*/
}
BOOL CSkowron::OpenFile(LPCTSTR lpszPathName)
{
FILE* fp;
if((fp = fopen(lpszPathName,"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);
if(iStage==3)
{
fscanf(fp,"The Number of Condition attributes deleted: %d\n",&abtcount);
fscanf(fp,"The position of Condition attributes deleted:");
if(abtcount!=0)
{
abt=new int[abtcount];
for(int n=0;n<abtcount;n++)
fscanf(fp," %d",&abt[n]);
}
fscanf(fp,"\n");
}
if(_stricmp(cStyle,"train") == 0)
{
fscanf(fp,"Records number:%d\n",&iRecordNum);
if(!SetAttName(fp,iAttNum+1))
return FALSE;
if(!SetDataType(fp,iAttNum+1))
return FALSE;
if(!SetStringTable(fp,iAttNum+1,iRecordNum))
return FALSE;
/* if(iStage == 0)
{
if(GetIncompletion())
bIncompletion = TRUE;
if(GetDiscrete())
bDiscrete = TRUE;
if(!bIncompletion && !bDiscrete)
SetIntegerTable(iAttNum+1,iRecordNum);
}
if(iStage == 1)
{
if(GetDiscrete())
bDiscrete = TRUE;
else
SetIntegerTable(iAttNum+1,iRecordNum);
}
if(iStage == 2)
{
SetIntegerTable(iAttNum+1,iRecordNum);
SetCutResult(fp);
ChangeString();
}*/
if(iStage == 3|| iStage==2 )
{
SetIntegerTable(iAttNum+1,iRecordNum);
if(SetCutResult(fp))
ChangeString();
}
iStringRow = iRecordNum;
iStringCol = iAttNum+1;
}//end if
else
{
AfxMessageBox("Line 1 of the file has error!",MB_OK|MB_ICONSTOP);
return FALSE;
}//end else
fclose(fp);
return TRUE;
}
BOOL CSkowron::SetStringTable(FILE* fp, int column, int row)
{//以字符串的形式读入决策表数据 failed:return false; success:return true
//row:记录数; colum:条件属性加决策属性个数
int i,j;
if(pStringTable == NULL)
{
try
{
pStringTable = 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
{
pStringTable[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
{
pStringTable[i][j] = new char[MAX];//最大字符串长度为max=100
}
catch(CMemoryException* e)
{
::MessageBeep(MB_ICONHAND);
AfxMessageBox("Out of memory!",MB_OK|MB_ICONSTOP);
e->Delete();
return FALSE;
}
// strcpy(pStringTable[i][j],"");//初始化为空
}//end for
}//end for
}//end if
for(i = 0;i < row;i++)
for(j = 0;j < column;j++)
fscanf(fp,"%s",pStringTable[i][j]);
return TRUE;
}
BOOL CSkowron::SetAttName(FILE* fp, int count)
{//设置属性名称 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
for(i=0;i < count;i++)
fscanf(fp,"%s",pAttName[i]);
return TRUE;
}
BOOL CSkowron::SetDataType(FILE *fp, int count)
{//count:属性数目+1
int i;
if(pDataType == NULL)
{
try
{
pDataType = 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
{
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
for(i = 0;i < count;i++)
fscanf(fp,"%s",pDataType[i]);
return TRUE;
}
BOOL CSkowron::SetIntegerTable(int column, int row)
{
int i,j;
if(pIntTable == NULL)
{
try
{
pIntTable = new int*[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
{
pIntTable[i] = new int[column];
}
catch(CMemoryException* e)
{
::MessageBeep(MB_ICONHAND);
AfxMessageBox("Out of memory!",MB_OK|MB_ICONSTOP);
e->Delete();
return FALSE;
}
}//end for
}//end if
for(i = 0;i < row;i++)
{
for(j = 0;j < column;j++)
{
if(strcmp(pStringTable[i][j],"-") == 0)
pIntTable[i][j] = -1;//可以处理遗失属性
else
pIntTable[i][j] = atoi(pStringTable[i][j]);
}//end for
}//end for
return TRUE;
}
BOOL CSkowron::SetCutResult(FILE* fp)
{
int n;
int iColumn;
int iCuts;
struct CutRecord* pHead = NULL;
struct CutRecord* pEnd = NULL;
char* temp = new char[MAX];
if(fscanf(fp,"%s",temp) == -1)
{
delete[] temp;
return FALSE;
}
if(strcmp(temp,"[Cuts]") != 0)
{
delete[] temp;
return FALSE;
}
bCuts = TRUE;
while(fscanf(fp,"%s\n",temp) != -1)
{
if(stricmp(temp,"attribute")==0)
{
fscanf(fp,"%d\n",&abtcount);
abt=new int[abtcount];
for(int i=0;i<abtcount;i++)
fscanf(fp,"%d\n",&abt[i]);
break;
}
else
{
iColumn=atoi(temp);
fscanf(fp,"%d",&iCuts);
try
{
pEnd = new CutRecord;
}
catch(CMemoryException* e)
{
::MessageBeep(MB_ICONHAND);
AfxMessageBox("Out of memory!",MB_OK|MB_ICONSTOP);
e->Delete();
return FALSE;
}
try
{
pEnd->cpCut = new char*[iCuts];
}
catch(CMemoryException* e)
{
::MessageBeep(MB_ICONHAND);
AfxMessageBox("Out of memory!",MB_OK|MB_ICONSTOP);
e->Delete();
return FALSE;
}
for(int i = 0;i < iCuts;i++)
{
try
{
pEnd->cpCut[i] = new char[MAX];
}
catch(CMemoryException* e)
{
::MessageBeep(MB_ICONHAND);
AfxMessageBox("Out of memory!",MB_OK|MB_ICONSTOP);
e->Delete();
return FALSE;
}
}//end for
pEnd->iColumn = iColumn;
pEnd->iCuts = iCuts;
pEnd->p = NULL;
for(i = 0;i < iCuts;i++)
{
fscanf(fp,"%s",temp);
fscanf(fp,"%d\n",&n);
strcpy(pEnd->cpCut[i],temp);
}//end for
if(pHead ==NULL)
{
struCutRecord = pEnd;
pHead =pEnd;
}//end if
else
{
pHead->p = pEnd;
pHead = pEnd;
pEnd = pEnd->p;
}//end else
}
}//end while
delete[] temp;
return TRUE;
}
void CSkowron::DeleteContents()
{
// TODO: Add your specialized code here and/or call the base class
//释放pStringTable
if(pStringTable != NULL)
{
for(int i = 0;i < iStringRow;i++)
{
for(int j = 0;j < iStringCol;j++)
delete[] pStringTable[i][j];
}//end for
for(i = 0;i < iStringRow;i++)
delete[] pStringTable[i];
delete[] pStringTable;
}//end if
//释放pIntTable
if(pIntTable != NULL)
{
for(int i = 0;i < iRecordNum;i++)
delete[] pIntTable[i];
delete[] pIntTable;
}//end if
//释放pAttName
if(pAttName != NULL)
{
for(int i = 0;i < iStringCol;i++)
delete[] pAttName[i];
delete[] pAttName;
}//end if
//释放pDataType
if(pDataType != NULL)
{
for(int i = 0;i < iAttNum+1;i++)
delete[] pDataType[i];
delete[] pDataType;
}
//释放struCutRecord
struct CutRecord* pHead;
while(struCutRecord != NULL)
{
pHead = struCutRecord->p;
for(int i = 0;i < struCutRecord->iCuts;i++)
delete[] struCutRecord->cpCut[i];
delete[] struCutRecord->cpCut;
delete struCutRecord;
struCutRecord = pHead;
}
}
void CSkowron::ChangeString()
{//输出规则前用断点代表的连续值替换离散值
int i,j;
struct CutRecord* pHead;
for(i = 0;i < iRecordNum;i++)
{
for(j = 0;j < iAttNum;j++)
{
if(strcmp(pStringTable[i][j],"-") == 0)
continue;
pHead = struCutRecord;
while(pHead != NULL)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -