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

📄 rtr.cpp

📁 某个实验事编写粗糙集智能信息处理的程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		}
        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
	{
		InfTable = new int*[NumOfRecord];
	}
	catch(CMemoryException *e)
	{
		AfxMessageBox("超出内存(NO.1)!");
		e->Delete(); 
		return FALSE;
	}
	for(i = 0;i<NumOfRecord;i++)
	{
		
		try
		{
			InfTable[i] = new int[NumOfAttribute];
		}
        catch(CMemoryException *e)
		{
		    AfxMessageBox("超出内存(NO.2)!");
		    e->Delete(); 
			return FALSE;
		}
	}

	for(i=0;i<NumOfRecord;i++)
	{
		for(int j=0;j<NumOfAttribute;j++)
		{
			CString strTemp;
			int intTemp;
			fscanf(fp,"%s",strTemp);
			if((strcmp(strTemp,"*")==0) || (strcmp(strTemp,"-"))|| (strcmp(strTemp,"?")  == 0))
			{
                intTemp=-1;
			}
			else
			{
				intTemp=atoi(strTemp);
			}
			InfTable[i][j]=intTemp;
		}
	}
   try
   {
	   Marry=new int[NumOfRecord];
   }
   catch(CMemoryException *e)
   {
	   AfxMessageBox("超出内存(NO.2)!");
	   e->Delete(); 
	   return FALSE;
   }
   for(i=0;i<NumOfRecord;i++)
	   Marry[i]=-1;


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

	   for(j=0;j<NumOfRule;j++)
	   {
		   BOOL bl=TRUE;
		   int count1=0,count2=0;
		   for(int k=0;k<NumOfRuleAttr;k++)
		   {
			   if((TempArray.GetAt(k)!=-1) && (Rule[j][k]!=-1) && (Rule[j][k]!=TempArray.GetAt(k)))
			   {
				   bl=FALSE;
				   break;
			   }
               
               if((TempArray.GetAt(k)!=-1) && (Rule[j][k]!=-1) && (Rule[j][k]==TempArray.GetAt(k)))
				   count1++;

			   if((TempArray.GetAt(k)==-1) && (Rule[j][k]==-1))
				   count2++;
		   }

		   if(bl && (count2==NumOfRuleAttr || count1>0))
		   {
			   MarryRule.Add(j);
			   MarryNumber.Add(count2==NumOfRuleAttr?count2:count1);
		   }
	   }
	   if(MarryRule.GetSize()==0)
	   {
		   Marry[i]=-1;
		   continue;
	   }

	   if(MarryRule.GetSize()==1)
	   {
		   Marry[i]=MarryRule.GetAt(0);
		   continue;
	   }

	   int Max=0;
	   for(j=0;j<MarryNumber.GetSize();j++)
	   {
		   if(MarryNumber.GetAt(j)>Max)
		   {
			   Max=MarryNumber.GetAt(j);
		   }
	   }

	   CArray<int,int&> Same;
	   for(j=0;j<MarryNumber.GetSize();j++)
	   {
		   if(MarryNumber.GetAt(j)==Max)
			   Same.Add(j);
	   }

	   if(Same.GetSize()==1)
	   {
		   Marry[i]=MarryRule.GetAt(Same.GetAt(0));
		   continue;
	   }

	   double MaxConf=0;
	   int num;
	   for(j=0;j<Same.GetSize();j++)
	   {
		   if(RuleConf[MarryRule[Same.GetAt(j)]]>MaxConf)
		   {
			   MaxConf=RuleConf[MarryRule.GetAt(Same.GetAt(j))];
			   num=MarryRule.GetAt(Same.GetAt(j));
		   }
	   }

	   Marry[i]=num;
	   MarryRule.RemoveAll();
	   MarryNumber.RemoveAll();
	   TempArray.RemoveAll(); 
   }
   delete []FileStyle;
   fclose(fp);
   return TRUE;
}

