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

📄 rtr.cpp

📁 某个实验事编写粗糙集智能信息处理的程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
   CArray<int,int&> *ConClass;
   try
   {
	   ConClass=new CArray<int,int&> [NumOfRecord];
   }
   catch(CMemoryException *e)
   {
        AfxMessageBox("超出内存(NO.8)!");
		e->Delete();
		return FALSE;
   }

   int* a;
   try
   {
       a=new int[NumOfAttribute-1];
   }
   catch(CMemoryException *e)
   {
		AfxMessageBox("超出内存(NO.16)!");
	    e->Delete();
	    return FALSE;
   }
   for(int i=0;i<NumOfSet;i++)
	   a[i]=Set[i];
   int Number=NumOfSet;
   //计算每条记录的限制容差类
   for(i=0;i<NumOfRecord;i++)
   {
	   ConClass[i].Add(i);
	   for(int j=0;j<NumOfRecord;j++)
	   {
		   if(j==i) continue;
           if(JudgeRelation(i,j,a,Number))
			   ConClass[i].Add(j);
	   }
   }

   int Precision=0;
   for(i=0;i<NumOfRecord;i++)
   {
	   for(int j=0;j<NumOfDecClass;j++)
	   {
		   if(Contain(ConClass[i],DecClass[j]))
		   {
			   Pos.Add(i); 
			   Precision++;
			   break;
		   }
	   }
   }
   //释放占用的内存
   for(i=0;i<NumOfRecord;i++)
	   ConClass[i].RemoveAll();
   delete []ConClass;
   ConClass=NULL;
   return TRUE;
}

