yyzsamplerecognise1.cpp

来自「某个实验事编写粗糙集智能信息处理的程序」· C++ 代码 · 共 1,917 行 · 第 1/3 页

CPP
1,917
字号
	if(iFloat != 0 || iChar != 0)
		return 1;
	return 0;
}

BOOL CLieSampleRecogniseYyz1::SetIntegerTable(int column, int row)
{
		TRACE("SetInitTable\n");
AfxMessageBox("GHJGHJLGH'JH");
	int i,j;
	if(pIntTable == NULL)
	{
		try
		{
			pIntTable = new int*[row];
		}
		catch(CMemoryException* e)
		{
			::MessageBeep(MB_ICONHAND);
			AfxMessageBox("Out of memory2!",MB_OK|MB_ICONSTOP);
			e->Delete();
			return FALSE;
		}
		for(i = 0;i < row;i++)
		{
			try
			{
				pIntTable[i] = new int[column];
			}
			catch(CMemoryException* e)
			{
				::MessageBeep(MB_ICONHAND);
				AfxMessageBox("Out of memory3!",MB_OK|MB_ICONSTOP);
				e->Delete();
				return FALSE;
			}
		}//end for
	}//end if
	for(i = 0;i < row;i++)
	{
		for(j = 0;j < column;j++)
		{
			if(strcmp(pStringTable[i][j],"-") == 0)
				pIntTable[i][j] = -1;
			else
				pIntTable[i][j] = atoi(pStringTable[i][j]);
		}//end for
	}//end for
	return TRUE;
}

BOOL CLieSampleRecogniseYyz1::SetCutResult(FILE* fp)
{
	TRACE("Setcutresult\n");

	int n;
	int iColumn;
	int iCuts;
	struct CutRecord* pHead = NULL;
	struct CutRecord* pEnd = NULL;
	
	char* temp = new char[MAX];
	if(fscanf(fp,"%s",temp) == -1)
	{
		delete[] temp;
		return FALSE;
	}
	if(strcmp(temp,"[Cuts]") != 0)
	{
		delete[] temp;
		return FALSE;
	}

	bCuts = TRUE;
	while(fscanf(fp,"%d",&iColumn) != -1)
	{
		fscanf(fp,"%d",&iCuts);
		try
		{
			pEnd = new CutRecord;
		}
		catch(CMemoryException* e)
		{
			::MessageBeep(MB_ICONHAND);
			AfxMessageBox("Out of memory4!",MB_OK|MB_ICONSTOP);
			e->Delete();
			return FALSE;
		}
		try
		{
			pEnd->cpCut = new char*[iCuts];
		}
		catch(CMemoryException* e)
		{
			::MessageBeep(MB_ICONHAND);
			AfxMessageBox("Out of memory5!",MB_OK|MB_ICONSTOP);
			e->Delete();
			return FALSE;
		}
		for(int i = 0;i < iCuts;i++)
		{
			try
			{
				pEnd->cpCut[i] = new char[MAX];
			}
			catch(CMemoryException* e)
			{
				::MessageBeep(MB_ICONHAND);
				AfxMessageBox("Out of memory6!",MB_OK|MB_ICONSTOP);
				e->Delete();
				return FALSE;
			}
		}//end for
		pEnd->iColumn = iColumn;
		pEnd->iCuts = iCuts;
		pEnd->p = NULL;
		for(i = 0;i < iCuts;i++)
		{
			fscanf(fp,"%s",temp);
			fscanf(fp,"%d",&n);
			strcpy(pEnd->cpCut[n],temp);
		}//end for
		if(pHead == NULL)
		{
			struCutRecord = pEnd;
			pHead =pEnd;
		}//end if
		else
		{
			pHead->p = pEnd;
			pHead = pEnd;
			pEnd = pEnd->p;
		}//end else
	}//end while

	delete[] temp;
	return TRUE;
}



BOOL CLieSampleRecogniseYyz1::SetRuleTable1(FILE *fp, int column, int row,int *atb,int atbcount)
{
	TRACE("SetRuleTable\n");

	int i,j,k=0;
	if(pRuleTable == NULL)
	{
		try
		{
			pRuleTable = new char**[row];
		}
		catch(CMemoryException* e)
		{
			::MessageBeep(MB_ICONHAND);
			AfxMessageBox("Out of memory6!",MB_OK|MB_ICONSTOP);
			e->Delete();
			return FALSE;
		}
		for(i = 0;i < row;i++)
		{
			try
			{
				pRuleTable[i] = new char*[column];
			}
			catch(CMemoryException* e)
			{
				::MessageBeep(MB_ICONHAND);
				AfxMessageBox("7!",MB_OK|MB_ICONSTOP);
				e->Delete();
				return FALSE;
			}
			for(j = 0;j < column;j++)
			{
				try
				{
					pRuleTable[i][j] = new char[MAX];
				}
				catch(CMemoryException* e)
				{
					::MessageBeep(MB_ICONHAND);
					AfxMessageBox("Out of memory7!",MB_OK|MB_ICONSTOP);
					e->Delete();
					return FALSE;
				}
				strcpy(pRuleTable[i][j]," ");
			}//end for
		}//end for
	}//end if
	///先读列再读行
//	ofstream outout;
//	outout.open("d:\\output.txt",ios::out);
for(i=0;i<row;i++)
{
	
   for(j = 0;j<column;j++)
   {
		k=0;
		for(int x=0;x<atbcount;x++)
		{
			if(j!=atb[x])
				k++;
		} 
		if(k==atbcount)
             fscanf(fp,"%s",pRuleTable[i][j]);
		else
			strcpy(pRuleTable[i][j],"-");
		//	outout<<pRuleTable[i][j]<<' ';
   }
		//outout<<endl;
}
//outout.close();
	return TRUE;
}

BOOL CLieSampleRecogniseYyz1::SetBlockTable1(FILE *fp, int column, int row,int *atb,int atbcount)
{
	TRACE("SetBlockTable\n");
	int i,j,k=0;
	if(pBlockTable == NULL)
	{
		try
		{
			pBlockTable = new char**[row];
		}
		catch(CMemoryException* e)
		{
			::MessageBeep(MB_ICONHAND);
			AfxMessageBox("Out of memory9!",MB_OK|MB_ICONSTOP);
			e->Delete();
			return FALSE;
		}
		for(i = 0;i < row;i++)
		{
			try
			{
				pBlockTable[i] = new char*[column];
			}
			catch(CMemoryException* e)
			{
				::MessageBeep(MB_ICONHAND);
				AfxMessageBox("Out of memory10!",MB_OK|MB_ICONSTOP);
				e->Delete();
				return FALSE;
			}
			for(j = 0;j < column;j++)
			{
				try
				{
					pBlockTable[i][j] = new char[MAX];
				}
				catch(CMemoryException* e)
				{
					::MessageBeep(MB_ICONHAND);
					AfxMessageBox("Out of memory11!",MB_OK|MB_ICONSTOP);
					e->Delete();
					return FALSE;
				}
				strcpy(pBlockTable[i][j]," ");
			}//end for
		}//end for
	}//end if
for(i=0;i<row;i++)
{
	   
	for(int j=0;j<column;j++)
	{ 
		k=0;
		for(int x=0;x<atbcount;x++)
		{
			if(j!=atb[x])
				k++;
		} 
		if(k==atbcount)
             fscanf(fp,"%s",pBlockTable[i][j]);
		else
			strcpy(pBlockTable[i][j],"-");
	}

}	



/*	for(i = 0;i < row;i++)
		for(j = 0;j < column;j++)
			fscanf(fp,"%s",pBlockTable[i][j]);*/
	return TRUE;
}


void CLieSampleRecogniseYyz1::SampleRecogniseFew()
{
	TRACE("sample recognise few\n");
	int i,j,k;
	int* temp;
	int temp1 = -1;
	float reliability;			//reliability
	int totalnum;
	int coveragenum;			//cover?
	int n = 0;
	int pos;
	char* d;

	pRecogniseResultTable = new char**[iTestNum];	//result table
	for(i = 0;i < iTestNum;i++)
		pRecogniseResultTable[i] = new char*[2];
	for(i = 0;i < iTestNum;i++)
	{
		pRecogniseResultTable[i][0] = new char[MAX];
		pRecogniseResultTable[i][1] = new char[MAX];
	}

	d = new char[MAX];
	
	temp = new int[iRuleNum];

	CString tempstring;
	CString trimstring;
	CString compstring;
		
	for(i = 0;i < iTestNum;i++)
	{
		int MaxMatch=0;
		n = 0;
		for(j = 0;j < iRuleNum;j++)
			temp[j] = 0;					//initialize temp[] to zero
		for(j = 0;j < iRuleNum;j++)
		{
			//------added by li e ,2001.6.27---------------
			if ( IsRuleAttAllNull(j) )
			{
				if( IsTestAttAllNull(i) )
				{
					for(int x=0;x<j;x++)
						temp[x]=0;
					temp[j]=1;
					temp1=j;
					n=1;
					// 此处continue已经没有意义
					//因为规则集中已经不再有全为空的情况.
					//而其余的规则肯定和该测试(识别)数据不匹配
				}
				break;
			}

			///  end li e     2001.6.27------------
			for(k = 0;k < iAttNum;k++)
			{
				if(strcmp(pRuleTable[j][k],"-") == 0 ||
					pStringTable[i][k][0]=='-')
				{
					temp[j]++;
					continue;
				}
				//规则为‘-’,继续该循环
				tempstring = pRuleTable[j][k];
				if(tempstring[0] == '[')
				{
					pos = tempstring.Find(',');
					trimstring = tempstring.Left(pos);
					trimstring.Remove('[');
					if(strcmp(trimstring,"*") != 0)
					{
						if(atof(trimstring) > atof(pStringTable[i][k]))
						{
							temp[j]=0;
							break;		//l比下限小,退出该循环
						}
					}//end if
					trimstring = tempstring;
					trimstring.Delete(0,pos+1);
					trimstring.Remove(')');
					if(strcmp(trimstring,"*") != 0)
						if(atof(trimstring) <= atof(pStringTable[i][k]))
						{
							temp[j]=0;
							break;	//比上限大,退出该循环
						}

					//应该加 continue
					temp[j]+=iAttNum+1;
					continue;				
				}//end if
				//上端比较范围情况[XXX,XXX),如果在这个范围,go on,否则跳出
				else if(tempstring[0] == '{')
				{
					int m = 0;
					trimstring = tempstring;
					trimstring.Delete(0,1);
					pos = trimstring.Find(',');
					while(pos != -1)
					{
						compstring = trimstring.Left(pos);
						trimstring.Delete(0,pos+1);
						if(strcmp(compstring,pStringTable[i][k]) == 0)
						{
							m =1;
							temp[j]=0;
							break;
						}//end if
						pos = trimstring.Find(',');
					}//end while
					if(m == 0)
					{
						trimstring.Remove('}');
						if(strcmp(trimstring,pStringTable[i][k]) != 0)
						{
							temp[j]=0;
							break;
						}
					}//end if
					temp[j]+=iAttNum+1;
					continue;			//added by li e
				}//end if
				//上段比较是否在一个字符串组中,如果不在跳出,如果在,继续
				else
				{
					if(atoi(pRuleTable[j][k]) != atoi(pStringTable[i][k]))
					{
						temp[j]=0;
						break;
					}
					else
					{
						temp[j]+=iAttNum+1;
						continue;
					}
				}//end else
				//上段为整数情况,如果不等,跳出。如果在,go on
			}//end for
					
			if(temp[j]>0)
				n++;
			if(temp[j]> MaxMatch)
			{
				MaxMatch=temp[j];
				temp1=j;
			}
		}//end for
		if(n>0)
		{
			for(int x=0;x<iRuleNum;x++)
			{
				if(temp[x]!=0 && MaxMatch>0 &&  temp[x]<MaxMatch )
				{
					temp[x]=0;//考虑最长匹配
					n--;
				}
				
				if(temp[x]!=0)
					temp[x]=1;
				
			}
		}

		if(n != 0 && bBlock)		//存在匹配的规则,而且有 Blocks
		{
			for(j = 0;j < iBlockNum;j++)
			{
				for(k = 0;k < iAttNum;k++)
				{
					if(strcmp(pBlockTable[j][k],"-") == 0)
						continue;
				
					tempstring = pBlockTable[j][k];
					if(tempstring[0] == '[')
					{
						pos = tempstring.Find(',');
						trimstring = tempstring.Left(pos);
						trimstring.Remove('[');
						if(strcmp(trimstring,"*") != 0)
						{
							if(atof(trimstring) > atof(pStringTable[i][k]))
								break;
						}//end if
						trimstring = tempstring;
						trimstring.Delete(0,pos+1);
						trimstring.Remove(')');
						if(strcmp(trimstring,"*") != 0)
							if(atof(trimstring) <= atof(pStringTable[i][k]))
								break;
					}//end if
					else if(tempstring[0] == '{')
					{
						int m = 0;
						trimstring = tempstring;
						trimstring.Delete(0,1);
						pos = trimstring.Find(',');
						while(pos != -1)
						{
							compstring = trimstring.Left(pos);
							trimstring.Delete(0,pos+1);
							if(strcmp(compstring,pStringTable[i][k]) == 0)
							{
								m =1;
								break;
							}//end if
							pos = trimstring.Find(',');
						}//end while
						if(m == 0)
						{
							trimstring.Remove('}');
							if(strcmp(trimstring,pStringTable[i][k]) != 0)
								break;
						}//end if
					}//end if
					else
						if(atoi(pBlockTable[j][k]) != atoi(pStringTable[i][k]))
							break;
				}//end for
		
				if(k == iAttNum)		//如果块匹配
				{
					trimstring = pBlockTable[j][iAttNum];
					trimstring.Delete(0,5);
					if(temp[atoi(trimstring)] == 1)
					{
						temp[atoi(trimstring)] = 0;
						n--;
					}//end if
				}//end if
			}//end for
		}//end if

		if(n == 0)
		{
			strcpy(pRecogniseResultTable[i][0],"未识别");
			strcpy(pRecogniseResultTable[i][1],"-");
			if(bTest)
				iDentifyNone++;
			continue;
		}//end if

		if(n == 1)
		{
			for(j = 0;j < iRuleNum;j++)
			{
				if(temp[j] == 1)
					break;
			}//end for

			if(bTest)
			{
				if(strcmp(pRuleTable[j][iAttNum],pStringTable[i][iAttNum]) == 0)
					iDentifyRight++;
				else
				iDentifyError++;
			}
			strcpy(pRecogniseResultTable[i][0],pRuleTable[j][iAttNum]);
			_itoa(temp1+1,pRecogniseResultTable[i][1],10);
			continue;
		}//end if

		temp1 = -1;
		if(bTest)
			iTestConflict++;
		for(j = 0;j < iRuleNum;j++)
		{
			if(temp[j] == 1)
			{
				if(temp1 == -1)
				{
					temp1 = j;
					reliability = (float)atof(pRuleTable[j][iAttNum+1]);
					coveragenum = atoi(pRuleTable[j][iAttNum+2]);
					totalnum = atoi(pRuleTable[j][iAttNum+3]);
					strcpy(d,pRuleTable[j][iAttNum]);
				}//end if
				else
				{
					if(reliability == atof(pRuleTable[j][iAttNum+1]))
					{
						if(totalnum > atoi(pRuleTable[j][iAttNum+2]))
						{
							temp1 = j;
							reliability = (float)atof(pRuleTable[j][iAttNum+1]);
							coveragenum = atoi(pRuleTable[j][iAttNum+2]);
							totalnum = atoi(pRuleTable[j][iAttNum+3]);
							strcpy(d,pRuleTable[j][iAttNum]);
						}//end if
					}//end if
					else
					{
						if(totalnum == atoi(pRuleTable[j][iAttNum+3]))
						{
							if(reliability < atof(pRuleTable[j][iAttNum+1]))
							{
								temp1 = j;
								reliability = (float)atof(pRuleTable[j][iAttNum+1]);
								coveragenum = atoi(pRuleTable[j][iAttNum+2]);
								totalnum = atoi(pRuleTable[j][iAttNum+3]);
								strcpy(d,pRuleTable[j][iAttNum]);
							}//end if
						}//end if
						else
						{
							if(totalnum > atoi(pRuleTable[j][iAttNum+3]))
							{
								if(reliability < atof(pRuleTable[j][iAttNum+1]))
								{
									temp1 = j;
									reliability = (float)atof(pRuleTable[j][iAttNum+1]);
									coveragenum = atoi(pRuleTable[j][iAttNum+2]);
									totalnum = atoi(pRuleTable[j][iAttNum+3]);
									strcpy(d,pRuleTable[j][iAttNum]);
								}//end if
								else
								{
									if(coveragenum/(totalnum*totalnum) < 
										atoi(pRuleTable[j][iAttNum+2])/(atoi(pRuleTable[j][iAttNum+3])*atoi(pRuleTable[j][iAttNum+3])))
									{
										temp1 = j;
										reliability = (float)atof(pRuleTable[j][iAttNum+1]);
										coveragenum = atoi(pRuleTable[j][iAttNum+2]);
										totalnum = atoi(pRuleTable[j][iAttNum+3]);
										strcpy(d,pRuleTable[j][iAttNum]);
									}//end if
								}//end else
							}//end if
							else
							{
								if(reliability < atof(pRuleTable[j][iAttNum+1]))
								{
									if(coveragenum/(totalnum*totalnum) < 
										atoi(pRuleTable[j][iAttNum+2])/(atoi(pRuleTable[j][iAttNum+3])*atoi(pRuleTable[j][iAttNum+3])))
									{
										temp1 = j;
										reliability = (float)atof(pRuleTable[j][iAttNum+1]);
										coveragenum = atoi(pRuleTable[j][iAttNum+2]);
										totalnum = atoi(pRuleTable[j][iAttNum+3]);
										strcpy(d,pRuleTable[j][iAttNum]);
									}//end if
								}//end if
							}//end else
						}//end else
					}//end else
				}//end else
			}//end if
		}//end for

⌨️ 快捷键说明

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