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

📄 test.cpp

📁 网页分类器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			if(mFile.Open(str,CFile::modeRead) == 0) 
			{
				AfxMessageBox("请将测试集放在正确的位置!", MB_ICONINFORMATION);
				return FALSE; 
			}
			char seps[] = "(;:,)";   	
			char *token;//临时变量
			int i=0;
			mFile.ReadString(str); //从文件中读取一行内容到str中
			char string[10000];
			sprintf( string,"%s",str ); //定义字符数组string,将cstr复制到string中
			token = strtok( string, seps ); //strtok函数将seps指定的内容去掉以后得到剩下的,此时得到第一个有效数据type值
			Page.SetType(atoi( token ));
			token = strtok( NULL, seps );//strtok函数首次使用时赋值为字符串,再调用时赋值为NULL
		
			while( token != NULL )
			{
				if(i%2 == 0)
				{
					vi.nIndex = atoi( token );//定义i记数,当i为偶数时,保存关键词的索引
				}
				if(i%2 == 1)
				{
					vi.dPower = atof( token );//当i为奇数时,保存权重
					Page.SetCharacterVector( vi );
				}
				token = strtok( NULL, seps );//取得下一个有效数据     
				i++; 
			}
			
			mFile.Close(); //关闭文件

			//判断读得的测试集文件(即网页)的类别
			GetType( strNameTxtPath, strNameTxt, Page );
				
			p->m_progress.SetPos(pos++);
		}
	}
	f.Close();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    

	//将处理结果写入文件中
	CString strFileName;
	strFileName = pPath;
	strFileName+="\\result.txt";
	//当没有结果文件时创建并打开文件,有的话直接打开文件
	mFile.Open(strFileName,CFile::modeCreate|CFile::modeWrite); 

	//准备写入文件
	//写入文件格式是各个类别的网页总数和测试准确的数目
	CArchive ar(&mFile,CArchive::store); 

	for( int k = 0; k < KINDNUMBER; k++ )
	{
		ar << nTotal[k];
		ar << nRight[k];
		ar << nTotalTest[k];
	}

	ar.Close();
	mFile.Close();

	p->UpdateData(FALSE);
	return TRUE;
}

/********************************************************************
       // 创建日期   :  2008-5-19 18:59:21
	   // 作    者	  : 祝美莲<zml123818@163.com> 
       // 函数名称	  : CTest::Right
       // 函数功能	  : 判断分类是否正确
       // 返回类型	  : BOOL 
       // 函数参数	  : CString strName
       // 函数参数	  : int nType
********************************************************************/
BOOL CTest::Right(CString strName, int nType)
{
	CString str;//字符串,存放文件名
	int nPlace;   //存放路径中最后一个'/'的位置

	//赋初值,进行操作
	nPlace = strName.ReverseFind('\\');  
	if( nPlace == -1 )
		nPlace = strName.ReverseFind('/');
	str = strName.Right(strName.GetLength() - nPlace - 1);

	if( !str.IsEmpty() )
	{
		str = str.Left(1);//得到类别,因为是以文件名开始的一个字符表示类别
		if( nType == (atoi(str)) )//判断是否正确
		{
			return TRUE;
		}
	}	
	return FALSE;
}

