yyzsamplerecognise1.cpp

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

CPP
1,917 行
字号

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

	delete[] temp;
	delete[] d;
}

void CLieSampleRecogniseYyz1::SampleRecogniseMany()
{
	TRACE("sample recognise mang\n");

	int i,j;
	int* temp;
	int temp1 = -1;
	float reliability;
	int totalnum;
	int coveragenum;
	int n = 0;

	char* d;

	pRecogniseResultTable = new char**[iTestNum];
	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 compstring;
		
	for(i = 0;i < iTestNum;i++)
	{
		int MaxMatch=0;
		n = 0;
		for(j = 0;j < iRuleNum;j++)
			temp[j] = 0;
		for(j = 0;j < iRuleNum;j++)
		{
			if ( IsRuleAttAllNull(j) )
			{
				if( IsTestAttAllNull(i) )
				{
					for(int x=0;x<j;x++)
					temp[x]=0;
					temp[j]=1;
					MaxMatch=1;
					temp1=j;
					n=1;
				}
				break;
			}
			if( ( temp[j]=Match(i,j) )!=0)
			{
				n++;
			}
			if(temp[j] > MaxMatch)
			{
				MaxMatch=temp[j];
				temp1=j;
			}
		}
		if(n>1)
		{
			for(int x=0;x<iRuleNum;x++)
			{
				if(temp[x]!=0 && MaxMatch>0 && temp[x]<MaxMatch )
				{
					temp[x]=0;//考虑最长匹配
					n--;
				}
				if(temp[x]>=1)		
					temp[x]=1;
	
			}		
		}


/*
		if(n != 0 && bBlock)
		{
			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*coveragenum)/totalnum < 
										(atoi(pRuleTable[j][iAttNum+2])*atoi(pRuleTable[j][iAttNum+2]))/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

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

	delete[] temp;
	delete[] d;
}

void CLieSampleRecogniseYyz1::SaveRecogniseResult(LPCTSTR lpszPathName)
{
	FILE* fp;
	fp = fopen(lpszPathName,"w");
	fprintf(fp,"Style:%s\n","result");
	if(bTest)
		fprintf(fp,"Stage:0\n");
	else
		fprintf(fp,"Stage:1\n");
	fprintf(fp,"Condition attributes number:%d\n",iAttNum);
	fprintf(fp,"Tests number:%d\n",iTestNum);
	if(bTest)
	{
		for(int i = 0;i < iAttNum+1;i++)
		fprintf(fp,"%s ",pAttName[i]);
	   bTest=1;
	}
	else
	{
		for(int i = 0;i < iAttNum;i++)
			fprintf(fp,"%s ",pAttName[i]);
	}

	fprintf(fp,"识别值 ");
	fprintf(fp,"所用规则\n");
	for(int i = 0;i < iTestNum;i++)
	{
		TRACE("%d",i);
		if(bTest)
		{
			for(int j = 0;j < iAttNum+1;j++)
				fprintf(fp,"%s ",pStringTable[i][j]);
		}
		else
		{
			for(int j = 0;j < iAttNum;j++)
				fprintf(fp,"%s ",pStringTable[i][j]);
		}
		fprintf(fp,"%s ",pRecogniseResultTable[i][0]);
		fprintf(fp,"%s\n",pRecogniseResultTable[i][1]);
	}
	if(bTest)
	{
		fprintf(fp,"正确识别数:%d\n",iDentifyRight);
		fprintf(fp,"错误识别数:%d\n",iDentifyError);
		fprintf(fp,"未识别 数:%d\n",iDentifyNone);
		fprintf(fp,"冲突样本数:%d\n",iTestConflict);
		fprintf(fp,"决策正确数:%d\n",iTestRight);
		fprintf(fp,"决策错误数:%d\n",iTestError);
	}
	

	fclose(fp);
}


void CLieSampleRecogniseYyz1::ChangeString()
{
	TRACE(" changestring \n");
	int i,j;
	struct CutRecord* pHead;
	
	for(i = 0;i < iRecordNum;i++)
	{
		for(j = 0;j < iAttNum;j++)
		{
			if(strcmp(pStringTable[i][j],"-") == 0)
				continue;
			pHead = struCutRecord;
			while(pHead != NULL)
			{
				if(pHead->iColumn == j)
					break;
				pHead = pHead->p;
			}//end while
			if(pHead != NULL)
				strcpy(pStringTable[i][j],pHead->cpCut[pIntTable[i][j]]);
		}//end for
	}//end for
}
BOOL CLieSampleRecogniseYyz1::SetAttName(FILE* fp, int count)
{
	TRACE("set att name\n");

	int i;
	if(pAttName == NULL)
	{
		try
		{
			pAttName = new char*[count];
		}
		catch(CMemoryException* e)
		{
			TRACE("memeory error  1\n");
			::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]);
 return TRUE;
}
BOOL CLieSampleRecogniseYyz1::SetBlockTable(FILE *fp, int column, int row)
{
	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(j = 0;j < column;j++)
			fscanf(fp,"%s",pBlockTable[i][j]);
     	return TRUE;
}

BOOL CLieSampleRecogniseYyz1::SetDataType(FILE *fp, int count)
{
	TRACE("SetDatType\n");

	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++)
 for(i = 0;i < count;i++)
		fscanf(fp,"%s",pDataType[i]);
	return TRUE;
}
BOOL CLieSampleRecogniseYyz1::SetRuleTable(FILE *fp, int column, int row)
{
	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
	///先读列再读行
	for(i = 0;i < row;i++)
		for(j = 0;j < column;j++)
			fscanf(fp,"%s",pRuleTable[i][j]);
//	ofstream outout;
return TRUE;
}

bool CLieSampleRecogniseYyz1::IsRuleAttAllNull(int row)
{
	for(int i=0;i<iAttNum;i++)
		if(stricmp(pRuleTable[row][i],"-")!=0)
			return false;
	return true;
}

bool CLieSampleRecogniseYyz1::IsTestAttAllNull(int row)
{
	int k=0;
	for(int i=0;i<iAttNum;i++)
	{
		if(atbcount>k)
			if(i==atb[k])
			{
				k++;
				continue;
			}
		if(stricmp(pStringTable[row][i],"-")!=0)
			return false;
	}
	return true;
}

int CLieSampleRecogniseYyz1::Match(int test,int rule )
{
	int n=0,i;
	CString tempstring,trimstring;

	for(i=0;i<iAttNum;i++)
	{
		if( pStringTable[test][i][0]=='-' || pRuleTable[rule][i][0]=='-')
		{
			n++;
			continue;
		}
		tempstring = pRuleTable[rule][i];
		if(tempstring[0] == '[')
		{
			int pos = tempstring.Find(',');
			trimstring = tempstring.Left(pos);
			trimstring.Remove('[');
			if(strcmp(trimstring,"*") != 0)
			{
				if(atof(trimstring) > atof(pStringTable[test][i]))
					return 0;		//l比下限小,退出该循环
			}//end if
			trimstring = tempstring;
			trimstring.Delete(0,pos+1);
			trimstring.Remove(')');
			if(strcmp(trimstring,"*") != 0)
				if(atof(trimstring) <= atof(pStringTable[test][i]))
					return 0;     //比上限大,退出该循环
			n+=iAttNum+1;
			continue;
		}//end if
		
		if( atoi(pStringTable[test][i]) == atoi(pRuleTable[rule][i]))
			n+=iAttNum+1;
		else
			return 0;
	}
	return n;
}

⌨️ 快捷键说明

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