table.cpp
来自「某个实验事编写粗糙集智能信息处理的程序」· C++ 代码 · 共 493 行
CPP
493 行
// Table.cpp : implementation file
//
#include "stdafx.h"
#include "Table.h"
#include "fstream.h"
#ifndef MAX
#define MAX 30
#endif
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTable
CTable::CTable()
{
m_AttrName=NULL;
m_AttrType=NULL;
m_Record=NULL;
m_strValue=NULL;
m_nCount=0;
m_nConAttrNum=0;
m_bHaveCuts=false;
m_Cuts=NULL;
m_bStr=NULL;
m_bHaveStr=false;
m_bAttr=NULL;
m_bObj=NULL;
}
CTable::~CTable()
{
int i,j;
if(m_Record!=NULL)
{
for(i=0;i<m_nCount;i++)
delete []m_Record[i];
delete []m_Record;
}
if(m_AttrName!=NULL)
{
for(i=0;i<m_nConAttrNum+1;i++)
delete []m_AttrName[i];
delete []m_AttrName;
}
if(m_AttrType!=NULL)
{
for(i=0;i<m_nConAttrNum+1;i++)
delete []m_AttrType[i];
delete []m_AttrType;
}
if(m_strValue!=NULL)
{
for(i=0;i<m_nConAttrNum+1;i++)
{
if(m_bStr[i])
{
for(j=0;j<m_nCount;j++)
delete []m_strValue[i][j];
delete []m_strValue[i];
}
}
delete []m_strValue;
}
if(m_Cuts)
{
for(i=0;i<m_nConAttrNum+1;i++)
{
if(m_Cuts[i])
delete m_Cuts[i];
}
delete []m_Cuts;
}
if(m_bAttr)
delete []m_bAttr;
if(m_bObj)
delete []m_bObj;
if(m_bStr)
delete []m_bStr;
}
int CTable::GetConAttrNum()
{
return m_nConAttrNum;
}
int CTable::GetCount()
{
return m_nCount;
}
bool CTable::StrToInt(int col)
{
int i,j; //循环变量
int att_num=0; //统计字符串的种类
bool find=FALSE;
char ** val;
try
{
val=new char * [m_nCount]; //count为单元个数
}
catch(CMemoryException * e)
{
::MessageBeep(MB_ICONHAND);
AfxMessageBox("Out of the memory!",MB_OK|MB_ICONSTOP);
e->Delete();
return 0;
}
for(i=0;i<m_nCount;i++) //
{
try
{
val[i]=new char[MAX];//用来存放信息表每个属性值得中间信息表
}
catch(CMemoryException * e)
{
::MessageBeep(MB_ICONHAND);
AfxMessageBox("Out of the memory!",MB_OK|MB_ICONSTOP);
e->Delete();
return 0;
}
}
strcpy(val[0],m_strValue[col][0]);//全部拷贝
att_num++;//开始att_num=0 //赋为1
for(i=1;i<m_nCount;i++)
{ //和val中已有的数据比较
for(j=0;j<att_num;j++)
if(strcmp(m_strValue[col][i],val[j])==0)
{//第一次时肯定为真?
find=TRUE;
break;
}
if(find==FALSE)//此时把strValuepcol][1[赋予val[1]
strcpy(val[att_num++],m_strValue[col][i]);
find=FALSE;
}
for(i=0;i<m_nCount;i++)
for(j=0;j<att_num;j++)
if(strcmp(m_strValue[col][i],val[j])==0)
{
m_Record[i][col]=j;//(float)j;
break;
}
for(i=0;i<m_nCount;i++)
delete []val[i];
delete []val;
return TRUE;
}
bool CTable::InitTable(CString infotable)
{
int i,j; //循环变量
FILE * fp; //文件指针
fp=fopen(infotable,"r");
if(!fp)
{
::MessageBeep(MB_ICONHAND);
AfxMessageBox("Some error happen,File can't be opened!",
MB_OK|MB_ICONSTOP);
return false;
}
fscanf(fp,"Style: %s\n",m_strStyle); //
fscanf(fp,"Stage: %d\n",&m_nStage); //
if(stricmp(m_strStyle,"train")!=0)
{
if(AfxMessageBox("file input is not correct, are you sure to continue?",
MB_YESNO|MB_ICONINFORMATION|MB_DEFBUTTON2)==IDNO)
{ fclose(fp);
return false;
}
}
fscanf(fp,"Condition attributes number:%d\n",&m_nConAttrNum);
fscanf(fp,"Records number:%d\n",&m_nCount);
//-------allocate memory for m_bAttr and initialize-----------------
if((m_bAttr=new bool[m_nConAttrNum+1])==NULL ) //initialize m_bAttr
{
//memory error
AfxMessageBox("out of memory!");
fclose(fp);
return false;
}
for( i=0;i<m_nConAttrNum+1;i++)
m_bAttr[i]=true;
//----------allocate memory for m_bStr and initialize---------------
/*
m_bStr=new bool[m_nConAttrNum+1]; //initialize m_bStr
for(i=0;i<m_nConAttrNum+1;i++)
m_bStr[i]=false;
*/
//-----------allocate memory for m_bObj--------------
if((m_bObj=new bool[m_nCount])==NULL)
{
AfxMessageBox("out of memory!");//memory error
fclose(fp);
return false;
}
for( i=0;i<m_nCount;i++)
m_bObj[i]=true;
//---------------condition attribute name-------------
if((m_AttrName=new char* [m_nConAttrNum+1])==NULL)
{
//memory error
AfxMessageBox("out of memory!");
fclose(fp);
return false;
}
for(i=0;i<m_nConAttrNum+1;i++)
{
try
{
m_AttrName[i]=new char[MAX]; // 属性名称
}
catch(CMemoryException * e)
{
::MessageBeep(MB_ICONHAND);
AfxMessageBox("Out of the memory!",MB_OK|MB_ICONSTOP);
e->Delete();
fclose(fp);
return false;
}
fscanf(fp,"%s",m_AttrName[i]);
}
if((m_AttrType=new char * [m_nConAttrNum+1])==NULL)
{
//memory error
AfxMessageBox("out of memory!");
fclose(fp);
return false;
}
for(i=0;i<m_nConAttrNum+1;i++)
{
if((m_AttrType[i]=new char[10])==NULL)
{
//memory error
AfxMessageBox("out of memory!");
fclose(fp);
return false;
}
fscanf(fp,"%s",m_AttrType[i]);
}
/* m_strValue=new char ** [m_nConAttrNum+1]; //字符串类型的属性值
for(i=0;i<m_nConAttrNum+1;i++) //m_nCount为纪录数
{
if(!m_bStr[i])
m_strValue[i]=NULL;
else
{
try
{
m_strValue[i]=new char * [m_nCount];
}
catch(CMemoryException * e)
{
::MessageBeep(MB_ICONHAND);
AfxMessageBox("Out of the memory!",MB_OK|MB_ICONSTOP);
e->Delete();
return false;
}
for(j=0;j<m_nCount;j++)
{
try
{
m_strValue[i][j]=new char[MAX];
}
catch(CMemoryException * e)
{
::MessageBeep(MB_ICONHAND);
AfxMessageBox("Out of the memory!",MB_OK|MB_ICONSTOP);
e->Delete();
return false;
}
}
}
}
*/
/////////////////////alocate memory for m_Record
try
{
m_Record=new int* [m_nCount]; //create table(int)
}
catch(CMemoryException * e)
{
::MessageBeep(MB_ICONHAND);
AfxMessageBox("Out of the memory!",MB_OK|MB_ICONSTOP);
e->Delete();
fclose(fp);
return false;
}
for(i=0;i<m_nCount;i++)
{
try
{
m_Record[i]=new int[m_nConAttrNum+1];
}
catch(CMemoryException * e)
{
::MessageBeep(MB_ICONHAND);
AfxMessageBox("Out of the memory!",MB_OK|MB_ICONSTOP);
e->Delete();
fclose(fp);
return false;
}
}
///////////////////////read data from file input
CString str_temp;
for(i=0;i<m_nCount;i++)
{
for(j=0;j<m_nConAttrNum+1;j++)
{
fscanf(fp,"%s",str_temp);
m_Record[i][j]=atoi(str_temp);
}
}
// if(m_bHaveStr) //deal with string column
// for(i=0;i<m_nConAttrNum+1;i++)
// { if(m_bStr[i])
// StrToInt(i);
// }
//处理断点
char temp[20];
if(fscanf(fp,"%s\n",temp)==EOF)
{
TRACE("EOF");
}
else
{
if(stricmp(temp,"[Cuts]")==0)
{
m_bHaveCuts=true;
if((m_Cuts=new cuts* [m_nConAttrNum+1])==NULL)
{
//memory error
AfxMessageBox("out of memory!");
fclose(fp);
return false;
}
for(i=0;i<m_nConAttrNum+1;i++)
m_Cuts[i]=NULL; //初始化
int col=0; //该列有断点 第0例
int n; //有n个断点
int m; //离散化为m
char tp1[100];
fscanf(fp,"%d\n",&col);
do
{
fscanf(fp,"%d\n",&n);
m_Cuts[col]=new cuts(col,n);
TRACE("%d\n%d\n",col,n);
for(int k=0;k<n;k++)
{
fscanf(fp,"%s %d\n",tp1,&m);
m_Cuts[col]->data[k]=new char[strlen(tp1)+1];
strcpy(m_Cuts[col]->data[k],tp1);
m_Cuts[col]->m[k]=m;
TRACE("%s %d\n",tp1,m);
}
}
while(fscanf(fp,"%d\n",&col)!=EOF);
}
}
// else TRACE("no cuts\n");
fclose(fp);
return TRUE;
}
bool CTable::CompObj(int i,int j)//compare object i and j
{//仅仅比较条件属性,全部相等返回为true,否则为false
if(!m_bObj[j])//被删除
return false;
for (int k=0;k<m_nConAttrNum;k++)
{
if(!m_bAttr[k]) continue;//当条件属性为false时候,不比较
if(m_Record[i][k]!=m_Record[j][k])
return false;
}
return true;
}
void CTable::deleteDup()
{//删除重复记录
for(int i=0;i<m_nCount-1;i++)
{
if(!m_bObj[i]) continue;
for(int j=i+1;j<m_nCount;j++)
{//比较后面的记录,看是否有相同的条件属性和决策,则删除,
if( m_bObj[j] &&CompObj(i,j) && m_Record[i][m_nConAttrNum]==m_Record[j][m_nConAttrNum])
m_bObj[j]=false;
}
}
}
bool CTable::WriteFile(CString name)
{
TRACE(" Kylin CTable::writefile;\n");
// deleteDup();
int i;
// AfxMessageBox("completed!");
int nConRed=0;
for(i=0;i<m_nConAttrNum;i++)//计算约见后的条件属性个数
if(m_bAttr[i]) nConRed++;
// int nRecRed=0; //计算约简后的单元个数
// for(i=0;i<m_nCount;i++)
// if(m_bObj[i]) nRecRed++;
ofstream out(name,ios::out);
if(! out)
return false;
// out<<"Style: "<<m_strStyle<<"\n"; //写文件头
out<<"Style:train\n";
out<<"Stage:3\n";
out<<"Condition attributes number:";
out<<nConRed<<'\n';
out<<"The Number of Condition attributes deleted:";
out<<m_nConAttrNum-nConRed<<endl;
out<<"The position of Condition attributes deleted:";
if((m_nConAttrNum-nConRed)>0)
for(i=0;i<m_nConAttrNum;i++)
if(!m_bAttr[i]) out<<' '<<i;
out<<"\n";
out<<"Records number:";
// out<<nRecRed<<"\n";
out<<m_nCount<<"\n";
for(i=0;i<m_nConAttrNum;i++) //写属性名称
if(m_bAttr[i])
out<<m_AttrName[i]<<" ";
out<<m_AttrName[m_nConAttrNum]<<'\n';
for(i=0;i<m_nConAttrNum;i++) //写数据类型
if(m_bAttr[i])
out<<m_AttrType[i]<<" ";
out<<m_AttrType[m_nConAttrNum]<<'\n';
for(i=0;i<m_nCount;i++)
{
for(int j=0;j<m_nConAttrNum;j++)
{
if(m_bAttr[j]) //写条件属性
{
out<<m_Record[i][j]<<" ";
}
}
out<<m_Record[i][m_nConAttrNum];
out<<"\n";
}
//deal with cuts
// m_bHaveCuts=false;
if(m_bHaveCuts)
if(nConRed==0)//表明在约简属性集中不存在属性,故不需要写断点
m_bHaveCuts=false;
if(!m_bHaveCuts)
{
out.close();
return true;
}
out<<"[Cuts]"<<'\n';
int mmm=0;
for(i=0;i<m_nConAttrNum+1;i++)
{
if(!m_bAttr[i]){ mmm++; continue;}
if(!m_Cuts[i]) continue;
out<<m_Cuts[i]->col-mmm<<"\n"<<m_Cuts[i]->n<<"\n";
for(int j=0;j<m_Cuts[i]->n;j++)
out<<m_Cuts[i]->data[j]<<' '<<m_Cuts[i]->m[j]<<'\n';
}
out.close();
return true;
}
bool CTable::Perform(const char* filein,const char* fileout)
{
if(!InitTable(filein))
return false;
if(! reduct() )
return false;
if(!WriteFile(fileout))
return false;
return true;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?