/********************************************************************
       // 创建日期   :  2008-5-19 20:56:01
	   // 作    者	  : 祝美莲<zml123818@163.com> 
       // 函数名称	  : CTest::GetType
       // 函数功能	  : 处理网页信息
       // 返回类型	  : void 
       // 函数参数	  :  CString strNamePath
       // 函数参数	  : CString strName
       // 函数参数	  : CPageInfo Page
********************************************************************/
void CTest::GetType( CString strNamePath, CString strName, CPageInfo Page )
{
	CString str;  //存放文件名
	int nPlace;   //存放测试网页路径的最后一个'\'的位置

	//赋值,得到文件名
	nPlace = strNamePath.ReverseFind('\\');
	if( nPlace == -1 )
		nPlace = strNamePath.ReverseFind('/');
	str = strNamePath.Right(strNamePath.GetLength() - nPlace - 1);

	if( !str.IsEmpty())
	{
		str = str.Left(1);	//以左边的第一个字符表示类别
		nTotalTest[atoi(str)]++;//将相应的类别的网页数加1
	}
	else
		return;

	double VX[VECTORITEMNUMBER];
	Transform(Page,VX);
	
	char pBuf[MAX_PATH];
	strcpy(pBuf,pPath);
	strcat(pBuf,"\\结果文件夹\\");
	CString string;
	char pBuf0[MAX_PATH];
	strcpy(pBuf0, pBuf);

	int i=1;
	while(i!=nKindNumber)
	{
		if(Func(VX,i))//属于第i类		
		{
			string.Empty();
			string.Format( "%d",nKind[i-1]);
			string=string+"类网页\\";//网页路径
			strcat(pBuf0,string);
			strcat(pBuf0,strName);

			CopyFile(strNamePath,pBuf0,TRUE);//将文件写入文件夹中
			int k = nKind[i-1]-1;
			nTotal[k]++;//统计

			//得到文件名,判断是否测试的准确
			if( Right(strNamePath, k ) == TRUE )
				nRight[k]++;//统计

			break;//结束循环
		}
		else 
		{
			i++;
		}
		
	}
	if(i==nKindNumber)//如果是最后一类单独处理
	{
		string.Empty();
		string.Format( "%d",nKind[i-1]);
		string=string+"类网页\\";//路径
		strcat(pBuf0,string);
		strcat(pBuf0,strName);

		CopyFile(strNamePath,pBuf0,TRUE);//将文件写入文件夹中
		int k = nKind[i-1]-1;
		nTotal[k]++;

		//得到文件名,判断是否测试的准确
		if( Right(strNamePath, k ) == TRUE )
			nRight[k]++;
	}

	/*5月30号修改,改为使用循环来实现,从而方便程序的扩展*/

	/*if(Func(VX,1))			
	{
		string.Empty();
		string.Format( "%d",nKind[0]);
		string=string+"类网页\\";
		strcat(pBuf0,string);
		strcat(pBuf0,strName);

		CopyFile(strNamePath,pBuf0,TRUE);//将文件写入文件夹中
		int k = nKind[0]-1;
		nTotal[k]++;

		//得到文件名,判断是否测试的准确
		if( Right(strNamePath, k ) == TRUE )
			nRight[k]++;
	}
	else 
	{
		if(Func(VX,2))
		{
			string.Empty();
			string.Format( "%d",nKind[1]);
			string=string+"类网页\\";
			strcat(pBuf0,string);
			strcat(pBuf0,strName);

			CopyFile(strNamePath,pBuf0,TRUE);//将文件写入文件夹中
			int k = nKind[1]-1;
			nTotal[k]++;

			//得到文件名,判断是否测试的准确
			if( Right(strNamePath, k ) == TRUE )
				nRight[k]++;
		}
		else 
		{
			if(Func(VX,3))
			{
				string.Empty();
				string.Format( "%d",nKind[2]);
				string=string+"类网页\\";
				strcat(pBuf0,string);
				strcat(pBuf0,strName);

				CopyFile(strNamePath,pBuf0,TRUE);//将文件写入文件夹中
				int k = nKind[2]-1;
				nTotal[k]++;

				//得到文件名,判断是否测试的准确
				if( Right(strNamePath, k ) == TRUE )
					nRight[k]++;
			}
			else
			{
				string.Empty();
				string.Format( "%d",nKind[3]);
				string=string+"类网页\\";
				strcat(pBuf0,string);
				strcat(pBuf0,strName);

				CopyFile(strNamePath,pBuf0,TRUE);//将文件写入文件夹中
				int k = nKind[3]-1;
				nTotal[k]++;

				//得到文件名,判断是否测试的准确
				if( Right(strNamePath, k ) == TRUE )
					nRight[k]++;
			}
		}
	}*/

}


/********************************************************************
       // 创建日期   :  2008-5-19 20:56:24
	   // 作    者	  : 祝美莲<zml123818@163.com> 
       // 函数名称	  : CTest::GetTotal
       // 函数功能	  : 得到分类后各类网页的总数
       // 返回类型	  : void 
       // 函数参数	  : int nNumber[KINDNUMBER]
********************************************************************/
void CTest::GetTotal(int nNumber[KINDNUMBER])
{
	for(int i=0;i<KINDNUMBER;i++)
	{
		nNumber[i]=nTotal[i];
	}
}

/********************************************************************
       // 创建日期   :  2008-5-19 20:56:38
	   // 作    者	  : 祝美莲<zml123818@163.com> 
       // 函数名称	  : CTest::DeleteFolder
       // 函数功能	  : 删除文件夹
       // 返回类型	  : void 
       // 函数参数	  : CString   sPath
********************************************************************/
void CTest::DeleteFolder(CString   sPath)   
{   
	CFileFind   ff;   
	BOOL   bFound;   
	bFound = ff.FindFile(sPath   +   "\\*.*");   
	while(bFound)   
	{   
		bFound   =   ff.FindNextFile();   //递归查找
		CString   sFilePath   =   ff.GetFilePath();   
    
		if(ff.IsDirectory())   //文件夹
		{   
			if(!ff.IsDots())   
			DeleteFolder(sFilePath);   
		}   
		else   
		{   
			if(ff.IsReadOnly())   
			{   
				SetFileAttributes(sFilePath,   FILE_ATTRIBUTE_NORMAL);   
			}   
			DeleteFile(sFilePath);   
		}   
	 }   
	 ff.Close();   
	 SetFileAttributes(sPath,   FILE_ATTRIBUTE_NORMAL);   
	 RemoveDirectory(sPath);   //删除空的文件夹
}



⌨️ 快捷键说明

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