void Rtr::Performance()
{ 

	if(!ComputerMissNum())
	{
		AfxMessageBox("计算遗失属性值失败,操作被中断!");
		return;
	}
	//计算整个决策表的近似分类精度
	int* tempA;
	tempA=new int[NumOfAttribute-1];
	for(int i=0;i<NumOfAttribute-1;i++)
		tempA[i]=i; 
	ComputerDecClass();
	if(!ComputerClassifyPre(tempA,NumOfAttribute-1,ExpClassifyPre))
	{
		AfxMessageBox("计算近似分类精度失败!");
		return;
	}
	delete []tempA;
	tempA=NULL;

    AttributeReduct();
	RecordCombine();
    DataStyle="rule";
    DataState=0;
   
	return ;

}

bool Rtr::WriteTable1(CString filesave)
{
 //   CFileDialog dlgSave(FALSE);
  /*  CString Savefile;
    if(dlgSave.DoModal()!=IDOK)
	{
	       delete []FileStyle;
	       return FALSE;
	}
       Savefile=dlgSave.GetPathName();
 */
   FILE* fp;
   if((fp = fopen(filesave,"w"))==NULL)
   {		
		AfxMessageBox("无法打开文件!");
		return FALSE; 
   }

   DataState=0;
   fprintf(fp,"Style:result\n"); 
   fprintf(fp,"Stage:%d\n",DataState);
   fprintf(fp,"Condition attributes number:%d\n",NumOfAttribute-1);
   fprintf(fp,"Tests number:%d\n",NumOfRecord);

   CString string="";
   for(int i=0;i<NumOfAttribute;i++)
   {
	   string+=NameOfAttribute[i];
	   string+="  ";
   }
   string+="识别值";
   string+="    所用规则";
   fprintf(fp,string);
   fprintf(fp,"\n");
   int Correct=0,Wrong=0,None=0;
   for(i=0;i<NumOfRecord;i++)
   {
	   string="";
	   for(int j=0;j<NumOfAttribute;j++)
	   {
		   CString temp="";
		   temp.Format("%d",InfTable[i][j]);
		   string+=temp;
		   string+="   ";
	   }
	   CString out="";
	   if(Marry[i]!=-1)
	     out.Format("%d",Rule[Marry[i]][NumOfRuleAttr]);
	   else
		 out.Format("%c",'*'); 
	   string+=out;
	   string+="   ";
	   out="";
	   out.Format("%d", Marry[i]);
	   string+=out;
	   string+="\n";
	   fprintf(fp,string);
	   if(Marry[i]==-1)
	   {
		   None++;
		   continue;
	   }
	   if(InfTable[i][NumOfAttribute-1]==Rule[Marry[i]][NumOfRuleAttr])
		   Correct++;
	   else
		   Wrong++;
   }
   fprintf(fp,"正确识别数:%d\n",Correct);
   fprintf(fp,"错误识别数:%d\n",Wrong);
   fprintf(fp,"未识别数:%d\n",None);

   fclose(fp);
   return TRUE;
  
}

BOOL Rtr::DisData(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(FileStyle,"input")==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-1];
	}
	catch(CMemoryException *e)
	{
		AfxMessageBox("超出内存(NO.24)!");
		e->Delete(); 
		return FALSE;
	}

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

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

	for(i=0;i<NumOfRecord;i++)
	{//每条决策
		for(int j=0;j<NumOfAttribute-1;j++)
		{// 读入每个条件属性
			CString strTemp;
			int intTemp;
			fscanf(fp,"%s",strTemp);
			if((strcmp(strTemp,"*")==0) || (strcmp(strTemp,"-"))
				|| (strcmp(strTemp,"?") == 0))
			{
                intTemp=-1;
			}
			else
			{
				intTemp=atoi(strTemp);
			}
			InfTable[i][j]=intTemp;
		}
	}
   try
   {
	   Marry=new int[NumOfRecord];
   }
   catch(CMemoryException *e)
   {
	   AfxMessageBox("超出内存(NO.2)!");
	   e->Delete(); 
	   return FALSE;
   }
   for(i=0;i<NumOfRecord;i++)
	   Marry[i]=-1;


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

	   for(j=0;j<NumOfRule;j++)
	   {
		   BOOL bl=TRUE;
		   int count1=0,count2=0;
		   for(int k=0;k<NumOfRuleAttr;k++)
		   {
			   if((TempArray.GetAt(k)!=-1) && (Rule[j][k]!=-1) && (Rule[j][k]!=TempArray.GetAt(k)))
			   {
				   bl=FALSE;
				   break;
			   }
               
               if((TempArray.GetAt(k)!=-1) && (Rule[j][k]!=-1) && (Rule[j][k]==TempArray.GetAt(k)))
				   count1++;

			   if((TempArray.GetAt(k)==-1) && (Rule[j][k]==-1))
				   count2++;
		   }

		   if(bl && (count2==NumOfRuleAttr || count1>0))
		   {
			   MarryRule.Add(j);
			   MarryNumber.Add(count2==NumOfRuleAttr?count2:count1);
		   }
	   }
	   if(MarryRule.GetSize()==0)
	   {
		   Marry[i]=-1;
		   continue;
	   }

	   if(MarryRule.GetSize()==1)
	   {
		   Marry[i]=MarryRule.GetAt(0);
		   continue;
	   }

	   int Max=0;
	   for(j=0;j<MarryNumber.GetSize();j++)
	   {
		   if(MarryNumber.GetAt(j)>Max)
		   {
			   Max=MarryNumber.GetAt(j);
		   }
	   }

	   CArray<int,int&> Same;
	   for(j=0;j<MarryNumber.GetSize();j++)
	   {
		   if(MarryNumber.GetAt(j)==Max)
			   Same.Add(j);
	   }

	   if(Same.GetSize()==1)
	   {
		   Marry[i]=MarryRule.GetAt(Same.GetAt(0));
		   continue;
	   }

	   double MaxConf=0;
	   int num;
	   for(j=0;j<Same.GetSize();j++)
	   {
		   if(RuleConf[MarryRule[Same.GetAt(j)]]>MaxConf)
		   {
			   MaxConf=RuleConf[MarryRule.GetAt(Same.GetAt(j))];
			   num=MarryRule.GetAt(Same.GetAt(j));
		   }
	   }

	   Marry[i]=num;
	   MarryRule.RemoveAll();
	   MarryNumber.RemoveAll();
	   TempArray.RemoveAll(); 
   }
   fclose(fp);
   delete []FileStyle;
   return TRUE;
}


bool Rtr::WriteTable2(CString filesave)
{//写入识别结果
	FILE* fp;
   if((fp = fopen(filesave,"w"))==NULL)
   {		
		AfxMessageBox("无法打开文件!");
		return FALSE; 
   }
   DataState=0;
   fprintf(fp,"Style:result\n"); 
   fprintf(fp,"Stage:%d\n",DataState);
   fprintf(fp,"Condition attributes number:%d\n",NumOfAttribute-1);
   fprintf(fp,"Tests number:%d\n",NumOfRecord);

   CString string="";
   for(int i=0;i<NumOfAttribute-1;i++)
   {
	   string+=NameOfAttribute[i];
	   string+="  ";
   }
   string+="识别值";
   string+="    所用规则";
   fprintf(fp,string);
   fprintf(fp,"\n");
   int Correct=0,Wrong=0,None=0;
   for(i=0;i<NumOfRecord;i++)
   {//每条记录
	   string="";
	   for(int j=0;j<NumOfAttribute-1;j++)
	   {//每个条件属性
		   CString temp="";
		   if(InfTable[i][j]!=-1)
		    temp.Format("%d",InfTable[i][j]);
		   else
			temp="-";
		   string+=temp;
		   string+="   ";
	   }
	   CString out="";
	   if(Marry[i]!=-1)//识别值
	     out.Format("%d",Rule[Marry[i]][NumOfRuleAttr]);
	   else
		 out.Format("%c",'-'); 
	   string+=out;
	   string+="   ";
	   out="";
	   out.Format("%d", Marry[i]);//匹配规则序号
	   string+=out;
	   string+="\n";
	   fprintf(fp,string);
	   if(Marry[i]==-1)
	   {
		   None++;
		   continue;
	   }
	   
   }
   fprintf(fp,"识别数:%d\n",NumOfRecord-None);
   fprintf(fp,"未识别数:%d\n",None);
   delete []Marry;
   fclose(fp);
}

⌨️ 快捷键说明

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