📄 disone.cpp
字号:
delete[] matrix[i];
delete[] matrix;
for(i=0;i<cols;i++)
delete[] maxcols[i];
if(maxcols!=NULL)
delete[] maxcols;
get_cut(cuts);//得到最终的断点集
}
void CDisOne::get_cut(int *pos)
{//根据pos(cuts)数组中的断点所在列,得到最终的断点集,并存入cut数组中
int i,j; //循环变量
int cut_num; //每个属性的断点数
int * new_pos=NULL;
int num;
if((cut=new float *[iNonStrAttNum])==0)
{
AfxMessageBox("out of memory!",MB_OK|MB_ICONSTOP);
return;
}
for(i=0;i<iNonStrAttNum;i++)
{//每个属性i 例如:interval[0][0]=5,interval[1][0]=2 .断点列集:2,4,6;第一次循环时中判断了
//2,4;然后剩下了6;第一次循环结束时,intercal[0][0]=6-4=2;没有interval[1][0]
cut_num=0;
num=0;
cut[i]=NULL;
if((cut[i]=new float[(int)interval[i][0]+1])==0)
{//分配cut[i]内存
AfxMessageBox("out of memory!",MB_OK|MB_ICONSTOP);
return;
}
for(j=0;j<pos[0];j++)//对所有选择的列
if(pos[j+1]<interval[i][0])//每次只对不变的i
{//pos[j+1]对应的列号小于属性i的候选断点集个数???
cut_num++;
cut[i][cut_num]=interval[i][pos[j+1]+1];//?
}
cut[i][0]=(float)cut_num;
new_pos=NULL;
if((new_pos=new int[pos[0]-cut_num+1])==0)
{
AfxMessageBox("out of memory!",MB_OK|MB_ICONSTOP);
return;
}
new_pos[0]=pos[0]-cut_num;//得到剩余的断点数目
for(j=0;j<pos[0];j++)
if(pos[j+1]>=interval[i][0])
new_pos[++num]=pos[j+1]-(int)interval[i][0];//缩减列偏移量
delete []pos;
pos=NULL;
if((pos=new int[num+1])==0)
{
AfxMessageBox("out of memory!",MB_OK|MB_ICONSTOP);
return;
}
for(j=0;j<num+1;j++)
pos[j]=new_pos[j];//新的列号
delete []new_pos;
}//end for(i)
if(pos!=NULL)
delete []pos;
}
void CDisOne::make_new_table()
{//根据产生的断点集cut,得到离散化后的决策表NewTable
int i,j,k; //循环变量
int m;
selectsort(cut);//对数组中的值按从小到大的顺序进行排序
if((NewTable=new int *[iRecordNum])==0)
{
AfxMessageBox("out of memory!",MB_OK|MB_ICONSTOP);
return;
}
for(i=0;i<iRecordNum;i++)
{//样例i
NewTable[i]=NULL;
if((NewTable[i]=new int[iNonStrAttNum+1])==0)
{
AfxMessageBox("out of memory!",MB_OK|MB_ICONSTOP);
return;
}
for(j=0;j<iNonStrAttNum;j++)
{//属性j
m=0;
for(k=0;k<cut[j][0];k++)
{//对属性j的各个断点,比较判断
if(pNonStringTable[i][j]<cut[j][k+1])
{
NewTable[i][j]=m;
break;
}
m++;
if(pNonStringTable[i][j]>=cut[j][(int)cut[j][0]])
NewTable[i][j]=m;
}
if(cut[j][0]==0)
NewTable[i][j]=0;
}
NewTable[i][iNonStrAttNum]=(int)pNonStringTable[i][iNonStrAttNum];//决策
}
}
int CDisOne::search(float val, int condition)
{//返回属性值val在数组att_val的下标 condition为条件属性坐标
int i; //循环变量
int num;
for(i=0;i<att_val[condition][0];i++)
if(val==att_val[condition][i+1])
{
num=i+1;
break;
}
return num;
}
void CDisOne::OnDisOneSave(LPCTSTR lpszPathName)
{//保存离散化后的结果
int i,j;
fstream fpw;
fpw.open(lpszPathName,ios::out);
if(!fpw)
{
::MessageBeep(MB_ICONHAND);
AfxMessageBox("some error happen, file can't be opend!",
MB_OK|MB_ICONSTOP);
exit(0);
}
fpw<<"Style:"<<"train"<<endl;
fpw<<"Stage:2"<<endl;
fpw<<"Condition attributes number:"<<iAttNum<<endl;
fpw<<"Records number:"<<iRecordNum<<endl;
for(i = 0;i < iAttNum+1;i++)
fpw<<pAttName[i]<<" ";
fpw<<endl;
for(i = 0;i < iAttNum+1;i++)
fpw<<pDataType[i]<<" ";
fpw<<endl;
int strIndex=0,nonStrIndex=0;
for(i=0;i<iRecordNum;i++)
{
for(j=0;j< iAttNum;j++)
{
if(!strcmp(pDataType[j],"String"))
fpw<<pStringTableResult[i][strIndex++]<<" ";
else
fpw<<NewTable[i][nonStrIndex++]<<" ";
}
fpw<<NewTable[i][nonStrIndex];//决策属性
strIndex=0,nonStrIndex=0;
fpw<<endl;
}
fpw<<"[Cuts]"<<endl;//写断点
strIndex=0,nonStrIndex=0;
for(i=0;i<iAttNum;i++){
fpw<<i<<endl;
if(!strcmp(pDataType[i],"String")){
fpw<<strCuts[strIndex]<<endl; //断点个数
for(j=0;j<strCuts[strIndex];j++){
fpw<<pStrResult[j][strIndex]<<" "<<j<<endl;
//断点与离散值对应关系
}
strIndex++;
}
else{
fpw<<cut[nonStrIndex][0]+1<<endl; //断点个数
fpw<<"["<<"*"<<",";
for(j=0;j<(int)cut[nonStrIndex][0];j++){
fpw<<cut[nonStrIndex][j+1]<<")"<<" "<<j<<endl;
fpw<<"["<<cut[nonStrIndex][j+1]<<",";
}
fpw<<"*"<<"]"<<" "<<j;
fpw<<endl;
nonStrIndex++;
}
}
fpw.close();
}
BOOL CDisOne::ReadDataFromFile(char *filename)
{//读入文件
FILE *fp;
if((fp = fopen(filename,"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(_stricmp(cStyle,"train") == 0 && iStage<=1)
{
fscanf(fp,"Records number:%d\n",&iRecordNum);
if(!readAttrInfo(fp))
return FALSE;
if(!readTable(fp))
return FALSE;
/*
if(!SetAttName(fp,iAttNum+1))
return FALSE;
if(!SetDataType(fp,iAttNum+1))
return FALSE;
if(!SetStringTable(fp,iAttNum+1,iRecordNum))
return FALSE;
*/
}
return TRUE;
}
//将属性名称读入pAttName中,将数据类型读入pDataType中
BOOL CDisOne::readAttrInfo(FILE* fp){
int i;
if(pAttName == NULL){
try{
pAttName = new char*[iAttNum+1];
pDataType= new char*[iAttNum+1];
}
catch(CMemoryException* e){
::MessageBeep(MB_ICONHAND);
AfxMessageBox("Out of memory!",MB_OK|MB_ICONSTOP);
e->Delete();
return FALSE;
}
for(i=0;i <= iAttNum;i++){
try{
pAttName[i]=new char[MAX];
pDataType[i]=new char[MAX];
}
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 <= iAttNum;i++)
fscanf(fp,"%s",pAttName[i]);
fscanf(fp,"\n");
//读属性名
iStrAttNum = 0;
iNonStrAttNum = 0;
for(i = 0;i < iAttNum;i++){
fscanf(fp,"%s",pDataType[i]);
if(!strcmp(pDataType[i],"String"))
iStrAttNum++;
else
iNonStrAttNum++;
}//读属性值类型
fscanf(fp,"%s",pDataType[iAttNum]);
//决策属性类型
fscanf(fp,"\n");
return TRUE;
}
//将表中属性值读入 pNonStringTable和pStringTable中
BOOL CDisOne::readTable(FILE* fp){
int i,j;
if((pStringTable == NULL)&&(pNonStringTable == NULL)){
try
{
if(iStrAttNum!=0)
pStringTable = new char**[iRecordNum];
pNonStringTable = new float*[iRecordNum];
//至少决策属性是保存在pNonStringTable中的,所以pNonStringTable一
//定不为空
}
catch(CMemoryException* e){
::MessageBeep(MB_ICONHAND);
AfxMessageBox("Out of memory!",MB_OK|MB_ICONSTOP);
e->Delete();
return FALSE;
}
for(i = 0;i < iRecordNum;i++){
try{
if(iStrAttNum!=0)
pStringTable[i] = new char*[iStrAttNum];
pNonStringTable[i] = new float[iNonStrAttNum+1];//包括决策属性
}
catch(CMemoryException* e){
::MessageBeep(MB_ICONHAND);
AfxMessageBox("Out of memory!",MB_OK|MB_ICONSTOP);
e->Delete();
return FALSE;
}
for(j = 0;j < iStrAttNum;j++){
try{
pStringTable[i][j] = new char[MAX];
}
catch(CMemoryException* e){
::MessageBeep(MB_ICONHAND);
AfxMessageBox("Out of memory!",MB_OK|MB_ICONSTOP);
e->Delete();
return FALSE;
}
}//end for
for(j=0;j<=iNonStrAttNum;j++)
pNonStringTable[i][j]=0.0;
}//end for
}//end if
int iStrIndex=0,iNonStrIndex=0;
char* str=NULL;
if((str=new char[MAX])==0)
return FALSE;
for(i = 0;i < iRecordNum;i++){
for(j = 0;j < iAttNum;j++){
if(!strcmp(pDataType[j],"String"))
fscanf(fp,"%s",pStringTable[i][iStrIndex++]);
//字符串类型的属性值读入pStringTable
else{
fscanf(fp,"%s",str);
pNonStringTable[i][iNonStrIndex++]=(float)atof(str);
}//非字符串类型的属性值读入pNonStringTable
}
fscanf(fp,"%s",str);//读入决策属性的值
fscanf(fp,"\n");
pNonStringTable[i][iNonStrIndex]=(int)atof(str);
iStrIndex=0,iNonStrIndex=0;
}
delete[] str;
return TRUE;
}
void CDisOne::doString(){
//先建立一个pStrResult,通过对pStringTable的一次扫描,把每一个属性的所有可能值
//找出来,建立字符串值与离散值的映射关系
//再把pStringTable中的数据与pStrResult中的数据进行比较,修改pStringTable中的
//值,以便打印结果 //strCuts[]存放断点个数
int i=0,j=0,k=0;
try{
pStrResult = new char**[iRecordNum];
pStringTableResult = new int*[iRecordNum];
//存放离散化以后的结果
strCuts = new int[iStrAttNum];
//存放字符串属性的断点个数
}
catch(CMemoryException* e){
::MessageBeep(MB_ICONHAND);
AfxMessageBox("Out of memory!",MB_OK|MB_ICONSTOP);
e->Delete();
}
for (i=0;i<iRecordNum;i++){
try{
pStrResult[i]= new char*[iStrAttNum];
pStringTableResult[i]= new int[iStrAttNum];
}
catch(CMemoryException* e){
::MessageBeep(MB_ICONHAND);
AfxMessageBox("Out of memory!",MB_OK|MB_ICONSTOP);
e->Delete();
}
for(j=0;j<iStrAttNum;j++){
pStrResult[i][j] = new char[MAX];
strcpy(pStrResult[i][j],"");
}
}
for(i=0;i<iStrAttNum;i++){
for(j=0;j<iRecordNum;j++)
pStringTableResult[j][i]=0;
strCuts[i]=0;
}
for(i=0;i<iStrAttNum;i++){
for(j=0;j<iRecordNum;j++){//对每一列
for(k=0;k<=j; k++){
if(!strcmp(pStringTable[j][i],pStrResult[k][i]))
break;
else if(!strcmp(pStrResult[k][i],"")){
//如果pStrResult[k][i]不等于pStringTable[j][i]
//而且pStrResult[k][i]为空则应该把pStringTable[j][i]赋值给pStrResult[k][i]
strcpy(pStrResult[k][i],pStringTable[j][i]);
strCuts[i]++;//记录断点个数
break;
}
}
pStringTableResult[j][i]=k;
//把pStringTableResult[j][i]的值改为离散化以后的值
}
}
}
/*
BOOL CDisOne::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
for(i=0;i < count;i++)
fscanf(fp,"%s",pAttName[i]);
fscanf(fp,"\n");
return TRUE;
}
BOOL CDisOne::SetDataType(FILE *fp, int count)
{
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 CDisOne::SetStringTable(FILE* fp, int column, int row)
{
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];
}
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;
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -