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

📄 liecompletedel.cpp

📁 某个实验事编写粗糙集智能信息处理的程序
💻 CPP
字号:
// LieCompleteDel.cpp: implementation of the LieCompleteDel class.
//
//////////////////////////////////////////////////////////////////////
/*-----------------------
2001.12.9修改bug
◇	数据保存不完全.(writefile时,只写了部分数据. Kylin Li
-------------------------*/
#include <stdafx.h>
#include "RSet.h"
#include "LieCompleteDel.h"
#include <fstream.h>
#include <stdio.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CLieCompleteDel::CLieCompleteDel()
{
	iReserved=0;
	pStringTable=NULL;
	pAttrName=NULL;
	pDataType=NULL;
	bRedDel=NULL;
}

CLieCompleteDel::~CLieCompleteDel()
{
	int i,j;
	if(pStringTable)
	{
		for(i=0;i<iRecordNum;i++)
		{
			for(j=0;j<iAttNum+1;j++)
				if(pStringTable[i][j])
					delete[] pStringTable[i][j];
				if(pStringTable[i])
					delete[] pStringTable[i];
		}
		delete[] pStringTable;
	}
	if(pAttrName)
	{
		for(i=0;i<iAttNum+1;i++)
		{
			if(pAttrName[i]) delete[]pAttrName[i];
			
		}
		delete[] pAttrName;
	}
	if(pDataType)
	{
		for(i=0;i<iAttNum+1;i++)
		{
			
			if(pDataType[i]) delete[]pDataType[i];
		}
		delete[] pDataType;
	}
	
	if(bRedDel) delete[]bRedDel;
}

BOOL CLieCompleteDel::ReadFile(char *filename)
{
	int i;
	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);
	fscanf(fp,"Records number: %d\n",&iRecordNum);

	if(iAttNum<0 || iRecordNum<0 )
	{
		//error
		return false;
	}
	if( stricmp(cStyle,"train")!=0 && iStage==0)
	{
		::MessageBeep(MB_ICONHAND);
		AfxMessageBox("error!",MB_OK|MB_ICONSTOP);
		return false;
	}

//-----------allocate memroy for name and datatype------------------
	if( (pAttrName=new char*[iAttNum+1])==NULL)
	{
		::MessageBeep(MB_ICONHAND);
		AfxMessageBox("Out of memory!",MB_OK|MB_ICONSTOP);
		return FALSE;
	}
	for(i=0;i<iAttNum+1;i++)
	{
		if((pAttrName[i]=new char[LIEMAX])==NULL)
		{
			::MessageBeep(MB_ICONHAND);
			AfxMessageBox("Out of memory!",MB_OK|MB_ICONSTOP);
			return FALSE;
		}
	}

	if( (pDataType=new char*[iAttNum+1])==NULL)
	{
		::MessageBeep(MB_ICONHAND);
		AfxMessageBox("Out of memory!",MB_OK|MB_ICONSTOP);
		return FALSE;
	}
	for(i=0;i<iAttNum+1;i++)
	{
		if((pDataType[i]=new char[LIEMAX])==NULL)
		{
			::MessageBeep(MB_ICONHAND);
			AfxMessageBox("Out of memory!",MB_OK|MB_ICONSTOP);
			return FALSE;
		}
	}
//---------------read name and datatype---------------------------------
	for(i=0;i<iAttNum+1;i++)
		fscanf(fp,"%s",pAttrName[i]);
	fscanf(fp,"\n");
	
	for(i=0;i<iAttNum+1;i++)
		fscanf(fp,"%s",pDataType[i]);
	fscanf(fp,"\n");
	

//---------------allocate memory for pStringtable-------------------------

	if((pStringTable=new char** [iRecordNum])==NULL)
	{
		//error
		return false;
	}
	for(i=0;i<iRecordNum;i++)
	{
		if((pStringTable[i]=new char*[iAttNum+1])==NULL)
		{
			//error
			return false;
		}
		for(int j=0;j<iAttNum+1;j++)
		{
			if((pStringTable[i][j]=new char[LIEMAX])==NULL)
			{
				//error
				return false;
			}
		}
	}

	if((bRedDel=new BOOL[iRecordNum])==NULL)
	{
		//memory error
		return false;
	}
	for(i=0;i<iRecordNum;i++)
		bRedDel[i]=FALSE;
//--------------read data  ------------------------------------=-----------
	for(i=0;i<iRecordNum;i++)
		for(int j=0;j<iAttNum+1;j++)
		{
			fscanf(fp,"%s",pStringTable[i][j]);
			if(!bRedDel[i] && (stricmp(pStringTable[i][j],"-")==0 || stricmp(pStringTable[i][j],"?")==0 || stricmp(pStringTable[i][j],"*")==0))
			{
				bRedDel[i]=true;
				iReserved++;
			}
		}

	iReserved=iRecordNum-iReserved;
	TRACE("%d\n",iReserved);
	return true;
}

BOOL CLieCompleteDel::WriteFile (char* filename)
{
	fstream WriteFile;
	WriteFile.open(filename,ios::out);
	if(!WriteFile)
	{//  can not open  file
		return false;
	}
	WriteFile<<"Style: train"<<endl;
	WriteFile<<"Stage: 1"<<endl;
	WriteFile<<"Condition attributes number: ";
	WriteFile<<iAttNum<<endl;
	WriteFile<<"Records number: ";
	WriteFile<<iReserved<<endl;
	for(int i=0;i<iAttNum+1;i++)
		WriteFile<<pAttrName[i]<<' ';
	WriteFile<<endl;
	for(i=0;i<iAttNum+1;i++)
		WriteFile<<pDataType[i]<<' ';
	WriteFile<<endl;
	
////////////////begin perform /////////////////////
	for(i=0;i<iRecordNum;i++)
	{
		if(bRedDel[i])		//the row was deleted
			continue;
		for(int j=0;j<iAttNum+1;j++)
		{
			if(strcmp("Float",pDataType[j])==0)
				WriteFile<<(float)atoi(pStringTable[i][j])<<" ";
			else if(strcmp("Integer",pDataType[j])==0)
				WriteFile<<(int)atoi(pStringTable[i][j])<<" ";
			else WriteFile<<pStringTable[i][j]<<" ";
		 
			
		}
		WriteFile<<endl;
	}

return true;
}

⌨️ 快捷键说明

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