BOOL Rtr::RecordCombine()
{
	try
	{
        RuleSupport=new int[NumOfRecord];
	}
    catch(CMemoryException *e)
    {
		AfxMessageBox("超出内存(NO.17)!");
	    e->Delete();
	    return FALSE;
    }
	
    try
	{
        RuleCover=new int[NumOfRecord];
	}
    catch(CMemoryException *e)
    {
		AfxMessageBox("超出内存(NO.18)!");
	    e->Delete();
	    return FALSE;
    }

	try
	{
        RuleCondition=new int[NumOfRecord];
	}
    catch(CMemoryException *e)
    {
		AfxMessageBox("超出内存(NO.19)!");
	    e->Delete();
	    return FALSE;
    }

    try
	{
		Rule = new int*[NumOfRecord];
	}
	catch(CMemoryException *e)
	{
		AfxMessageBox("超出内存(NO.20)!");
		e->Delete(); 
		return FALSE;
	}
	for(int i=0;i<NumOfRecord;i++)
	{
		
		try
		{
			Rule[i] = new int[NumOfReduct+1];
		}
        catch(CMemoryException *e)
		{
		    AfxMessageBox("超出内存(NO.21)!");
		    e->Delete(); 
			return FALSE;
		}
	}

	CArray<int,int&> arr;
	for(i=0;i<NumOfAttribute-1;i++)
		if(AttributeReductResult[i])
			arr.Add(i);
		
	for(i=0;i<NumOfRecord;i++)
	{
		for(int j=0;j<NumOfReduct;j++)
			Rule[i][j]=InfTable[i][arr.GetAt(j)];
		Rule[i][NumOfReduct]=InfTable[i][NumOfAttribute-1];
	}

   for(i=0;i<NumOfRecord;i++)
   {
	   RuleSupport[i]=1;
	   RuleCover[i]=1;
	   RuleCondition[i]=1;
   }

   //合并完全相同的记录
   for(i=0;i<NumOfRecord;i++)
   {
	   if(RuleCondition[i]==0)
		   continue;
	   for(int j=i+1;j<NumOfRecord;j++)
	   {
		   if(RuleCondition[j]==0)
			   continue;
		   if(Rule[i][NumOfReduct]!=Rule[j][NumOfReduct])
			   continue;
		   BOOL bl=TRUE;
		   for(int k=0;k<NumOfReduct;k++)
			   if(Rule[i][k]!=Rule[j][k])
			   {
				   bl=FALSE;
				   break;
			   }
		   if(bl) 
		   {
			   RuleSupport[i]+=RuleSupport[j];
			   RuleCover[i]+=RuleCover[j];
			   RuleCondition[j]=0;
		   }
	   }
   }

   //合并完全矛盾的记录
   for(i=0;i<NumOfRecord;i++)
   {
	   if(RuleCondition[i]==0) continue;
	   for(int j=i+1;j<NumOfRecord;j++)
	   {
		   if(RuleCondition[j]==0) continue;
           if(Rule[i][NumOfReduct]==Rule[j][NumOfReduct])
			   continue;
           BOOL bl=TRUE;
		   for(int k=0;k<NumOfReduct;k++)
			   if(Rule[i][k]!=Rule[j][k])
			   {
				   bl=FALSE;
				   break;
			   }
           if(bl)
		   {
			   if(RuleSupport[i]>=RuleSupport[j])
			   {
				   RuleCover[i]+=RuleCover[j];
				   RuleCondition[j]=0;
			   }
			   else
			   {
                   RuleCover[j]+=RuleCover[i];
				   RuleCondition[i]=0;
			   }
		   }
	   }
   }

   //合并满足限制容差关系且决策相同的记录
   int Number=NumOfReduct;
   int *a;
   try
   {
	   a=new int[NumOfReduct];
   }
   catch(CMemoryException* e)
   {
	   AfxMessageBox("超出内存(NO.22)!");
	   e->Delete(); 
		return FALSE;
   }
   for(i=0;i<NumOfReduct;i++)
	   a[i]=arr.GetAt(i);
   
   for(i=0;i<NumOfRecord;i++)
   {
	   if(RuleCondition[i]==0)
		   continue;
	   for(int j=i+1;j<NumOfRecord;j++)
	   {
		   if(RuleCondition[j]==0)
			   continue;
		   if(Rule[i][NumOfReduct]!=Rule[j][NumOfReduct])
			   continue;
		   if(JudgeRelation(i,j,a,Number))
		   {
			   int numi=0;
			   int numj=0;
			   for(int k=0;k<NumOfReduct;k++)
			   {
				   if(Rule[i][k]==-1)
					   numi++;
				   if(Rule[j][k]==-1)
					   numj++;
			   }
			   if(numi>=numj)
               {
				   RuleCover[i]+=RuleCover[j];
				   RuleSupport[i]+=RuleSupport[j];
				   RuleCondition[j]=0;
			   }
			   else
			   {
                   RuleCover[j]+=RuleCover[i];
				   RuleSupport[j]+=RuleSupport[i];
				   RuleCondition[i]=0;
			   }
		   }
	   }
   }
        
   NumOfRule=0;
   for(i=0;i<NumOfRecord;i++)
	   if(RuleCondition[i])
	   {
		   NumOfRule++;
		   CString string="";
		   for(int k=0;k<=NumOfReduct;k++)
		   {
			   CString str="";
			   str.Format("%d",Rule[i][k]);
			   if(str==_T("-1"))
				   str="*";
			   string+=str;
			   string+="  ";
		   }
		   //AfxMessageBox(string);
	   }
		   
	   
       
	  


  
   delete []a;
   return TRUE;
	
}

BOOL Rtr::WriteTable(CString filename)
{//写规则文件
    FILE* fp;
    if((fp = fopen(filename,"w"))==NULL)
	{		
		AfxMessageBox("无法打开文件!");
		return FALSE; 
	}

	fprintf(fp,"Style:%s\n",DataStyle);
	fprintf(fp,"Stage:%d\n",DataState);
	fprintf(fp,"Condition attributes number:%d\n",NumOfReduct);
	int tempnum=NumOfAttribute-NumOfReduct-1;


	fprintf(fp,"The Number of Condition attributes deleted:%d\n",tempnum);
	CString string="";
	if(tempnum!=0)
	{
	  for(int i=0;i<NumOfAttribute-1;i++)
		if(AttributeReductResult[i]==0)
		{
			CString temps="";
			temps.Format("%d",i);
			string+=temps;
			string+=" ";
		}
		fprintf(fp,"The Position of Condition attributes deleted:%s\n",string);
	}
	else
       fprintf(fp,"The Position of Condition attributes deleted:\n");

   fprintf(fp,"Rules number:%d\n",NumOfRule);
   fprintf(fp,"Blocks number: 0\n");
   string="";
   for(int i=0;i<NumOfAttribute-1;i++)
		if(AttributeReductResult[i]!=0)
		{
			CString temps="";
			temps.Format("%c",'A'+i);
			string+=temps;
			string+=" ";
		}
   CString temps="";
   temps.Format("%c",'A'+NumOfAttribute-1);
   string+=temps;
   fprintf(fp,string);
   fprintf(fp,"\n");
   string="";
   for(i=0;i<NumOfAttribute-1;i++)
		if(AttributeReductResult[i]!=0)
		{
			CString temps=TypeOfAttribute[i];
			string+=temps;
			string+="  ";
		}
   string+=TypeOfAttribute[NumOfAttribute-1];
   fprintf(fp,string);
   fprintf(fp,"\n");

   //写入规则集
   for(i=0;i<NumOfRecord;i++)
   {
	   if(!RuleCondition[i])
		   continue;
	   CString string="";
	   for(int j=0;j<NumOfReduct+1;j++)
	   {
		   CString temp;
		   if(Rule[i][j]==-1)
			   temp="-";
		   else
			   temp.Format("%d",Rule[i][j]);
		   string+=temp;
		   string+="   ";
	   }
	   CString temp;
	   double radio;
	   radio=(double)RuleSupport[i]/(double)RuleCover[i];
	   temp.Format("%f",radio);
	   string+=temp;
       string+="    ";
	   temp.Format("%d",RuleSupport[i]); 
	   string+=temp;
	   string+="    ";	   	
 	   temp.Format("%d",RuleCover[i]);
	   string+=temp;
	   string+="    "; 
 	   fprintf(fp,"%s\n",string);
   }
   fclose(fp);
   DeleteMemory();
   return TRUE;

}

BOOL Rtr::ReadRule(CString filename)
{
    FILE* fp;
    if((fp = fopen(filename,"r"))==NULL)
	{		
		AfxMessageBox("无法打开文件!");
		return FALSE; 
	}
    
	try
	{
		DataStyle=new char[MAX];
	}
    catch(CMemoryException *e)
	{
		AfxMessageBox("超出内存(NO.24)!");
		e->Delete(); 
		return FALSE;
	}
	fscanf(fp,"Style:%s\n",DataStyle);
	fscanf(fp,"Stage:%d\n",&DataState);


	if ((strcmp(DataStyle,"rule")!=0) && (DataState!=0))
	{
		AfxMessageBox("数据集类型不符!");
		return FALSE;
	}

	int temp;
	CString string;
	fscanf(fp,"Condition attributes number:%d\n",&NumOfAttribute);
	NumOfRuleAttr=NumOfAttribute;
	NumOfAttribute++;
    fscanf(fp,"The Number of Condition attributes deleted:%d\n",&temp);

	try
	{
		AttributeReductResult=new int[NumOfAttribute-1+temp];
	}
    catch(CMemoryException *e)
	{
		AfxMessageBox("超出内存(NO.24)!");
		e->Delete(); 
		return FALSE;
	}

	for(int i=0;i<NumOfAttribute+temp-1;i++)
		AttributeReductResult[i]=1;
    fscanf(fp,"The Position of Condition attributes deleted:");
    for(i=0;i<temp;i++)
	{
	
		fscanf(fp,"%s",DataStyle);
		int number=atoi(DataStyle);
		AttributeReductResult[number]=0;
	}
		
    fscanf(fp,"\n");
	fscanf(fp,"Rules number:%d\n",&NumOfRule);
	fscanf(fp,"Blocks number:%d\n",&temp);

	//读取属性名称
	try
	{
         NameOfAttribute=new char*[NumOfAttribute];
	}
	catch(CMemoryException *e)
	{
		AfxMessageBox("超出内存(NO.24)!");
		e->Delete(); 
		return FALSE;
	}

	for(i=0;i<NumOfAttribute;i++)
	{
		try
		{
			NameOfAttribute[i]=new char[MAX];
		}
        catch(CMemoryException *e)
		{
		    AfxMessageBox("超出内存(NO.25)!");
		    e->Delete(); 
		    return FALSE;
		}
	}

	for(i=0;i<NumOfAttribute;i++)
	{
		fscanf(fp,"%s",NameOfAttribute[i]);
	}
	fscanf(fp,"\n");
    
    //读取属性类型
    try
	{
         TypeOfAttribute=new char*[NumOfAttribute];
	}
	catch(CMemoryException *e)
	{
		AfxMessageBox("超出内存(NO.27)!");
		e->Delete(); 
		return FALSE;
	}
    
	for(i=0;i<NumOfAttribute;i++)
	{
		try
		{
			TypeOfAttribute[i]=new char[MAX];
		}
        catch(CMemoryException *e)
		{
		    AfxMessageBox("超出内存(NO.28)!");
		    e->Delete(); 
		    return FALSE;
		}
	}
    
	for(i=0;i<NumOfAttribute;i++)
	{
		fscanf(fp,"%s",TypeOfAttribute[i]);
	}
	fscanf(fp,"\n");  
    
	//读取决策表值
	try
	{
		Rule = new int*[NumOfRule];
	}
	catch(CMemoryException *e)
	{
		AfxMessageBox("超出内存(NO.1)!");
		e->Delete(); 
		return FALSE;
	}
	for(i = 0;i<NumOfRule;i++)
	{
		
		try
		{
			Rule[i] = new int[NumOfAttribute];
		}
        catch(CMemoryException *e)
		{
		    AfxMessageBox("超出内存(NO.2)!");
		    e->Delete(); 
			return FALSE;
		}
	}

	try
	{
		RuleConf=new double[NumOfRule];
	}
	catch(CMemoryException *e)
	{
	    AfxMessageBox("超出内存(NO.2)!");
		e->Delete(); 
		return FALSE;
	}

	for(i=0;i<NumOfRule;i++)
	{
		CString strTemp;
		for(int j=0;j<NumOfAttribute;j++)
		{
			
			int intTemp;
			fscanf(fp,"%s",strTemp);
			if((strcmp(strTemp,"*")==0) || (strcmp(strTemp,"-") )
				|| (strcmp(strTemp,"?") == 0))
			{
                intTemp=-1;
			}
			else
			{
				intTemp=atoi(strTemp);
			}
			Rule[i][j]=intTemp;
		}
		fscanf(fp,"%s",strTemp);
		RuleConf[i]=atof(strTemp);
    	fscanf(fp,"%s",strTemp);
		fscanf(fp,"%s",strTemp);
		fscanf(fp,"\n");
	}
   return TRUE;
}	

BOOL Rtr::TestData(CString filename)
{
	FILE* fp;
	char* FileStyle;
	int FileState;
    if((fp = fopen(filename,"r"))==NULL)
	{
		AfxMessageBox("无法打开文件!");
		return FALSE; 
	}    
	try
	{
		FileStyle=new char[MAX];
	}
    catch(CMemoryException *e)
	{
		AfxMessageBox("超出内存(NO.24)!");
		e->Delete(); 
		return FALSE;
	}
	fscanf(fp,"Style:%s\n",FileStyle);
	fscanf(fp,"Stage:%d\n",&FileState);
	if ((strcmp(DataStyle,"test")!=0) && (DataState!=0))
	{
		AfxMessageBox("数据集类型不符!");
		return FALSE;
	}
    fscanf(fp,"Condition attributes number:%d\n",&NumOfAttribute);
	NumOfAttribute++;
	fscanf(fp,"Tests number:%d\n",&NumOfRecord);
	//读取属性名称
	try
	{
         NameOfAttribute=new char*[NumOfAttribute];
	}
	catch(CMemoryException *e)
	{
		AfxMessageBox("超出内存(NO.24)!");
		e->Delete(); 
		return FALSE;
	}

	for(int i=0;i<NumOfAttribute;i++)
	{
		try
		{
			NameOfAttribute[i]=new char[MAX];

⌨️ 快捷键说明

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